简体   繁体   English

通过python中的键迭代字典多个值

[英]iterate dictionary multiple values by keys in python

I want to find most optimal way to iterate values in key in python. 我想找到最优化的方法来迭代python中键的值。

I have file with that structure: 我有这种结构的文件:

17 key1 17键1

18 key1 18键1

45 key2 45键2

78 key2 78键2

87 key2 87键2

900 key3 900键3

92 key4 92键4

so I need to set the second column as key(with no repetition) and link to this key all the values (first column) corresponding to it. 所以我需要将第二列设置为键(无重复),并将与之对应的所有值(第一列)链接到此键。

'key1':['17','18'] 'key1':['17','18']

'key2':['45','78','87'] 'key2':['45','78','87']

'key3':['900'] 'key3':['900']

'key4':['92'] 'key4':['92']

Up to now I do it without using the dictionary: 到现在为止,我还没有使用字典:

for line in file:

           value, key = line.strip().split(None,1)

And then I can put it into the dictionary with 然后我可以将其放入字典中

 diction.setdefault(key, []).append(value)

so after that I have a nice dictionary as I needed. 所以之后,我根据需要准备了一本不错的字典。

But after that I have to reread file for changes. 但是之后,我必须重新读取文件以进行更改。 changes can occur in keys(pairs) (adding/removing) or only in value (adding/removing) How can I check if change occured by iteration keys by values? 更改可以在键(对)(添加/删除)中发生,也可以仅在值(添加/删除)中发生,如何检查迭代键是否按值发生了更改?

UPD***: for keys check is more or less clear: UPD ***:对于按键检查或多或少是很清楚的:

if diction[key]:

but how to iterate values inside the key? 但是如何迭代键中的值? I need to find the difference, and then add\\remove this value\\pair(if last value of the key) from dictionary? 我需要找到差异,然后从字典中添加\\删除该值\\对(如果键的最后一个值)?

I suppose it can be done with some iteritem()\\itervalues() or smthng but I m not familiar with that. 我想可以用一些iteritem()\\ itervalues()或smthng完成,但我对此并不熟悉。

Thank you for help. 谢谢你的帮助。

UPD*** UPD ***

Thank you @Joël. 谢谢@Joël。 Finally I used 3 checks. 最后我用了3张支票。 first is any keys added: 首先是添加的任何键:

set_old_dict = set(new_old.keys())
set_new_dict = set(new_dict.keys()) 
intersect = set_new_dict.intersection(set_old_dict)



def added(self):
    return set_new_dict - intersect 
  def removed(self):
    return set_old_dict - intersect

And then if I do not catch or have already processed this situations I will use your function: 然后,如果我没有发现或未处理这种情况,我将使用您的函数:

 def comp(old_dict, new_dict):
     for key, old_val in old_dict.items():
         new_val = new_dict[key]  
        print 'evolutions for', key
         print 'new content:', [x for x in new_val if x not in old_val]
         print 'removed content:', [x for x in old_val if x not in new_val]

My advice is that, if you have to re-read the input file, you may as well re-create your dictionary, but that depends on the time needed for dictionary creation. 我的建议是,如果您必须重新读取输入文件,也可以重新创建字典,但这取决于创建字典所需的时间。 As you request, maybe it's quicker to analyze differences in file, and to update the dictionary. 根据您的要求,也许可以更快地分析文件中的差异并更新字典。

You can have a look at the difflib module, and then to analyze the differences. 您可以查看difflib模块,然后分析差异。 Based on this, removals can be deleted in dictionary, addition added as necessary. 基于此,可以在字典中删除删除内容,并根据需要添加其他内容。

Sadly, I bet you'll have a hard time with its output: this is meant to be human-readable, not machine-readable, so there may be a better answer. 不幸的是,我敢打赌,您的输出将很困难:这是人类可读的,而不是机器可读的,因此可能会有更好的答案。


EDIT if you want to keep track of the changes between two files version, as written in your comment, you can compare the dictionaries. 编辑如果您想保留之间的两个文件版本的变化轨迹,写在你的评论,你可以比较的字典。 For the keys, you already have what is needed. 对于键,您已经有了所需的东西。

Now, for updated values: if you are sure that your values will always be lists of strings, then you can do quite the same thing as for comparing the dict keys: 现在,对于更新的值:如果您确定您的值将始终是字符串列表,那么您可以执行与比较dict键相同的操作:

>>> def comp(old_dict, new_dict):
...     for key, old_val in old_dict.items():
...         new_val = new_dict[key]  # warning: to be used on keys in both dict
...         print 'evolutions for', key
...         print 'new content:', [x for x in new_val if x not in old_val]
...         print 'removed content:', [x for x in old_val if x not in new_val]

# now testing on a simple example
>>> o = {'key1': ['a', 'b', 'c']}
>>> n = {'key1': ['b', 'c', 'd']}
>>> comp(o, n)
evolutions for key1
new content: ['d']
removed content: ['a']

Warning: this function works only if new_dict contains all keys of old_dict , otherwise creation of new_val will fail. 警告:仅当new_dict包含new_dict所有键时,此函数才old_dict ,否则new_val创建将失败。 You can easily go around this concern, by adding the comparisons of keys in the function: 通过在函数中添加键的比较,您可以轻松解决此问题:

  • keys in old_dict that are not in new_dict are removed entries; old_dict中不在new_dict中的键是已删除条目;
  • keys in new_dict and not in old_dict are additions. new_dict键而不是old_dict中的键是添加项。

Please publish your result in your answer, so that others may benefit from it. 请在您的答案中发布您的结果,以便其他人可以从中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM