简体   繁体   English

我可以从一个数组到另一个数组的 append 特定值吗?

[英]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.

相关问题 如何将值从一个数组存储到另一个数组? - How can i store values from one array to another array? pandas:如何根据列值在一个数据帧中从另一个数据帧中 append 行? - pandas: How can I append rows in one data frame from another based on column values? 将特定行从一个列表追加到另一个 - Append specific rows from one list to another 附加一个数组中的值,如果另一个数组中的值大于某个值,否则 append 该值 - Appending values from one array, if value in another array is greater than a certain value, else append that value 如何从一个数据框中的列中提取特定值并将它们附加到另一个数据框中的列中? - 熊猫 - How do you extract specific values from a column in one dataframe and append them to a column in another dataframe? - Pandas 如何根据索引更有效地将一个数组中的值分配给另一个数组? - How can I assign values from one array to another according to the index more efficiently? 用另一个数组的特定值替换一个数组中的值 - Replacing values in one array with specific values of another array append 如果 pandas 中没有重复,则从一个 df 到另一个的行值 - append row values from one df to another if no duplicates in pandas 将字符串从一个文件匹配到另一个文件并附加值 - Matching string from one file to another file and append the values 我可以将一个数据列中的特定值移动到另一列,同时保持其他值不变吗? - Can I shift specific values in one data column to another column while keeping the other values unchanged?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM