标签:
str,int,list,tuple,dict 是类型调用之后会产生一个 实例
1 >>> brand=["李宁",‘耐克‘,‘阿迪达斯‘,‘鱼C‘] 2 >>> slogan=[‘一切皆有可能‘,‘Just do it‘,‘Impossible is nothing‘,‘让编程改变世界‘] 3 >>> print("鱼C的口号是:",slogan[brand.index(‘鱼C‘)]) 4 鱼C的口号是: 让编程改变世界
1 >>> dict1={"李宁":"一切皆有可能",‘耐克‘:"Just do it",‘阿迪达斯‘:‘Impossible is nothing‘} 2 >>> print(dict1[‘李宁‘]) 3 一切皆有可能 4 >>> print(dict1["李宁"]) 5 一切皆有可能
1 >>> # dict只能传入一个参数 . 2 >>> dict1=dict(((‘F‘,70),[‘i‘,105])) 3 >>> dict1 4 {‘F‘: 70, ‘i‘: 105}
1 >>> # dict只能传入一个参数 . 2 >>> dict1=dict(((‘F‘,70),[‘i‘,105])) 3 >>> dict1 4 {‘F‘: 70, ‘i‘: 105} 5 >>> dict1[‘F‘]=10000 6 >>> dict1 7 {‘F‘: 10000, ‘i‘: 105} 8 >>> dict1["QQ"]="钱" 9 >>> dict1 10 {‘F‘: 10000, ‘i‘: 105, ‘QQ‘: ‘钱‘}
1 >>> dict1.fromkeys((1,2,3)) 2 {1: None, 2: None, 3: None} 3 >>> dict1.fromkeys((1,2,3),‘num‘) 4 {1: ‘num‘, 2: ‘num‘, 3: ‘num‘}
1 >>> dict1=dict1.fromkeys(range(1,9),‘赞‘) 2 Traceback (most recent call last): 3 File "<pyshell#0>", line 1, in <module> 4 dict1=dict1.fromkeys(range(1,9),‘赞‘) 5 NameError: name ‘dict1‘ is not defined 6 >>> dict1={} 7 >>> dict1=dict1.fromkeys(range(1,9),‘赞‘) 8 >>> dict1 9 {1: ‘赞‘, 2: ‘赞‘, 3: ‘赞‘, 4: ‘赞‘, 5: ‘赞‘, 6: ‘赞‘, 7: ‘赞‘, 8: ‘赞‘}
1 >>> for i in dict1.items(): 2 i 3 4 5 (1, ‘赞‘) 6 (2, ‘赞‘) 7 (3, ‘赞‘) 8 (4, ‘赞‘) 9 (5, ‘赞‘) 10 (6, ‘赞‘) 11 (7, ‘赞‘) 12 (8, ‘赞‘)
1 >>> for i in dict1.values(): 2 i 3 4 5 ‘赞‘ 6 ‘赞‘ 7 ‘赞‘ 8 ‘赞‘ 9 ‘赞‘ 10 ‘赞‘ 11 ‘赞‘ 12 ‘赞‘
1 >>> a={1:‘one‘,2:‘two‘} 2 >>> b=a.copy() 3 >>> c=a 4 >>> id(a) 5 48682848 6 >>> id(b) 7 48854120 8 >>> id(c) 9 48682848 10 >>> # 由此可见 浅拷贝是 将内容放到另一块的储存区域中
1 >>> a={1:‘one‘,2:‘two‘} 2 >>> b={3:‘three‘,4:‘four‘} 3 >>> a=a+b 4 Traceback (most recent call last): 5 File "<pyshell#2>", line 1, in <module> 6 a=a+b 7 TypeError: unsupported operand type(s) for +: ‘dict‘ and ‘dict‘ 8 >>> a.update(b) 9 >>> a 10 {1: ‘one‘, 2: ‘two‘, 3: ‘three‘, 4: ‘four‘} 11 >>> b.update(a) 12 >>> b 13 {1: ‘one‘, 2: ‘two‘, 3: ‘three‘, 4: ‘four‘}
标签:
原文地址:http://www.cnblogs.com/A-FM/p/5663082.html