我发生了一件非常奇怪的事情。 我试图遍历一组字典,以查找具有与字段键相关的特定值的所有项目。 请采取以下措施:
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 += 1The 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'更多推荐
发布评论