[英]Grouping together items that appear together in separate list
我有 2 个嵌套列表,我想编写一些代码来运行两个列表中的每个子列表,并将两个列表中一起出现的任何元素组合在一起。 我正在做的项目实际上有巨大的嵌套列表,所以我创建了以下 2 个列表来稍微简化问题(我只有一年的 python 经验)。 如果可以制作 function 将这两个列表中的元素组合在一起,那么我可以将 function 应用于实际项目。 这个问题可能类似于: 查找一起出现在多个列表中的项目,但我无法理解该问题中编写的代码,正如我所说,我对 python 比较陌生。
my_list = [['a', 'd', 'l'], ['c', 'e', 't'], ['q', 'x'], ['p', 'f', 'd', 'k']
sec_list = [['f', 'd', 'w', 'a'], ['c', 'e', 'u', 'h'], ['q', 'x', 'd', 'z'], ['p', 'k']]
##The output should be something like:
[['a', 'd'], ['c', 'e'], ['q', 'x'], ['p', 'k'], ['f', 'd']]```
Thanks
您可以使用zip
遍历两个序列并找到具有集合交集的共同元素。 请注意,您的代码在my_list
中缺少结束 ]
my_list = [['a', 'd', 'l'], ['c', 'e', 't'], ['q', 'x'], ['p', 'f', 'd', 'k']]
sec_list = [['f', 'd', 'w', 'a'], ['c', 'e', 'u', 'h'], ['q', 'x', 'd', 'z'], ['p', 'k']]
# each item of my_list and sec_list are lists
# zip allows parallel iteration so l1 and l2 are the pairs of inner lists
# sets are designed for tasks like finding common elements
# the & sign is python for set intersection
matches = []
for l1, l2 in zip(my_list, sec_list):
matches.append(list(set(l1) & set(l2)))
这可以合并到列表理解中
my_list = [['a', 'd', 'l'], ['c', 'e', 't'], ['q', 'x'], ['p', 'f', 'd', 'k']]
sec_list = [['f', 'd', 'w', 'a'], ['c', 'e', 'u', 'h'], ['q', 'x', 'd', 'z'], ['p', 'k']]
matches = [list(set(l1) & set(l2)) for l1, l2 in zip(my_list, sec_list)]
如果要保留重复项,可以使用collections.Counter
使用此解决方案:
from collections import Counter
my_list = [['a', 'd', 'l'], ['c', 'e', 't'], ['q', 'x'], ['p', 'f', 'd', 'k', 'k']]
sec_list = [['f', 'd', 'w', 'a'], ['c', 'e', 'u', 'h'], ['q', 'x', 'd', 'z'], ['p', 'k', 'k']]
result = [list((Counter(a) & Counter(b)).elements()) for a in my_list for b in sec_list]
result = [x for x in result if len(x) > 0]
print(result)
Output:
[['a', 'd'], ['d'], ['c', 'e'], ['x', 'q'], ['f', 'd'], ['d'], ['k', 'k', 'p']]
根据评论更新。
根据评论:
my_list = [["a", "d", "l"], ["c", "e", "t"], ["q", "x"], ["p", "f", "d", "k"]]
sec_list = [
["f", "d", "w", "a"],
["c", "e", "u", "h"],
["q", "x", "d", "z"],
["p", "k"],
]
# to speed up, convert the sublists to sets
tmp1 = [set(i) for i in my_list]
tmp2 = [set(i) for i in sec_list]
out = []
for s1 in tmp1:
for s2 in tmp2:
m = s1 & s2
if m:
out.append(list(m))
print(out)
印刷:
[['d', 'a'], ['d'], ['e', 'c'], ['x', 'q'], ['d', 'f'], ['d'], ['k', 'p']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.