python中字典,元組,集合
阿新 • • 發佈:2018-06-01
luchangshan5200python中的字典,元組,集合
-dict
-tuple
-set
dict 字典增刪改查
字典創建
my_dict = {‘a‘:1,‘b‘:2}
my_dict
{‘a‘: 1, ‘b‘: 2}
de8ug = {‘name‘:‘de8ug‘,
‘city‘:‘beijing‘,
‘code‘:‘python‘} #個人信息
de8ug[‘name‘]
‘de8ug‘
de8ug[‘city‘]
‘beijing‘
de8ug.get(‘name‘) #嘗試去獲取name的值
‘de8ug‘
de8ug.get(‘beijing‘) #如果沒有返回為空
none
de8ug.get(‘beijing‘,‘shanghai‘) #如果為空,就返回為上海
‘shanghai‘
字典的增加
de8ug[‘age‘] = 18
de8ug
{‘name‘: ‘de8ug‘, ‘city‘: ‘beijing‘, ‘code‘: ‘python‘, ‘age‘: 18}
de8ug[‘student‘] = [‘lilei‘,‘hmm‘,‘tony‘] #字典嵌套列表
de8ug
{‘name‘: ‘de8ug‘, ‘city‘: ‘beijing‘, ‘code‘: ‘python‘, ‘age‘: 18, ‘student‘: [‘lilei‘, ‘hmm‘, ‘tony‘]}
book = {‘name‘:‘python3‘,‘price‘:100}
de8ug[‘book‘] = book #字典嵌套字典
de8ug
{‘name‘: ‘de8ug‘,
‘city‘: ‘beijing‘,
‘code‘: ‘python‘,
‘age‘: 18,
‘student‘: [‘lilei‘, ‘hmm‘, ‘tony‘],
‘book‘: {‘name‘: ‘python3‘, ‘price‘: 100}}
字典的刪除
del de8ug[‘name‘]
de8ug
{‘city‘: ‘beijing‘, ‘code‘: ‘python‘, ‘age‘: 18, ‘student‘: [‘lilei‘, ‘hmm‘, ‘tony‘], ‘book‘: {‘name‘: ‘python3‘, ‘price‘: 100}}
字典的修改
de8ug[‘age‘] = 20
de8ug
{‘city‘: ‘beijing‘,
‘code‘: ‘python‘,
‘age‘: 20,
‘student‘: [‘lilei‘, ‘hmm‘, ‘tony‘],
‘book‘: {‘name‘: ‘python3‘, ‘price‘: 100}}
字典的各種查
de8ug[‘book‘]
{‘name‘: ‘python3‘, ‘price‘: 100}
de8ug[‘book‘][‘price‘] #類似找誰家的小孩
100
de8ug.keys()
dict_keys([‘city‘, ‘code‘, ‘age‘, ‘student‘, ‘book‘])
[ i for i in de8ug.keys()]
[‘city‘, ‘code‘, ‘age‘, ‘student‘, ‘book‘]
[ i for i in de8ug]
[‘city‘, ‘code‘, ‘age‘, ‘student‘, ‘book‘]
de8ug.items() #所有項
dict_items([(‘city‘, ‘beijing‘), (‘code‘, ‘python‘), (‘age‘, 20), (‘student‘, [‘lilei‘, ‘hmm‘, ‘tony‘]), (‘book‘, {‘name‘: ‘python3‘, ‘price‘: 100})])
for item in de8ug.items():
print(item) #去每一項
(‘city‘, ‘beijing‘)
(‘code‘, ‘python‘)
(‘age‘, 20)
(‘student‘, [‘lilei‘, ‘hmm‘, ‘tony‘])
(‘book‘, {‘name‘: ‘python3‘, ‘price‘: 100})
tuple 元組的增刪改查
why,what,how
坐標(x,y)
長方體(x,y,z)
cor = 23,56
type(cor)
tuple
cor2 = (23,56)
type(cor2)
tuple
#不可變
ip_port = (‘192.168.1.1‘,8001)
host = {}
host[‘ip_port‘] = ip_port #一個是key,一個是value
host
{‘ip_port‘: (‘192.168.1.1‘, 8001)}
host[‘ip_port‘][0]
‘192.168.1.1‘
host[‘ip_port‘][1]
8001
#類似列表,不需要修改時候使用
ip = (‘192.168.1.2‘)
type(ip)
str
host[ip] = ‘adminpc‘
host
{‘ip_port‘: (‘192.168.1.1‘, 8001), ‘192.168.1.2‘: ‘adminpc‘}
ip = (‘192.168.1.2‘,) #元組成對出現
type(ip)
tuple
tuple與list相互轉換
names = [‘lilei‘,‘hmm‘,‘jack‘]
type(names)
list
tuple(names)
(‘lilei‘, ‘hmm‘, ‘jack‘)
type(names)
list
type(tuple(names))
tuple
names
[‘lilei‘, ‘hmm‘, ‘jack‘]
host[names] = ‘admin‘ #報錯,列表可變
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-69-c0390f036cc8> in <module>()
----> 1 host[names] = ‘admin‘
TypeError: unhashable type: ‘list‘
name_tuple = (‘lilei‘,‘hmm‘,‘jack‘)
host[name_tuple] = ‘admin‘ #不報錯,元組不可變
host
{‘ip_port‘: (‘192.168.1.1‘, 8001),
‘192.168.1.2‘: ‘adminpc‘,
(‘lilei‘, ‘hmm‘, ‘jack‘): ‘admin‘}
set集合,不重復的元素
set集合創建
numbers = {1,2,3,1,3,4,5,6}
type(numbers)
set
numbers
{1, 2, 3, 4, 5, 6}
set集合查看
numbers[0] ##不支持
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-81-7486ea9d37bc> in <module>()
----> 1 numbers[0]
TypeError: ‘set‘ object does not support indexing
for i in numbers:
print(i)
1
2
3
4
5
6
#列表
others = [2,3,4,6,7,7]
set(others) #列表轉換成set集合
{2, 3, 4, 6, 7}
len(others) #others還是列表
6
type(others)
list
len(set(others))
5
python中字典,元組,集合