我撰写了文字冒险游戏,这是我提出的第一个假日方案。 我希望有一份清单,列出可能吃的物品、这些物品的坏之处,以及它们多么坏。 因此,我认为我这样做了:
badfoods = []
keys = [ Food , Problem , Imminent death ]
food1 = [ alcohol , alcohol poisoning , 0]
food2 = [ anti-freeze , ethylene glycol , 1]
food3 = [ apple seeds , cyanogenic glycosides , 0]
badfoods.append(dict(zip(keys,food1)))
badfoods.append(dict(zip(keys,food2)))
badfoods.append(dict(zip(keys,food3)))
我想要包括的食物实际上大约40种。 我知道我也可以这样做:
[{ Food : alcohol , Problem : alcohol poisoning , Imminent death :0},
{ Food : anti-freeze , Problem : ethylene glycol , Imminent death :1}
{ Food : apple seeds, Problem : cyanogenic glycosides , Imminent death :0}] ]
I also read this post on here about using YAML, which is appealing: What is the best way to implement nested dictionaries? but I still do not see how to avoid writing the keys a ton.
此外,我确信,我可以列举我的原始做法,避免撰写约40次,即:
def poplist(listname, keynames, name):
listname.append(dict(zip(keynames,name)))
def main():
badfoods = []
keys = [ Food , Chemical , Imminent death ]
food1 = [ alcohol , alcohol poisoning , 0]
food2 = [ anti-freeze , ethylene glycol , 1]
food3 = [ apple seeds , cyanogenic glycosides , 0]
food4 = [ apricot seeds , cyanogenic glycosides , 0]
food5 = [ avocado , persin , 0]
food6 = [ baby food , onion powder , 0]
for i in range(5):
name = food + str(i+1)
poplist(badfoods, keys, name)
print badfoods
main()
我假定,这是不做的,因为我是 lo,正在形成一种扼杀,然后把它当作一种功能,而流行者的职能却承认它是一种变称。 然而,我不知道是否有办法解决这一问题,或者我是否必须每次使用阿盟或书写钥匙。 在我 st忙的时候,任何帮助都值得赞赏。