Loading... ## 字典 ### 使用字典 ``` # alien_0 = {"color": "green", "points": 5} # # print(alien_0["color"]) # print(alien_0["points"]) # # new_points = alien_0["points"] # print(f"You just earned {new_points} points!") # # print(alien_0) # # alien_0["x_position"] = 0 # alien_0["y_position"] = 25 # print(alien_0) alien_0 = {} alien_0["color"] = "green" alien_0["points"] = 5 print(alien_0) alien_0["color"] = "yellow" print(alien_0) alien_0["x_position"] = 0 alien_0["y_position"] = 25 alien_0["speed"] = "medium" print(alien_0) if alien_0["speed"] == "slow": x_increment = 1 elif alien_0["speed"] == "medium": x_increment = 2 elif alien_0["speed"] == "fast": x_increment = 3 alien_0["x_position"] = alien_0["x_position"] + x_increment print(alien_0) del alien_0["points"] ### 注意del print("alien_0") print(alien_0.get("points", "No point value assaigned")) print(alien_0.get("points", 1)) #get函数用法注意 ``` 结果 ``` {'color': 'green', 'points': 5} {'color': 'yellow', 'points': 5} {'color': 'yellow', 'points': 5, 'x_position': 0, 'y_position': 25, 'speed': 'medium'} {'color': 'yellow', 'points': 5, 'x_position': 2, 'y_position': 25, 'speed': 'medium'} alien_0 No point value assaigned 1 ``` ## 嵌套 ### 列表中嵌套字典 **管理一堆外星人** ``` # alien_0 = {"color": "green", "points": 5} # alien_1 = {"color": "yellow", "points": 10} # alien_2 = {"color": "red", "points": 15} # # aliens = [alien_1, alien_2, alien_0] ## 列表中存储字典 aliens = [] for alien_number in range(30): new_alien = {"color": "green", "points": 5, "speed": "slow"} aliens.append(new_alien) for alien in aliens[:3]: if alien["color"] == "green": alien["color"] = "yellow" alien["speed"] = "medium" alien["points"] = 10 for alien in aliens[:5]: print(alien) print("\n") for alien in aliens[:5]: if alien["color"] == "green": alien["color"] = "yellow" alien["speed"] = "medium" alien["points"] = 10 elif alien["color"] == "yellow": alien["color"] = "red" alien["speed"] = "fast" alien["points"] = 15 for alien in aliens[:5]: print(alien) ``` 结果 ``` {'color': 'yellow', 'points': 10, 'speed': 'medium'} {'color': 'yellow', 'points': 10, 'speed': 'medium'} {'color': 'yellow', 'points': 10, 'speed': 'medium'} {'color': 'green', 'points': 5, 'speed': 'slow'} {'color': 'green', 'points': 5, 'speed': 'slow'} {'color': 'red', 'points': 15, 'speed': 'fast'} {'color': 'red', 'points': 15, 'speed': 'fast'} {'color': 'red', 'points': 15, 'speed': 'fast'} {'color': 'yellow', 'points': 10, 'speed': 'medium'} {'color': 'yellow', 'points': 10, 'speed': 'medium'} ``` ### 字典中存储列表 ``` pizza = { "crust": "thick", "toppings": ["mushrooms", "extra cheese"] } print(f"You orderd a {pizza['crust']}-crust pizza " "with the following toopings:") 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(f"\n{name.title()}'s favoite languages are:") for language in languages: print(f"\t{language.title()}") ## 其它遍历方式 ## for value in dict.values(): ## for key in dict.keys(): ``` 结果 ``` You orderd a thick-crust pizza with the following toopings: mushrooms extra cheese Jen's favoite languages are: Python Ruby Sarah's favoite languages are: C Edward's favoite languages are: Ruby Go Phil's favoite languages are: Python Haskell ``` ### 字典中有字典 ``` megami_info = { "NEPTUNE": { "location": "Planeptune", "cute": 90, "handsome": 90, "power": 90, "HDD": True, }, "Uni": { "location": "Lastatoin", "cute": 70, "handsome": 80, "power": 80, "HDD": True, }, "Nepgear": { "location": "Planeptune", "cute": 80, "handsome": 80, "power": 80, "HDD": True, }, "IF": { "location": "Planeptune", "cute": 70, "handsome": 70, "power": 70, "HDD": False, }, "Compa": { "location": "Planeptune", "cute": 90, "handsome": 70, "power": 70, ##假装忘记填了 }, "Rom": { "location": "Lowee", "cute": 100, "handsome": 70, "power": 70, "HDD": True, }, "Ram": { "location": "Lowee", "cute": 100, "handsome": 70, "power": 70, "HDD": True, } } locations = [] for megami, info in megami_info.items(): ## 注意megami的属性是字符串,而info才是列表 cute, handsome, power = info["cute"], info["handsome"], info["power"] HDD_Mode = info.get("HDD", False) location = info["location"] locations.append(location) print(f"{megami} from {location}") print(f"\tHer score is cute: {cute}, handsome: {handsome}, power: {power}") print("They are from") for place in set(locations): ## set可以找出列表中的元素(不重复),并创建在集合中,集合也可以手动创建 ## languages = {"python", "ruby", "python", "c"} print(f"\t{place}") ``` 结果 ``` NEPTUNE from Planeptune Her score is cute: 90, handsome: 90, power: 90 Uni from Lastatoin Her score is cute: 70, handsome: 80, power: 80 Nepgear from Planeptune Her score is cute: 80, handsome: 80, power: 80 IF from Planeptune Her score is cute: 70, handsome: 70, power: 70 Compa from Planeptune Her score is cute: 90, handsome: 70, power: 70 Rom from Lowee Her score is cute: 100, handsome: 70, power: 70 Ram from Lowee Her score is cute: 100, handsome: 70, power: 70 They are from Planeptune Lowee Lastatoin ``` Last modification:January 4, 2023 © Allow specification reprint Like 如果觉得我的文章对你有用,请留下评论。