字典dict:索引不好用时
dir(dict):.fromkeys&.keys()& .values()&.items()&.get()&clear/copy
dict1={'李宁':'一切皆有可能','耐克':'Just do it','阿迪达斯':'Impossible is nothing','鱼C工作室':'让编程改变世界'} print('鱼C工作室的口号是:',dict1[('鱼C工作室')]) dict2={1:'one',2:'two',3:'three',} print(dict2[2])
>>> a = dict(one=1, two=2, three=3)
>>> b = {'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)])
>>> e = dict({'three': 3, 'one': 1, 'two': 2})
dict3=dict((("F",70),("i",105),("s",115),("h",104),("C",67))) >>>dict3 {'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67} dict4=dict(小甲鱼='让编程改变世界',苍井空='让AV征服所有宅男') >>> dict4 {'小甲鱼': '让编程改变世界', '苍井空': '让AV征服所有宅男'}
dict4=dict(小甲鱼='让编程改变世界',苍井空='让AV征服所有宅男')
>>> dict4{'小甲鱼': '让编程改变世界', '苍井空': '让AV征服所有宅男'}
>>> dict4['苍井空']='所有者AV从业都要通过学习编程来提高职业技能'
>>> dict4{'小甲鱼': '让编程改变世界', '苍井空': '所有者AV从业都要通过学习编程来提高职业技能'}
>>> dict4['Thomas Edison']='The talent is 1% inspiration adds on 99% sweat, certainly, does not have that 1% inspiration, in the world all sweat to put or bringtogether also only is the sweat!'
>>> dict4{'小甲鱼': '让编程改变世界', '苍井空': '所有者AV从业都要通过学习编程来提高职业技能', 'Thomas Edison': 'The talent is 1% inspiration adds on 99% sweat, certainly, does not have that 1% inspiration, in the world all sweat to put or bringtogether also only is the sweat!'}
>>> dict1.fromkeys((1,2,3)) {1: None, 2: None, 3: None} >>> dict1.fromkeys((1,2,3),'number') {1: 'number', 2: 'number', 3: 'number'} >>> dict1.fromkeys((1,2,3),('one','two','three')) {1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')} >>> dict1.fromkeys((1,3),'数字') {1: '数字', 3: '数字'}
dict1=dict1.fromkeys(range(30),'赞!') for eachKey in dict1.keys(): print(eachKey) for eachValue in dict1.values(): print(eachValue) for eachItem in dict1.items(): print(eachItem) print(dict1[32]) dict1.get(32) >>> 32 in dict1 False >>> 29 in dict1 True >>> print(dict1.get(32)) None >>> dict1.get(32,'木有') '木有'
>>> dir(dict) ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] >>> a={'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67} >>> b=a >>> c=a.copy() >>> a {'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67} >>> b {'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67} >>> c {'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67} >>> id(a) 2020710601784 >>> id(b) 2020710601784 >>> id(c) 2020710646552
>>> c[6]='小甲鱼'
>>> c
{'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67, 6: '小甲鱼'}
>>> b
{'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67}
>>> a
{'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67}
>>> c.popitem()
('h', 104)
>>> a.setdefault('小黑鱼')
>>> a
{'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67, '小黑鱼': None}
集合set:没有体现映射关系,元素唯一性是他的特征
>>> num={} >>> type(num) <class 'dict'> >>> num2={1,2,3,4} >>> type(num2) <class 'set'>
>>> num2={1,2,3,4,5,4,3,2,1}
>>> num2
{1, 2, 3, 4, 5}
>>> set1=set([1,2,3,4,5,4,3,'小甲鱼'])
>>> set1
{1, 2, 3, 4, 5, '小甲鱼'}
>>> num1=[1,2,3,4,5,6,7,6,5,4,3,2,1,0]
>>> temp=[]
>>> for each in num1:
if each not in temp:
temp.append(each)
>>> temp
[1, 2, 3, 4, 5, 6, 7, 0]
>>> num1=list(set(num1))
>>> num1
[0, 1, 2, 3, 4, 5, 6, 7]
>>> num2
{1, 2, 3, 4, 5}
>>> 1 in num2
True
>>> '1' in num2
False
>>> num2.add(6)
>>> num2
{1, 2, 3, 4, 5, 6}
>>> num2.remove(5)
>>> num2
{1, 2, 3, 4, 6}
>>> num3
frozenset({1, 2, 3, 4, 6})
集合类型內建方法