English 中文(简体)
使用齐普的字典清单,将增量清单输入功能
原标题:List of dictionaries using zip, passing incremental list names into a function

我撰写了文字冒险游戏,这是我提出的第一个假日方案。 我希望有一份清单,列出可能吃的物品、这些物品的坏之处,以及它们多么坏。 因此,我认为我这样做了:

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忙的时候,任何帮助都值得赞赏。

问题回答

如果你只把它变成一个单一的结构,它就更容易。

foods = [
  [ alcohol ,  alcohol poisoning , 0],
  [ anti-freeze ,  ethylene glycol , 1],
  [ apple seeds ,  cyanogenic glycosides , 0],
  [ apricot seeds ,  cyanogenic glycosides , 0],
  [ avocado ,  persin , 0],
  [ baby food ,  onion powder , 0]
]
badfoods = [dict(zip(keys, food)) for food in foods]

你们接近:

>>> keys = [ Food , Chemical , Imminent death ]
>>> foods = [[ alcohol ,  alcohol poisoning , 0],
             [ anti-freeze ,  ethylene glycol , 1],
             [ apple seeds ,  cyanogenic glycosides , 0]]
>>> [dict(zip(keys, food)) for food in foods]
[{ Food :  alcohol ,  Chemical :  alcohol poisoning ,  Imminent death : 0}, { Food :  anti-freeze ,  Chemical :  ethylene glycol ,  Imminent death : 1}, { Food :  apple seeds ,  Chemical :  cyanogenic glycosides ,  Imminent death : 0}]

我建议采用最佳做法,将数据与代码分开。 仅用最适合您需要的格式将数据储存在另一个档案中。 从你迄今所公布的内容来看,CSV似乎是自然的选择。

# file  badfoods.csv : 

Food,Problem,Imminent death
alcohol,alcohol poisoning,0
anti-freeze,ethylene glycol,1
apple seeds,cyanogenic glycosides,0

在你的主要方案中,只需要两条线:

from csv import DictReader

with open( badfoods.csv ,  r ) as f:
    badfoods = list(DictReader(f))




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签