如何着手寻找嵌套列表的交集?
How to go about finding the intersection of a nested list?
def query_RR(postings, qtext):
words = tokenize(qtext)
allpostings = [postings[w] for w in words]
for a in allpostings:
print a.keys()
这是查询的结果
[0, 2, 3, 4, 6] [1, 4, 5] [0, 2, 4] [4, 5]
查询采用用户输入词条 (qtext
)、标记化并为每个标记生成帖子列表。
发帖列表是嵌套字典的列表。
例如
[{0 : 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}]
我正在尝试使用键查找这些嵌套字典的交集。
谁能给我一个解决方案,不胜感激!
既然你在评论中阐明了你想要的内容,这里是代码的修订版本,它将公共值收集到列表字典中。正如其他人所建议的那样,诀窍是使用 built-in set
类型来有效地确定键的交集。
自从我最初修改后,我已经能够使用对一个非常相似的问题的已接受答案中的想法进一步优化代码:
How to find common keys in a list of dicts and sort them by value?
最新版本:
def intersect_dicts(dict_list):
""" Create dictionary containing of the keys common to all those in the list
associated with a list of their values from each.
"""
# Gather the values associated with the keys common to all the dicts in the list.
common_keys = set.intersection(*map(set, dict_list))
return {key: [d[key] for d in dict_list] for key in common_keys}
if __name__ == '__main__':
postings_list = [{0 : 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}]
intersection = intersect_dicts(postings_list)
print(intersection) # -> {0: [0.68426, 0.9823]}
def query_RR(postings, qtext):
words = tokenize(qtext)
allpostings = [postings[w] for w in words]
for a in allpostings:
print a.keys()
这是查询的结果
[0, 2, 3, 4, 6] [1, 4, 5] [0, 2, 4] [4, 5]
查询采用用户输入词条 (qtext
)、标记化并为每个标记生成帖子列表。
发帖列表是嵌套字典的列表。
例如
[{0 : 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}]
我正在尝试使用键查找这些嵌套字典的交集。
谁能给我一个解决方案,不胜感激!
既然你在评论中阐明了你想要的内容,这里是代码的修订版本,它将公共值收集到列表字典中。正如其他人所建议的那样,诀窍是使用 built-in set
类型来有效地确定键的交集。
自从我最初修改后,我已经能够使用对一个非常相似的问题的已接受答案中的想法进一步优化代码:
How to find common keys in a list of dicts and sort them by value?
最新版本:
def intersect_dicts(dict_list):
""" Create dictionary containing of the keys common to all those in the list
associated with a list of their values from each.
"""
# Gather the values associated with the keys common to all the dicts in the list.
common_keys = set.intersection(*map(set, dict_list))
return {key: [d[key] for d in dict_list] for key in common_keys}
if __name__ == '__main__':
postings_list = [{0 : 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}]
intersection = intersect_dicts(postings_list)
print(intersection) # -> {0: [0.68426, 0.9823]}