[英]How can I analyze list values in a python dictionary, add extra dictionary values showing the different keys and the list value count in common?
I have a larger dictionary in the form as shown below. 我有一个较大的字典,形式如下所示。 I am trying to find similarities between keys and the values which are in list format.
我试图找到键和列表格式的值之间的相似性。
data_dict = {623823: ['draintheswamp', 'swimming'], 856273: ['elect2015'], 8236472: [], 623526: ['yearmatters'], 72645: ['elect2015'], 723641: ['draintheswamp'], 712641: ['swimming'], 917265: ['elect2015', 'draintheswamp']}
I want to output two (extra dictionary values) that show the key to which each key is related to if it finds a similarity or null and the number of similar values in that list. 我想输出两个(额外的字典值),以显示每个键与之相关的键(如果找到相似性或null以及该列表中相似值的数量)。
Columns in the dictionary values would be (key, [text_used], [related_key, number_of_related_texts]) 字典值中的列为(关键字,[已使用的文本],[相关关键字,与相关文本的数目])
Brief example on the look of the new dictionary result : 有关新字典结果外观的简短示例:
new_dict = {623823: (['draintheswamp', 'swimming'], [(723641, 1), (712641, 1)]), 856273: (['elect2015'], [(72645, 1), (917265, 1)]), ...}
So I hacked together a quick method for generating the dictionary you requested. 因此,我总结了一种快速的方法来生成您要求的字典。 For brevity, I used the
np.intersect1d
method to quickly count shared items between dict-value lists. 为简便起见,我使用
np.intersect1d
方法快速计算dict值列表之间的共享项。
import numpy as np
new_data = {} #new dict
for key in data_dict.keys():
new_data[key] = () #set empty tuple
x = [] #set empty list x
y = [] #set empty list y
for k, v in data_dict.items():
if key == k: #don't count similarity on same key
pass
else:
shared = np.intersect1d(data_dict[key],v) #all shared items
if shared:
for item in shared:
x.append(item) #add shared item to list x
y.append((k, len(shared))) # add k and number of shared items to list y
new_data[key] = (list(set(x)),y) #update new dict
else:
pass #pass if no shared items found...
If you have any questions that the comments don't answer, please let me know. 如果您有任何意见无法回答的问题,请告诉我。 I hope this helps your project.
希望对您的项目有所帮助。 It's also not optimized, since it's a quick-and-dirty routine to mimic what you were asking for.
它也没有经过优化,因为它是模仿您所要求的快速而肮脏的例程。 Good luck!
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.