简体   繁体   English

如何将元素添加到二维数组 python

[英]How to add an element into a 2-D array python

How to use data1 and data2 to get data3?python It seems to use for loop to iterate every element in the two arrays, but I don't know-how I used append function wish to append data1[i].append(data2[j]), but it does not work! How to use data1 and data2 to get data3?python It seems to use for loop to iterate every element in the two arrays, but I don't know-how I used append function wish to append data1[i].append(data2[ j]),但它不起作用! I can use我可以用

data1.append(data2[0]) to get [[1, 2, 3], 'Mon'],but not the rest of the array data1.append(data2[0]) 得到[[1, 2, 3], 'Mon'],而不是数组的rest

data1 = [
        [1,2,3],
        [3,2,1],
        [4,5,6]


        ]
data2 = ['Mon','Tues','Wed']

data3 = [
        [[1,2,3],'Mon'],
        [[2,3,4],'Tues'],
        [[3,4,5],'Wed']
        ]
data3 = list(zip(data1, data2))
data3 = [[d1, d2] for d1, d2 in zip(data1, data2)]

Output: Output:

[[[1, 2, 3], 'Mon'], [[3, 2, 1], 'Tues'], [[4, 5, 6], 'Wed']]
for a, b in zip(data1,data2):
    data3.append([a,b])

OR或者

data3 = [[d1, d2] for d1, d2 in zip(data1, data2)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM