English 中文(简体)
我怎么能够用“地图”来改变沙尔的价值观。
原标题:How can I "map" a dict to change only the values in Python
  • 时间:2011-11-24 08:41:58
  •  标签:
  • python

我这样说:

food_to_food_type = {"apple": "fruit", "green bean": "vegetable", "tomato": "controversial"}

我的职能是,在食品类型和价值是我所希望的粮食数量时,需要一条字典,例如:

num_foods_of_type = {"fruit": 1, "vegetable": 1, "controversial": 1}

在此情况下,我想要的是每类粮食的数量相等,我经常这样做。

采用我的<条码>食物_to_food_type的字典,并将其作为我的<条码>num_foods_of_type的字典中的关键,将每一方的数值固定下来,从而形成新的字典吗?

这里我要说的是:

NUM_FOODS_DESIRED = 1
num_foods_of_type = {}
for food_type in food_to_food_type.values():
    num_foods_of_type[food_type] = NUM_FOODS_DESIRED

但是,我希望以职能方式做到这一点,以便我能够把粮食换粮食换粮食换粮食换粮食——转而我的职能:

order_food_types(magic_maplike_function(food_to_food_type.values(), NUM_FOODS_DESIRED))

当然,我可以写上<条码>魔法>,像——功能<>/条码>我,但肯定必须用一种艰难的方式来做到这一点,这是正确的吗?

最佳回答

A wonderful solution was proposed here to count the number of occurence in a python list. This can be applied to your case:

values = food_to_food_type.values()
dict( zip( values, map( values.count, values ) ) )

Apparantly, I haven t read your question sufficiently careful. You meant something like this: (?)

values = food_to_food_type.values()
dict( zip( values, [NUM_FOOD_TYPES]*len(values) ) )
问题回答

你们可以做到这一点:

dict(zip(food_to_food_type.values(), food_to_food_type.values()*[NUM_FOODS_DESIRED]))
>>> { vegetable : 1,  fruit : 1,  controversial : 1}
>>> help(dict.fromkeys)
Help on built-in function fromkeys:

fromkeys(...)
    dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
    v defaults to None.

Thus,

dict.fromkeys(food_to_food_type.values(), NUM_FOODS_DESIRED)




相关问题
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 ]="...

热门标签