通过字典集

编程入门 行业动态 更新时间:2024-10-28 14:37:17
通过字典集 - 迭代 - 基本搜索迭代(Iterate through set of dictionaries - python - basic search)

我发生了一件非常奇怪的事情。 我试图遍历一组字典,以查找具有与字段键相关的特定值的所有项目。 请采取以下措施:

ex_set是我无法控制的mysql系统的输出。 如果我不得不使用python重新创建它,它将是这样的:

dict_a [ 'field' ] = 'fruit' dict_a [ 'value' ] = 'apple' dict_b [ 'field' ] = 'fruit' dict_b [ 'value' ] = 'berry' ex_set = set() ex_set.add (dict_a,dict_b)

重要的是该组在我打印时的外观。

pprint (ex_set) OUTPUTS> ({'field' : 'fruit', 'value' : 'apple'}, 'field' : 'fruit'}, 'value' : 'berry'}) i = 0 while len ( ex_set ) > i: for k , v in ex_set [i].iteritems ( ): if v == 'fruit': pprint ( ex_set[i] ) i += 1

问题是打印这个并不打印所有具有值=“水果”的字典。

有没有更好的方法来搜索一组词典? 我正在搜索的集合在每个字典中有3个键/值组合和大约30k字典。 这大约占25%的时间,我无法弄清楚为什么它只返回大约20%的比赛。

谢谢您的帮助!

I have a really weird thing happening. I am trying to loop through a set of dictionaries to find all of the items with a specific value related to a field key. Take the following:

ex_set is an output from a mysql system that I do not have control over. If I had to re-create it using python, it would be something like:

dict_a [ 'field' ] = 'fruit' dict_a [ 'value' ] = 'apple' dict_b [ 'field' ] = 'fruit' dict_b [ 'value' ] = 'berry' ex_set = set() ex_set.add (dict_a,dict_b)

The important thing is how the set looks when I pprint it.

pprint (ex_set) OUTPUTS> ({'field' : 'fruit', 'value' : 'apple'}, 'field' : 'fruit'}, 'value' : 'berry'}) i = 0 while len ( ex_set ) > i: for k , v in ex_set [i].iteritems ( ): if v == 'fruit': pprint ( ex_set[i] ) i += 1

The problem is that the printing of this does not print all of the dictionaries that have a value = "fruit".

Is there a better way to search through a set of dictionaries? The set that I am searching through has 3 key/value combinations in each dictionary and about 30k dictionaries. This works about 25% of the time and I can't figure out why it is only returning about 20% of the matches.

Thanks for the help!

最满意答案

根据您的描述,您正在寻找以下内容:

In [6]: ex_set = ({'fruit':'apple', ...: 'value':'fruit'}, ...: {'fruit':'fruit'}, ...: {'apple':'apple'}) In [7]: for d in ex_set: ...: if 'fruit' in d.values(): ...: print(d) ...: {'fruit': 'apple', 'value': 'fruit'} {'fruit': 'fruit'}

另外,除了事实,你的例子不是有效的python,ex_set当然不能是一个set ,因为sets's不能包含dictionaries因为它们是不可用的。 我会考虑将其重命名为更合适的东西:

In [8]: set([{}]) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-8-7facc835553f> in <module>() ----> 1 set([{}]) TypeError: unhashable type: 'dict'

Based on your description you are seeking something like:

In [6]: ex_set = ({'fruit':'apple', ...: 'value':'fruit'}, ...: {'fruit':'fruit'}, ...: {'apple':'apple'}) In [7]: for d in ex_set: ...: if 'fruit' in d.values(): ...: print(d) ...: {'fruit': 'apple', 'value': 'fruit'} {'fruit': 'fruit'}

Also, in addition to the fact, that your example isn't valid python, ex_set certainly can't be a set, as sets's cant contain dictionaries as they are unhashable. I would consider renaming it to something more suitable:

In [8]: set([{}]) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-8-7facc835553f> in <module>() ----> 1 set([{}]) TypeError: unhashable type: 'dict'

更多推荐

本文发布于:2023-07-31 10:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1343814.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!