简体   繁体   中英

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! I can use

data1.append(data2[0]) to get [[1, 2, 3], 'Mon'],but not the rest of the array

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:

[[[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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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