Python列表理解字典中的字典?
我刚刚了解了列表理解,这是在一行代码中获取数据的快速方法。但有件事困扰着我。
在我的测试中,我的列表中有这种字典:
[{'y': 72, 'x': 94, 'fname': 'test1420'}, {'y': 72, 'x': 94, 'fname': 'test277'}]
列表理解 s = [ r for r in list if r['x'] > 92 且 r['x'] < 95、r['y']>; 70 且 r['y'] < 75]
完美地解决了这个问题(事实上,这是这一行的结果)
无论如何,我后来意识到我并没有真正在我的其他项目中使用列表,我正在使用字典。像这样:
{'test1420': {'y': '060', 'x': '070', 'fname': 'test1420'}}
这样我就可以简单地使用 var['test1420'] = ...
编辑我的字典,
但是列表推导式对此不起作用! 我无法以这种方式编辑列表,因为您无法分配这样的索引。
还有别的办法吗?
I just learned about list comprehension, which is a great fast way to get data in a single line of code. But something's bugging me.
In my test I have this kind of dictionaries inside the list:
[{'y': 72, 'x': 94, 'fname': 'test1420'}, {'y': 72, 'x': 94, 'fname': 'test277'}]
The list comprehension s = [ r for r in list if r['x'] > 92 and r['x'] < 95 and r['y'] > 70 and r['y'] < 75 ]
works perfectly on that (it is, in fact, the result of this line)
Anyway, I then realised I'm not really using a list in my other project, I'm using a dictionary. Like so:
{'test1420': {'y': '060', 'x': '070', 'fname': 'test1420'}}
That way I can simply edit my dictionary with var['test1420'] = ...
But list comprehensions don't work on that!
And I can't edit lists this way because you can't assign an index like that.
Is there another way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以这样做:
这需要您指定的字典并返回“过滤”字典。
You can do this:
This takes a dict as you specified and returns a 'filtered' dict.
如果 dct 是
也许你正在寻找类似的东西:
一点好处是 Python 允许你链接不等式:
而不是
注意上面我已经写了一个列表理解,所以你会得到一个列表(在这种情况下,是字典)。
在 Python3 中,还有诸如 dict 推导式之类的东西:
在 Python2 中,相当于
I我不确定您是否正在寻找字典列表或字典的字典,但如果您理解上面的示例,则很容易修改我的答案以获得您想要的内容。
If
dct
isPerhaps you are looking for something like:
A little nicety is that Python allows you to chain inequalities:
instead of
Note also that above I've written a list comprehension, so you get back a list (in this case, of dicts).
In Python3 there are such things as dict comprehensions as well:
In Python2 the equivalent would be
I'm not sure if you are looking for a list of dicts or a dict of dicts, but if you understand the examples above, it is easy to modify my answer to get what you want.
听起来你想要类似的东西:
关于此代码的一些注释:
而不是列表推导式
测试为
low <值< high
键/值元组来创建
字典
Sounds like you want something like:
Some notes on this code:
instead of a list comprehension
tests as
low < value < high
of key/value tuples to create a
dictionary
在 Python 3 中,您可以使用字典理解,这可能是一个更短的解决方案:
如果您想像原始问题一样过滤字典:
或者我们甚至可以在列表或范围之外创建一些内容。例如,如果我们想要一个包含所有奇数平方数的字典:
In Python 3 you can use dict comprehension which can be an even shorter solution:
In case you want to filter a dictionary as in the original question:
Or we can even create something out of a list or range. E.g. if we want a dictionary with all odd square numbers:
您可以使用 d.values() 获取字典 d 的值列表。你的列表理解应该使用它来工作,尽管我有点不清楚你到底想要输出是什么。
You can get a list of the values of a dictionary d with
d.values()
. Your list comprehension should work using that, although I'm a little unclear what exactly you want the output to be.还有别的办法吗?
为什么不考虑使用一些轻量级的对象呢?
您仍然可以使用列表推导式来收集或过滤对象,并在清晰度/可扩展性方面获得很大的提升。
Is there another way?
Why not consider the use of some lightweight objects?
You can still use list comprehensions for gathering or filtering the objects, and gain a lot in clarity / extensibility.