简体   繁体   中英

Can I append specific values from one array to another?

I have successfully imported a CSV file into a multi-dimensional array in python. What I want to do now is pick specific values from the array and put them into a new single array. For instance if my current arrays were:

[code1, name1, number 1]
[code2, name2, number 2]

I want to select only the code1 and code 2 values and insert them into a new array, because I need to compare just those values to a user input for validation. I have tried using the following:

newvals=[]
newvals.append oldvals([0],[0])

where newvals is the new array for just the codes, oldvals is the original array with all the data and the index [0],[0] refers to code 1, but I'm getting a syntax error. I can't use any add ons as they will be blocked by my admin.

newvals = []
for i in oldvals:
    newvals.append(i[0])

Usually you can get the first Element in an array a with a[0] . You can create a new array based on another by using the " array for in " syntax

oldData = [[1,2,3],[4,5,6]]
newData = [x[0] for x in oldList]
# newData is now [1,4]

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