假设我有大量不同颜色的水果,例如,24个蓝色的香蕉,12个绿色的苹果,0个蓝色的草莓等等。 我想用 Python 中的数据结构来组织它们,以便于选择和排序。我的想法是把它们放到一个字典中,元组作为键,例如,
{
('banana', 'blue' ): 24,
('apple', 'green'): 12,
('strawberry','blue' ): 0,
# ...
}
甚至字典,例如:
{
{'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?
将元组作为键的字典很可能不是处理这种情况的 适当的方法。
欢迎大家提出建议!