[英]Can I append specific values from one array to another?
I have successfully imported a CSV file into a multi-dimensional array in python.我已成功将 CSV 文件导入到 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:
例如,如果我当前的 arrays 是:
[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.我想 select 只有 code1 和 code 2 值并将它们插入到一个新数组中,因为我需要将这些值与用户输入进行比较以进行验证。 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.其中 newvals 是仅用于代码的新数组, oldvals 是包含所有数据的原始数组,索引 [0],[0] 是指代码 1,但出现语法错误。 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]
.通常,您可以使用
a[0]
获取数组a
中的第一个元素。 You can create a new array based on another by using the " array for in " syntax您可以使用“ array for in ”语法基于另一个数组创建一个新数组
oldData = [[1,2,3],[4,5,6]]
newData = [x[0] for x in oldList]
# newData is now [1,4]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.