簡體   English   中英

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

[英]Python list comprehension for dictionaries in dictionaries?

我剛剛了解了列表推導式,這是一種在一行代碼中快速獲取數據的好方法。 但有些事情困擾着我。

在我的測試中,我在列表中有這樣的字典:

[{'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'] = ...

但是列表推導式對此不起作用! 而且我無法以這種方式編輯列表,因為您無法分配這樣的索引。

還有其他方法嗎?

你可以這樣做:

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 ])

這需要一個你指定的字典並返回一個“過濾”的字典。

如果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

還要注意,上面我寫了一個列表理解,所以你會得到一個列表(在這種情況下,是字典)。

在 Python3 中,還有諸如dict推導式之類的東西:

{ 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) )

我不確定您是在尋找字典列表還是字典字典,但是如果您了解上面的示例,很容易修改我的答案以獲得您想要的。

聽起來你想要這樣的東西:

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() 構造函數采用可迭代的鍵/值元組來創建字典

您可以使用d.values()獲取字典 d 的值列表。 你的列表理解應該使用它,盡管我有點不清楚你到底想要什么輸出。

還有其他方法嗎?

為什么不考慮使用一些輕量級的對象呢?

仍然可以使用列表推導式來收集或過濾對象,並在清晰度/可擴展性方面獲得很多好處。

>>> 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

在 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}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM