多字典迭代python

时间:2016-03-23 00:56:32

标签: python python-2.7 dictionary

我有

格式的字典(d)
[
{'green': 'banana', 'yellow' : 'apple', 'red' : 'cake', 'blue': 
  [ 'id:xyz:abc:def::12128:htm/example/example2/example | number: 289038 | time: 200 hours'],
'computer': 'lorem ispsum', 'COF': 19},


{'green': 'banana', 'purple' : 'plum', 'gray' : 'screen', 'white': 
  [ 'id:xya:abo:dee::121548:htm/examplez/example26/examplelp | number: 289034 | time: 900 hours'],
'box': 'lorem ispsums', 'COF': 91},


{'green': 'monkey', 'orange' : 'oranges', 'clear' : 'candy', 'chevy': 
  [ 'id:pyz:kbc:qef::12178:htm/example8/example19/exampley | number: 289738 | time: 3 hours'],
'play': 'lorems ispsum', 'COF': 119},

我试图遍历整个字典,如果键和值匹配打印字典的其余部分,但我得到了

"List indicies must be integers, not str"

当我使用

(item for item in d if item["green"] == "banana").next()

1 个答案:

答案 0 :(得分:0)

generator_item = (item for item in d if isinstance(d, dict) and item["green"] == "banana")
generator_item.next()

在构建生成器之后调用.next()并且不将其保存到特定值没有任何用处