>>> set1 = set((1,2,3,4,5)) >>> set1 {1, 2, 3, 4, 5} >>> set2 = {1,2,3,4,5}
>>> set1 = {1,2,3,4,6,5} >>> set2 = {2,3,4,5,7,8} >>> set1&set2 #交集 {2, 3, 4, 5} >>> set1|set2 #并集 {1, 2, 3, 4, 5, 6, 7, 8} >>> set1-set2 #差集 {1, 6}
add:增长一个元素post
>>> set1.add(8) >>> set1 {1, 2, 3, 4, 5, 6, 8}
update:将一个集合的全部元素添加到原来集合中spa
>>> set1.update({'a','s','d','f'}) >>> set1 {'a', 1, 2, 3, 4, 5, 6, 'd', 8, 's', 'f'}
pop: 删除任意一个元素code
{'a', 1, 2, 3, 4, 5, 6, 'd', 8, 's', 'f'} >>> set1.pop() 'a' >>> set1 {1, 2, 3, 4, 5, 6, 'd', 8, 's', 'f'}
remove: 删除指定元素对象
>>> set1 {1, 2, 3, 4, 5, 6, 'd', 8, 's', 'f'} >>> set1.remove(8) >>> set1 {1, 2, 3, 4, 5, 6, 'd', 's', 'f'}
isdisjoint 有交集返回Falseblog
>>> set1 {1, 2, 3, 4, 5, 6, 'd', 's', 'f'} >>> set2 {2, 3, 4, 5, 7, 8} >>> set1.isdisjoint(set2) False
issubset 判断是否包含于rem
>>> set1 {1, 2, 3, 4, 5, 6, 'd', 's', 'f'} >>> set2 {2, 3, 4, 5, 7, 8} >>> set1.issubset(set2) False
issuperset 判断是否包含get
>>> set1 {1, 2, 3, 4, 5, 6, 'd', 's', 'f'} >>> set2 {2, 3, 4, 5, 7, 8} >>> set1.issuperset(set2) False
>>> dict1 = {'name':'炎志','age':'18'} >>> dict2 = dict(name = '炎志',age = '18') >>> dict1 {'name': '炎志', 'age': '18'} >>> dict2 {'name': '炎志', 'age': '18'}
>>> dict1 {'name': '炎志', 'age': '18'} >>> dict2 {'name': '炎志', 'age': '18'} >>> dict1['name'] = '刘阳' #修改 >>> dict2['post'] = '老师' #添加 >>> dict1 {'name': '刘阳', 'age': '18'} >>> dict2 {'name': '炎志', 'age': '18', 'post': '老师'}
pop:经过keyj键删除对应value键值,并返回it
>>> dict1 {'name': '刘阳', 'age': '18'} >>> dict2 {'name': '炎志', 'age': '18', 'post': '老师'} >>> dict2.pop('post') '老师' >>> dict2 {'name': '炎志', 'age': '18'}
popitem : 删除一个键值对,并返回io
>>> dict2 {'name': '炎志', 'age': '18'} >>> dict2.popitem() ('name', '炎志') >>> dict2 {'age': '18'}
clear :清空列表class
>>> dict1 {'name': '刘阳', 'age': '18'} >>> dict1.clear() >>> dict1 {}
get :经过key查找对应的value值
>>>dict1 = {'name':'刘阳','age':'18'} >>> dict1.get('name') '刘阳'
keys : 查找出列表内全部的key
>>> dict1 = {'name':'刘阳','age':'18'} >>> dict1.keys() dict_keys(['name', 'age'])
values :查询全部的value
>>> dict1 = {'name':'刘阳','age':'18'} >>> dict1.values() dict_values(['刘阳', '18'])
items: 显示全部的键值对
>>> dict1 = {'name':'刘阳','age':'18'} >>> dict1.items() dict_items([('name', '刘阳'), ('age', '18')])
update:把一个字典里的键值对添加的原来的字典里
>>> dict1 {'name': '刘阳', 'age': '18'} >>> dict1.update({'from':'沈阳'}) >>> dict1 {'name': '刘阳', 'age': '18', 'from': '沈阳'}
setdefault 有则查,无则增
>>> dict1 {'name': '刘阳', 'age': '18', 'from': '沈阳'} >>> dict1.setdefault('from') #查 '沈阳' >>> dict1.setdefault('school') #增 >>> dict1 {'name': '刘阳', 'age': '18', 'school': None, 'from': '沈阳'}
copy:复制
>>> dict1 {'name': '刘阳', 'age': '18', 'school': None, 'from': '沈阳'} >>> dict2 = dict1.copy() >>> dict2 {'name': '刘阳', 'age': '18', 'school': None, 'from': '沈阳'}
fromkeys : 返回一个新的字典(建立字典)
>>> dict.fromkeys(['aa','bb'],[1,2]) {'bb': [1, 2], 'aa': [1, 2]} >>> dict.fromkeys(['aa','bb']) {'bb': None, 'aa': None} >>> dict.fromkeys(['aa','bb'],{1,2}) {'bb': {1, 2}, 'aa': {1, 2}}
>>> set1 {1, 2, 3, 4, 5, 6, 'd', 's', 'f'} >>> dict1 {'name': '刘阳', 'age': '18', 'school': None, 'from': '沈阳'} >>> type(set1) <class 'set'> >>> type(dict) <class 'type'> >>> type(dict1) <class 'dict'>
判断对象类型:isinstance
>>> isinstance(set1,dict) False >>> isinstance(set1,set) True
>>> 1 == 1 True >>> 2 == 1 False
>>> 1 != 1 False >>> 2 != 1 True
>>> True and False False >>> True and True True >>> False and False False
>>> False or False False >>> True or False True >>> True or True True
>>> not True False >>> not False True