alien_0 = {'color': 'green', 'points': 5} print(alien_0['color']) print(alien_0['points']) print(alien_0) alien_0['x_position'] = 0 alien_0['y_position'] = 25 print(alien_0) alien_0['color'] = 'yellow' print(alien_0) alien_0 = {'x_position': 0, 'y_position': 25, 'speed': 'medium', "points": 5} print("Original x-position: " + str(alien_0['x_position'])) # 向右移动外星人 # 据外星人当前速度决定将其移动多远 if alien_0['speed'] == 'slow': x_increment = 1 elif alien_0['speed'] == 'medium': x_increment = 2 else: # 这个外星人的速度一定很快 x_increment = 3 # 新位置等于老位置加上增量 alien_0['x_position'] = alien_0['x_position'] + x_increment print("New x-position: " + str(alien_0['x_position'])) # 删除键—值对 print(alien_0) del alien_0["points"] print(alien_0) # 即便遍历字典时,键—值对的返回顺序也与存储顺序不同。 # Python不关心键—值对的存储顺序,而只跟踪键和值之间的关联关系。 favorite_languages = { 'jen': 'python', 'sarah': 'c', 'phil': 'python', 'edward': 'ruby', } # 遍历所有的键—值对 for key, val in favorite_languages.items(): print(key.title() + "'s favorite language is " + val.title() + ".") # 遍历字典中的所有键 for key in favorite_languages.keys(): print(key.title()) # 确定Erin是否接受了调查 not in表示不在 if 'erin' not in favorite_languages.keys(): print("Erin, please take our poll!") print(favorite_languages.keys()) # 遍历字典中的所有值 print("\nThe following languages have been mentioned:") for value in favorite_languages.values(): print(value.title()) # set去重 print("\nThe following languages have been mentioned:") for value in set(favorite_languages.values()): print(value.title()) # 字典列表 aliens = [] for alien_number in range(0, 30): aliens.append({"color": "green", "speed": "slow", "points": 5}) for alien in aliens[0:3]: if alien["color"] == "green": alien["color"] = 'yellow' alien["speed"] = 'medium' alien["points"] = 10 for alien in aliens[0:5]: print(alien) print("...") # 在字典中存储列表 pizza = { 'crust': 'thick', 'toppings': ['mushrooms', 'extra cheese'], } print("You ordered a " + pizza['crust'] + "-crust pizza " + "with the following toppings:") for topping in pizza['toppings']: print("\t" + topping) favorite_languages = { 'jen': ['python', 'ruby'], 'sarah': ['c'], 'edward': ['ruby', 'go'], 'phil': ['python', 'haskell'], } for name, languages in favorite_languages.items(): print("\n" + name.title() + "'s favorite languages are:") for language in languages: print("\t" + language.title()) # 在字典中存储字典 users = { 'aeinstein': { 'first': 'albert', 'last': 'einstein', 'location': 'princeton', }, 'mcurie': { 'first': 'marie', 'last': 'curie', 'location': 'paris', }, } for key, val in users.items(): print('Username:' + key) full_name = val['first'] + " " + val['last'] print('\t' + full_name.title()) print('\tLocation:' + val['location'].title())
alien_0 = {'color': 'green', 'points': 5} print(alien_0['color']) print(alien_0['points']) print(alien_0) alien_0['x_position'] = 0 alien_0['y_position'] = 25 print(alien_0) alien_0['color'] = 'yellow' print(alien_0)
alien_0 = {'x_position': 0, 'y_position': 25, 'speed': 'medium', "points": 5} print("Original x-position: " + str(alien_0['x_position'])) # 向右移动外星人 # 据外星人当前速度决定将其移动多远 if alien_0['speed'] == 'slow': x_increment = 1 elif alien_0['speed'] == 'medium': x_increment = 2 else: # 这个外星人的速度一定很快 x_increment = 3 # 新位置等于老位置加上增量 alien_0['x_position'] = alien_0['x_position'] + x_increment print("New x-position: " + str(alien_0['x_position'])) # 删除键—值对 print(alien_0) del alien_0["points"] print(alien_0) # 即便遍历字典时,键—值对的返回顺序也与存储顺序不同。 # Python不关心键—值对的存储顺序,而只跟踪键和值之间的关联关系。 favorite_languages = { 'jen': 'python', 'sarah': 'c', 'phil': 'python', 'edward': 'ruby', } # 遍历所有的键—值对 for key, val in favorite_languages.items(): print(key.title() + "'s favorite language is " + val.title() + ".") # 遍历字典中的所有键 for key in favorite_languages.keys(): print(key.title())
# 确定Erin是否接受了调查 not in表示不在 if 'erin' not in favorite_languages.keys(): print("Erin, please take our poll!") print(favorite_languages.keys())
# 遍历字典中的所有值 print("\nThe following languages have been mentioned:") for value in favorite_languages.values(): print(value.title()) # set去重 print("\nThe following languages have been mentioned:") for value in set(favorite_languages.values()): print(value.title())
# 字典列表 aliens = [] for alien_number in range(0, 30): aliens.append({"color": "green", "speed": "slow", "points": 5}) for alien in aliens[0:3]: if alien["color"] == "green": alien["color"] = 'yellow' alien["speed"] = 'medium' alien["points"] = 10 for alien in aliens[0:5]: print(alien) print("...")
# 在字典中存储列表 pizza = { 'crust': 'thick', 'toppings': ['mushrooms', 'extra cheese'], } print("You ordered a " + pizza['crust'] + "-crust pizza " + "with the following toppings:") for topping in pizza['toppings']: print("\t" + topping)
favorite_languages = { 'jen': ['python', 'ruby'], 'sarah': ['c'], 'edward': ['ruby', 'go'], 'phil': ['python', 'haskell'], } for name, languages in favorite_languages.items(): print("\n" + name.title() + "'s favorite languages are:") for language in languages: print("\t" + language.title())
# 在字典中存储字典 users = { 'aeinstein': { 'first': 'albert', 'last': 'einstein', 'location': 'princeton', }, 'mcurie': { 'first': 'marie', 'last': 'curie', 'location': 'paris', }, } for key, val in users.items(): print('Username:' + key) full_name = val['first'] + " " + val['last'] print('\t' + full_name.title()) print('\tLocation:' + val['location'].title()) 标签:languages,title,python,favorite,alien,print,position,字典 From: https://www.cnblogs.com/sxww-zyt/p/17094876.html