python basis - Type a brief dictionary

Dictionary (dict)

effect

A plurality of values ​​used to access, in accordance with key: value of the stored-value mode, may not take the time to go to the index value by the key, key has a function of descriptive value. Store a variety of types of data and more data when you can use a dictionary.


definition

{} In the plurality of elements separated by commas, each element is key: value format, the format in which the value is an arbitrary data type, since the key has a function descriptive, the key is usually a string type. key values ​​can only immutable data type.

user_info = {'name': 'nash', 'gender': 'male', 'age': 18,
             'company_info': ['alibaba', 'hangzhou', 'xixiyuanqu']}

print(id(user_info))
print(type(user_info))
print(user_info)

# 输出结果
# 4396183344
# <class 'dict'>
# {'name': 'nash', 'gender': 'male', 'age': 18, 'company_info': ['alibaba', 'hangzhou', 'xixiyuanqu']}


use

Dictionary index value no longer depends on the way, but rather on the key, to obtain the value corresponding to the key value through the [key].

Dictionary set list -> list of note values ​​are associated with index

user_info = {'name': 'nick', 'gender': 'male', 'age': 18,
             'company_info': ['alibaba', 'hangzhou', 'alibaba']}
print(user_info['name'])
print(user_info['company_info'][0])
# 输出结果
# nash
# alibaba

Dictionary cover Dictionary -> Dictionary values ​​are associated with attention key

user_info = {'name': 'nash', 'gender': 'male', 'age': 19, 'company_info': {
    'c_name': 'alibaba', 'c_addr': 'shanghai', 'c_num_of_employee': 'alibaba'}}

print(user_info['name'])
print(user_info['company_info']['c_name'])
# 输出结果
# nash
# alibaba

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374630.html