[英]How do I do this with Python list? (itemgetter?)
[{'id':44}, {'name':'alexa'},{'color':'blue'}]
I want to select whatever in the list that is "id". 我想在列表中选择“id”中的任何内容。 Basically, I want to print 44, since that's "id" in the list.
基本上,我想打印44,因为这是列表中的“id”。
That's a weird data structure... A list of one item dictionaries. 这是一个奇怪的数据结构......一个项目词典的列表。
key = 'id'
l = [{'id':44}, {'name':'alexa'},{'color':'blue'}]
print [ x[key] for x in l if key in x ][0]
Assuming you can rely on key being present precisely once... 假设您可以依靠关键存在一次......
Maybe you should just convert the list into a dictionary first: 也许您应该首先将列表转换为字典:
key = 'id'
l = [{'id':44}, {'name':'alexa'},{'color':'blue'}]
d = {}
for x in l:
d.update(x)
print d[key]
All the other answers solve your problem, I am just suggesting an alternative way of going about doing this. 所有其他答案解决了你的问题,我只是建议另一种方法来做这件事。
Instead of having a list
of dict
s where you query on the key and have to iterate over all list
items to get values, just use a dict
of list
s. 取代具有的
list
的dict
是你要重的关键查询和必须遍历所有list
项得到的值,只需使用一个dict
的list
秒。 Each key would map to a list
of values (or just one value if all your dict
s had distinct sets of keys). 每个键都会映射到一个值
list
(如果你的所有dict
都有不同的键集,则只有一个值)。
So, 所以,
data=[{'id':44}, {'name':'alexa'},{'color':'blue'}]
becomes 变
data={'id':[44], 'name':['alexa'], 'color':['blue']}
and you can neatly access the value for 'id'
using data['id']
(or data['id'][0]
if you only need one value). 如果你只需要一个值,你可以使用
data['id']
(或data['id'][0]
整齐地访问'id'
值。
If all your keys are distinct across the dict
s (as in your example) you don't even have to have list
s of values. 如果您的所有键在
dict
都是不同的(如您的示例中所示),您甚至不必拥有值list
。
data={'id':44, 'name':'alexa', 'color':'blue'}
Not only does this make your code cleaner, it also speeds up your queries which no longer have to iterate over a list
. 这不仅可以使您的代码更清晰,而且还可以加快您的查询速度,而这些查询不再需要遍历
list
。
You could do something like this: 你可以这样做:
>>> KEY = 'id'
>>>
>>> my_list = [{'id':44}, {'name':'alexa'},{'color':'blue'}]
>>> my_ids = [x[KEY] for x in my_list if KEY in x]
>>> print my_ids
[44]
Which is obviously a list of the values you want. 这显然是您想要的值列表。 You can then print them as required.
然后,您可以根据需要打印它们。
Probably this is the best solution: 可能这是最好的解决方案:
>>> L = [{'id':44}, {'name':'alexa'},{'color':'blue'}]
>>> newd = {}
>>> for d in L:
... newd.update(d)
>>> newd['id']
44
>>> from itertools import dropwhile
>>> def find_value(l, key):
... return dropwhile(lambda x: key not in x, l).next()[key]
>>> find_value([{'id':44}, {'name':'alexa'},{'color':'blue'}], "id")
This will do a linear search, but only until the element is found. 这将进行线性搜索,但只能在找到元素之前进行。
If you want to have proper error handling, use: 如果要进行适当的错误处理,请使用:
def find_value(l, key):
try:
return dropwhile(lambda x: key not in x, l).next()[key]
except StopIteration:
raise ValueError(key)
>>> L = [{'id':44}, {'name':'alexa'},{'color':'blue'}]
>>> newd=dict(d.items()[0] for d in L)
>>> newd['id']
44
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.