欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

字典,列表

程序员文章站 2022-04-01 12:25:23
列表生成动态字典keys = [a,b,b]values = [1,2,3]Dictionary = dic(zip(keys,values))print(Dictionary)好玩游戏的物品清单stuff = {"rope":1,"torch":6,"gold coin":42,"dagger":1,"allow":12}def dispalyInventory():print("Inventory:")item_total = 0for k,v in inventory.it...

列表生成动态字典

keys = [a,b,b]
values = [1,2,3]
Dictionary = dic(zip(keys,values))
print(Dictionary)

好玩游戏的物品清单

stuff = {"rope":1,"torch":6,"gold coin":42,"dagger":1,"allow":12}
def dispalyInventory():
	print("Inventory:")
	item_total = 0
	for k,v in inventory.items():
		print(str(v)+" "+K)
		item_total+=v
	print("Total number of items:"+str(item_total))
displayIventory(stuff)

列表到字典的函数

假设一条龙的战利品
dragonLoot = ["gold coin", "dagger", "gold coin","gold coin","ruby"]
def addToinventory(Inventory,addItems):
for k in addItems:
         if k in inventory.keys():
             inventory[k]+=1
         else:
             inventory[k]=1    
return  inventory
inv={'gold coin':42,'rope':1}
dragonLoot=['gold coin','dagger','gold coin','gold coin','ruby']
inv=addToInventory(inv,dragonLoot)
displayInventory(inv)

本文地址:https://blog.csdn.net/m0_48853802/article/details/107879340

相关标签: 列表 python