如何创建具有所有行和值的新变量?

时间:2019-09-07 02:53:32

标签: python python-3.x variables

我遇到一个麻烦的循环。我试图创建一个新变量,但问题是它只会输出最后一行和值。有没有办法用所有行和值创建一个新变量

这里是一个例子。我有两个匹配的二维列表。

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)

1 个答案:

答案 0 :(得分:0)

要创建所有行的列表,请对extenda使用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]]