我遇到一个麻烦的循环。我试图创建一个新变量,但问题是它只会输出最后一行和值。有没有办法用所有行和值创建一个新变量
这里是一个例子。我有两个匹配的二维列表。
a=[[9, 15, 25],[4, 14, 18, 25],[11, 12, 24, 25],[4, 8, 9, 26]]
b=[[2, 4, 7, 13, 14],[3, 5, 8, 13, 14],[6, 9, 10, 13, 14],[5, 6, 7, 13, 15],[3, 4, 9, 13, 15],[2, 8, 12, 13, 15],[4, 6, 8, 14, 15],[2, 5, 9, 14, 15]]
但是问题是我想创建包含所有行和值的新变量?
for idx,item_a in enumerate(a):
result = []
a = set(item_a)
for item_b in b:
result.append(list(a.intersection(item_b)))
Df="{} : {}".format(idx,result)
答案 0 :(得分:0)
要创建所有行的列表,请对extend
和a
使用b
a=[[9, 15, 25],[4, 14, 18, 25],[11, 12, 24, 25],[4, 8, 9, 26]]
b=[[2, 4, 7, 13, 14],[3, 5, 8, 13, 14],[6, 9, 10, 13, 14],[5, 6, 7, 13, 15],[3, 4, 9, 13, 15],[2, 8, 12, 13, 15],[4, 6, 8, 14, 15],[2, 5, 9, 14, 15]]
c=a[:]
c.extend(b)
值c
-
[[9, 15, 25], [4, 14, 18, 25], [11, 12, 24, 25], [4, 8, 9, 26], [2, 4, 7, 13, 14], [3, 5, 8, 13, 14], [6, 9, 10, 13, 14], [5, 6, 7, 13, 15], [3, 4, 9, 13, 15], [2, 8, 12, 13, 15], [4, 6, 8, 14, 15], [2, 5, 9, 14, 15]]