Python dictionary, tuples, collections

Source: Internet
Author: User

Dictionaries, tuples, collections in Python

-dict

-tuple

-set

Dict dictionary additions and Deletions dictionary creation
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‘
The increase of the dictionary
de8ug[‘age‘] = 18de8ug
{‘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}}
Deletion of dictionaries
del de8ug[‘name‘]de8ug
{‘city‘: ‘beijing‘, ‘code‘: ‘python‘, ‘age‘: 18, ‘student‘: [‘lilei‘, ‘hmm‘, ‘tony‘], ‘book‘: {‘name‘: ‘python3‘, ‘price‘: 100}}
Modification of the dictionary
de8ug[‘age‘] = 20de8ug
{‘city‘: ‘beijing‘, ‘code‘: ‘python‘, ‘age‘: 20, ‘student‘: [‘lilei‘, ‘hmm‘, ‘tony‘], ‘book‘: {‘name‘: ‘python3‘, ‘price‘: 100}}
A variety of dictionary search
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})
Additions and deletions of tuple tuples

Why,what,how

Coordinates (x, y)

Box (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,一个是valuehost
{‘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 to list conversion
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, non-repeating element set collection creation
numbers = {1,2,3,1,3,4,5,6}
type(numbers)
set
numbers
{1, 2, 3, 4, 5, 6}
Set Collection View
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)
123456
#列表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 dictionary, tuples, collections

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.