字典中字典的Python列表理解?

时间:2010-07-12 18:46:00

标签: python list dictionary list-comprehension

我刚刚学习了列表理解,这是一种在单行代码中获取数据的快速方法。但有些事情让我烦恼。

在我的测试中,我在列表中有这样的词典:

[{'y': 72, 'x': 94, 'fname': 'test1420'}, {'y': 72, 'x': 94, 'fname': 'test277'}]

列表理解s = [ r for r in list if r['x'] > 92 and r['x'] < 95 and r['y'] > 70 and r['y'] < 75 ]完全适用于此(事实上,这是该行的结果)

无论如何,我意识到我并没有在我的其他项目中使用列表,我正在使用字典。像这样:

{'test1420': {'y': '060', 'x': '070', 'fname': 'test1420'}}

这样我就可以使用var['test1420'] = ...

简单地编辑我的字典

但是列表推导不起作用! 我无法以这种方式编辑列表,因为您无法分配这样的索引。

还有其他办法吗?

6 个答案:

答案 0 :(得分:36)

你可以这样做:

s = dict([ (k,r) for k,r in mydict.iteritems() if r['x'] > 92 and r['x'] < 95 and r['y'] > 70 and r['y'] < 75 ])

这会按照你指定的方式获取一个dict并返回一个'过滤的'dict。

答案 1 :(得分:9)

如果dct

{'test1420': {'y': '060', 'x': '070', 'fname': 'test1420'},
 'test277': {'y': 72, 'x': 94, 'fname': 'test277'},}

也许您正在寻找类似的东西:

[ subdct for key,subdct in dct.iteritems() 
  if 92<subdct['x']<95 and 70<subdct['y']<75 ]

有点精确的是Python允许你链接不平等:

92<dct[key]['x']<95

而不是

if r['x'] > 92 and r['x'] < 95

另请注意,上面我写了一个列表理解,所以你得到一个列表(在这种情况下,是dicts)。

在Python3中还有dict comprehensions之类的东西:

{ n: n*n for n in range(5) } # dict comprehension
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

在Python2中,等价物是

dict( (n,n*n) for n in range(5) )

我不确定你是否在寻找一个词典或词典的列表,但如果你理解上面的例子,很容易修改我的答案以获得你想要的东西。

答案 2 :(得分:2)

听起来像你想要的东西:

my_dict = {'test1420': {'y': '060', 'x': '070', 'fname': 'test1420'},
           'test277' : {'y': '072', 'x': '094', 'fname': 'test277'}}


new_dict = dict((k,v) for k,v in my_dict.items() 
                    if 92 < int(v['x']) < 95 and 70 < int(v['y']) < 75)

关于此代码的一些注意事项:

  1. 我正在使用生成器表达式 而不是列表理解
  2. Python可以让你结合不平等 测试为low < value < high
  3. dict()构造函数采用iterable 用于创建的键/值元组 字典

答案 3 :(得分:1)

在 Python 3 中,您可以使用 dict comprehension,这是一个更短的解决方案:

{key_expression(item) : value_expression(item) for item in something if condition}
  1. 如果您想像原始问题一样过滤字典:

    mydict = {'test1': {'y':  60},'test2': {'y':  70},'test3': {'y':  80}}
    s = {k : r for k,r in mydict.items() if r['y'] < 75 }
    > {'test1': {'y': 60}, 'test2': {'y': 70}}
    
  2. 或者我们甚至可以在列表或范围之外创建一些东西。例如。如果我们想要一个包含所有奇数平方数的字典:

    {i : i**2 for i in range(11) if i % 2 == 1}
    > {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
    

答案 4 :(得分:0)

您可以使用d.values()获取字典d的值列表。你的列表理解应该可以使用它,虽然我有点不清楚你想要输出到底是什么。

答案 5 :(得分:0)

还有其他办法吗?

为什么不考虑使用一些轻量级对象?

仍然可以使用列表推导来收集或过滤对象,并在清晰度/可扩展性方面获得很多收益。

>>> class Item(object):
...     def __init__(self, x, y, name):
...         self.x = x
...         self.y = y
...         self.name = name
... 
>>> list_items = []
>>> list_items.append(Item(x=70, y=60, name='test1420'))                        
>>> list_items.append(Item(x=94, y=72, name='test277'))                         
>>> items_matching = [item for item in list_items 
                      if 92 < item.x < 95 and 70 < item.y < 75]
>>> for item in items_matching:
...     print item.name
... 
test277
>>> first_item = items_matching[0]
>>> first_item.x += 50
>>> first_item.x
144