Python:元组/字典作为键,选择,排序 [英] Python: Tuples/dictionaries as keys, select, sort
问题描述
我想在Python中的一个数据结构中组织它们,可以方便地进行选择和排序。我的想法是把它们放入一个字典中,以元组作为键,例如
{('banana','blue') :24,
('apple','green'):12,
('strawberry','blue'):0,
...
}
甚至是字典,例如
:$ {$'$'$'$'''''''' ,
{'fruit':'strawberry','color':'blue'}:0,
...
}
pre>
我想要检索所有蓝色水果或所有颜色的香蕉的列表,或者按照水果的名称对该字典进行排序。有没有办法以干净的方式这样做?
很可能,使用元组作为键的字典不是正确的方式来处理这种情况。
所有建议欢迎!
解决方案我喜欢python的东西是元组合的组合。你在这里实际上是一个2d数组(其中x =水果名称和y =颜色),我一般是支持实现二维数组的元组的dict,至少当像
numpy
或数据库不太合适。所以简而言之,我认为你有一个很好的方法。
请注意,你不能在dict中使用diction作为key,而不需要做一些额外的工作,所以不是很好的解决方案。
也就是说,你也应该考虑 namedtuple()。这样你可以这样做:
>>>从集合导入namedtuple
>>> Fruit = namedtuple(Fruit,[name,color])
>>> f = Fruit(name =banana,color =red)
>>>打印f
水果(name ='banana',color ='red')
>>> f.name
'banana'
>>> f.color
'red'
现在你可以使用你的fruitcount dict:
>>> fruitcount = {Fruit(banana,red):5}
>>> fruitcount [f]
5
其他技巧:
>>> fruits = fruitcount.keys()
>>> fruits.sort()
>>>打印水果
[Fruit(name ='apple',color ='green'),
Fruit(name ='apple',color ='red'),
Fruit(name = Fruit',color ='blue'),
Fruit(name ='strawberry',color ='blue')]
>>> fruits.sort(key = lambda x:x.color)
>>>>打印水果
[Fruit(name ='banana',color ='blue'),
Fruit(name ='strawberry',color ='blue'),
Fruit(name =苹果',color ='green'),
Fruit(name ='apple',color ='red')]
回应chmullig,要获得一个水果的所有颜色的列表,您必须过滤键,即
code> bananas = [水果果实果实如果fruit.name =='banana']
suppose I have quantities of fruits of different colors, e.g., 24 blue bananas, 12 green apples, 0 blue strawberries and so on. I'd like to organize them in a data structure in Python that allows for easy selection and sorting. My idea was to put them into a dictionary with tuples as keys, e.g.,
{ ('banana', 'blue' ): 24, ('apple', 'green'): 12, ('strawberry','blue' ): 0, ... }
or even dictionaries, e.g.,
{ {'fruit': 'banana', 'color': 'blue' }: 24, {'fruit': 'apple', 'color': 'green'}: 12, {'fruit': 'strawberry','color': 'blue' }: 0, ... }
I'd like to retrieve a list of all blue fruit, or bananas of all colors, for example, or to sort this dictionary by the name of the fruit. Are there ways to do this in a clean way?
It might well be that dictionaries with tuples as keys are not the proper way to handle this situation.
All suggestions welcome!
解决方案Personally, one of the things I love about python is the tuple-dict combination. What you have here is effectively a 2d array (where x = fruit name and y = color), and I am generally a supporter of the dict of tuples for implementing 2d arrays, at least when something like
numpy
or a database isn't more appropriate. So in short, I think you've got a good approach.Note that you can't use dicts as keys in a dict without doing some extra work, so that's not a very good solution.
That said, you should also consider namedtuple(). That way you could do this:
>>> from collections import namedtuple >>> Fruit = namedtuple("Fruit", ["name", "color"]) >>> f = Fruit(name="banana", color="red") >>> print f Fruit(name='banana', color='red') >>> f.name 'banana' >>> f.color 'red'
Now you can use your fruitcount dict:
>>> fruitcount = {Fruit("banana", "red"):5} >>> fruitcount[f] 5
Other tricks:
>>> fruits = fruitcount.keys() >>> fruits.sort() >>> print fruits [Fruit(name='apple', color='green'), Fruit(name='apple', color='red'), Fruit(name='banana', color='blue'), Fruit(name='strawberry', color='blue')] >>> fruits.sort(key=lambda x:x.color) >>> print fruits [Fruit(name='banana', color='blue'), Fruit(name='strawberry', color='blue'), Fruit(name='apple', color='green'), Fruit(name='apple', color='red')]
Echoing chmullig, to get a list of all colors of one fruit, you would have to filter the keys, i.e.
bananas = [fruit for fruit in fruits if fruit.name=='banana']
这篇关于Python:元组/字典作为键,选择,排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!