文字列、リスト、メタグループ、コレクション、辞書の総合比較

9775 ワード

一.概念文字列string:一連の文字、引用符を使用して「」リストリストリストリストを作成する:一連の特定の順序で配列された要素から構成され、リストを作成し、カンマで区切られた異なるデータ項目を角カッコで囲むだけでよい[]メタグループtuple:リストクラスと同様に、カッコに要素を追加し、カンマで区切るだけでよい.肝心なのはカンマである.単一要素の後にカンマ()セットsetを付ける必要があります:無秩序な重複しない要素シーケンスです.カッコ{}またはset()関数を使用して、集合辞書dictionaryを作成できます.辞書は別の可変コンテナモデルであり、任意のタイプのオブジェクトを格納できます.辞書の各キー値(key=>value)対はコロン(:)で分割され、各対の間はカンマ(,)で分割され、辞書全体がカッコ({})に含まれる.二.総合比較1.作成
>>> c=''
>>> len(c)
0#         ,           0 ,len()             
>>> not ''
True#      
>>> str([1,2,3])	
'[1, 2, 3]'
>>> str(True)	
'True'
>>> 
>>> resum ='''name = "wuhzigao" company = "hbsf"'''
>>> resum
'name = "wuhzigao" company = "hbsf"'#            ,             
----------------------------------------------------------------
>>> b=[]
>>> type(b)

print(list())
>>> []#     
>>> a=[1,'asd',[1,2,3],(1,2,3),{2,3,4,5},{1:2,3:4},True]
>>> type(a)
#            
----------------------------------------------------------------------
>>> a=()
>>> type(a)

print(tuple())
>>>()#     
>>> a=1,'asd',[1,2,3],(1,2,3),{2,3,4,5},{1:2,3:4},True
>>> type(a)
#            ,      
>>> a=('asghh',)
>>> type(a)#        
------------------------------------------------------------------------
print(set())
>>>set()#            ,  {}   
>>> a={1,'asd',(2,2,3),True}
>>> type(a)
#                、  、   ,        、  、  
>>> a=set(range(10))
>>> print(a)#  range    
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
-------------------------------------------------------------------------
>>> a={}
>>> type(a)

print(dict())
>>>{}#     
>>> a={'gdh':1,False:'asd',3:[1,2,3],(23,'qwer'):(1,2,3),2:{2,3,4,5},3:{1:2,3:4},4:True}
>>> type(a)
#          ,         ,    ,     
>>> a=dict(spam=1,egg=2,bar=3)#            
>>> print(a)
{'spam': 1, 'egg': 2, 'bar': 3}#     dict        
>>> print(dict([('spam',1),('egg',2),('bar',3)]))
{'spam': 1, 'egg': 2, 'bar': 3}#         
>>> dic = dict(zip('abc', [1, 2, 3]))
>>> dic
{'a': 1, 'c': 3, 'b': 2}#dict zip    
>>> dic = {i:2*i for i in range(3)}
>>> dic
{0: 0, 1: 2, 2: 4}#         
>>> dic = dict.fromkeys(range(3), 'x')
>>> dic
{0: 'x', 1: 'x', 2: 'x'}#  dict.fromkeys()  ,         ,   value    

2.インデックス(アクセス)
>>> suoyin='cdgshjadjkhsd1223'
>>> suoyin[4]
'h'
>>> type(suoyin[4])

>>> suoyin[-1]
'3'                                                         
--------------------------------------------------------------------------
 >>> a=[1,'asd',[1,2,3],(1,2,3),{2,3,4,5},{1:2,3:4},True]
>>> a[5]
{1: 2, 3: 4}
>>> a[-3]
{2, 3, 4, 5}                                               
---------------------------------------------------------------------------
>>> a=(1,'asd',[1,2,3],(1,2,3),{2,3,4,5},{1:2,3:4},True)
>>> a[3]
(1, 2, 3)
>>> a[-7]
1                                                          
-----------------------------------------------------------------------------
>>> a={1,2,2,3,4,5,6,7,4}
>>> print(a)
{1, 2, 3, 4, 5, 6, 7}
>>> a[1]
Traceback (most recent call last):
  File "", line 1, in 
    a[1]
TypeError: 'set' object does not support indexing
#        ,                                       
-------------------------------------------------------------------------------------------------
>>> a={'gdh':1,False:'asd',3:[1,2,3],(23,'qwer'):(1,2,3),2:{2,3,4,5},3:{1:2,3:4},4:True}
>>> a[0]#   False
'asd'
>>> a[3]#    ,           ,           ,                        
{1: 2, 3: 4}
>>> a[8]
Traceback (most recent call last):
  File "", line 1, in 
    a[8]
KeyError: 8
>>> a[3][1]
2#  a[3]  {1: 2, 3: 4},    [1]  2                  

2.更新(添削・改ざん)、長さ
>>> a='adhjkliu'
>>> len(a)	
8	
>>> a[3]='l'	
Traceback (most recent call last):
  File "", line 1, in #           
    a[3]='l'
TypeError: 'str' object does not support item assignment
>>> b=a[3]+'ong'	
>>> print(b)	
jong#              
>>> a='abc'	
>>> 'a' in a	#    
True
>>> 'ab' in a	
True
>>> 'ac' in a	
False
>>> 'a b' in a	
False
---------------------------------------------------------------------------------
>>> a=[1,2,3]
>>>len(a)
3	
>>> a[2]=4	
>>> print(a)	
[1, 2, 4]#    /  
>>> a.append('4')	
>>> print(a)	#         
[1, 2, 3, '4']
b=[1,2,3,4,5]
>>> b.extend([3,4,5,6])#           	
>>> print(b)	
[1, 2, 3, 4, 5, 3, 4, 5, 6]
>>> a.insert(2,'jjs')	
>>> print(a)	#           
[1, 2, 'jjs', 3, '4']
>>> a.remove('4')#        	
>>> print(a)	
[1, 2, 'jjs', 3]
>>> a.pop()	#               
3
>>> print(a)	
[1, 2, 'jjs']
>>> a.pop(1)#                   	
2
>>> print(a)	
[1, 'jjs']
>>> del a[0]	#         
>>> print(a)	
['jjs']
>>> a=[1,2,3,4,5]	
>>> 1 in a#    in    	
True
--------------------------------------------------------------------------------
             ,   、    
>>> a=(1,2,3,4)
>>> len(a)	
4	
>>> 1 in a#  	
True
>>>
>>> a=(1,2,3,4)
>>>> del a#        ,         ,       	
>>> print(a)	
Traceback (most recent call last):
  File "", line 1, in 
    print(a)
NameError: name 'a' is not defined
-----------------------------------------------------------------------------
>>> a={1,1,2,3,23,4,5}
>>> len(a)	
6	
>>> a.add(1)#           ,     、   、       	
>>> print(a)	
{1, 2, 3, 4, 5, 23}
>>> a.add(123345)#        ,        	
>>> print(a)	
{1, 2, 3, 4, 5, 123345, 23}
>>> a={1,2,3,4,5}
>>> a.update('7',(8,9),[10,11],{12:13,14:15},{16,17})	#        ,     ,  ,  ,  ,  ,      
>>> print(a)	
{1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 14, '7', 16, 17}#  
{1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 14, '7', 16, 17}
>>> a.remove(1)	#    ,         
>>> print(a)	
{2, 3, 4, 5, 8, 9, 10, 11, 12, 14, '7', 16, 17}
>>> a.discard('7')	#    ,            
>>> print(a)
{2, 3, 4, 5, 8, 9, 10, 11, 12, 14, 16, 17}
>>> a.pop()#          	
2	
>>> 2 not in a	
True#    
-----------------------------------------------------------------------------------
>>> a={'wade':3,'bosh':1,'lebron':6}
>>> len(a)	
3
>>> a['paul']=3	#       - 
>>> print(a)	
{'wade': 3, 'bosh': 1, 'lebron': 6, 'paul': 3}
>>> a['leborn']=23#          	
>>> print(a)	
{'wade': 3, 'bosh': 1, 'lebron': 6, 'paul': 3, 'leborn': 23}#              ,                      
>>> print(a['leborn'])	
23
>>> del a['paul']#     	
>>> print(a)	
{'wade': 3, 'bosh': 1, 'lebron': 6, 'leborn': 23}
>>> a.clear()#          	
>>> print(a)
{}
>>> del a#      	
>>> print(a)	
Traceback (most recent call last):
  File "", line 1, in 
    print(a)
NameError: name 'a' is not defined
>>> a={'wade': 3, 'bosh': 1, 'lebron': 6, 'paul': 3}	
>>> 'wade' in a#     	
True

3.接合、繰返し、複製、最小値最大値
つなぎ合わせる
>>> a={1,2,3}	
>>> b={2,3,4}	
>>> print(a+b)	
Traceback (most recent call last):
  File "", line 1, in 
    print(a+b)
TypeError: unsupported operand type(s) for +: 'set' and 'set'
>>> a={1:2}	
>>> b={2:3}	
>>> print(a+b)	
Traceback (most recent call last):
  File "", line 1, in 
    print(a+b)
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
#   、  、      、  *,  ,       ,  *
#*             id,              ,                

4.遍歴、スライス、コピー(省略)、ネスト
a={1,1,2,3,4,5}
for i in a:
    print(i)#      :   、  、  、  
1
2
3
4
5
players={'wade':3,'bosh':1,'leborn':6}
for player in players:# for             
    print("key:",player)#   
key: wade
key: bosh
key: leborn   #                
a=(1,2,3,4)
print(a[0:2])#   、  、       ,  ,     
print(a[:])#   

一連の辞書をリストに格納するか、辞書に格納し、それ自体を格納します.すなわち、ネスト5.内蔵関数、方法、個性的な特徴は次のbolgを参照してください.