python中字典,元组,集合

python中的字典,元组,集合

-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

猜你喜欢

转载自blog.51cto.com/13587169/2123317