标签:添加 图片 集合 value 对象 man pytho users values
字典是一种可变容器模型,且可存储任意类型对象,字典的每个键值 key=>value
对用冒号:
分割,每个键值对之间用逗号 ,
分割,整个字典包括在花括号{}
中 ,如:
object = {
‘color‘: ‘yellow‘,
‘size‘: ‘big‘
}
print(object[‘color‘])
print(object[‘size‘])
object[‘age‘] = 18
object[‘sex‘] = ‘man‘
print(object)
object = {}
object[‘color‘] = ‘yellow‘
object[‘size‘] = ‘big‘
print(object)
object[‘color‘] = ‘black‘
print(object)
object = {‘color‘: ‘black‘, ‘age‘: 18}
del object[‘color‘]
print(object)
user = {
‘username‘: ‘coisini‘,
‘age‘: ‘18‘,
‘sex‘: ‘man‘
}
for key,value in user.items():
print(‘key:‘ + key)
print(‘value:‘ + value)
use_languages = {
‘one_user‘: ‘python‘,
‘two_user‘: ‘c‘,
‘three_user‘: ‘ruby‘,
‘four_user‘: ‘python‘
}
for key in use_languages.keys():
print(‘key:‘ + key)
print(‘value:‘ + use_languages[key])
if key == ‘one_user‘:
print(‘one_user is in use_languages‘)
if ‘four_user‘ in use_languages:
print(‘four_user is in use_languages‘)
for name in sorted(use_languages.keys()):
print(name.title())
for language in use_languages.values():
print(language.title())
for language in set(use_languages.values()):
print(language.title())
one_user = {‘color‘: ‘black‘, ‘age‘: 5}
two_user = {‘color‘: ‘white‘, ‘age‘: 10}
three_user = {‘color‘: ‘yellow‘, ‘age‘: 15}
users = [one_user, two_user, three_user]
for user in users:
print(user)
user = {
‘username‘: ‘coisini‘,
‘languages‘: [‘Java‘, ‘Python‘]
}
for language in user[‘languages‘]:
print(‘\t‘ + language)
users = {
‘coisini‘: {
‘name‘: ‘coisini‘,
‘age‘: 18,
‘location‘: ‘China‘
},
‘maggieq‘: {
‘name‘: ‘maggieq‘,
‘age‘: ‘22‘,
‘location‘: ‘Canada‘
}
}
for username, userinfo in users.items():
print(‘\nusername:‘ + username)
for key, val in users[username].items():
print(‘\tkey:‘ + str(key))
print(‘\tval:‘ + str(val))
标签:添加 图片 集合 value 对象 man pytho users values
原文地址:https://www.cnblogs.com/maggieq8324/p/14255578.html