将一起出现在单独列表中的项目组合在一起
Grouping together items that appear together in separate list
我有 2 个嵌套列表,我想编写一些代码来 运行 遍历两个列表中的每个子列表,并将出现在两个列表中的任何元素组合在一起。我正在从事的项目实际上有巨大的嵌套列表,所以我创建了以下 2 个列表来稍微简化问题(我只有一年的 python 经验)。如果可以制作一个函数将这两个列表中的元素组合在一起,我就可以将该函数应用到实际项目中。这个问题可能类似于:Find items that appear together on multiple lists,
但我无法理解该问题中编写的代码,正如我所说,我对 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)
输出:
[['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']]
我有 2 个嵌套列表,我想编写一些代码来 运行 遍历两个列表中的每个子列表,并将出现在两个列表中的任何元素组合在一起。我正在从事的项目实际上有巨大的嵌套列表,所以我创建了以下 2 个列表来稍微简化问题(我只有一年的 python 经验)。如果可以制作一个函数将这两个列表中的元素组合在一起,我就可以将该函数应用到实际项目中。这个问题可能类似于:Find items that appear together on multiple lists, 但我无法理解该问题中编写的代码,正如我所说,我对 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)
输出:
[['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']]