简体   繁体   English

将一个数组中的值附加到另一个未知维度的数组

[英]Appending values from one array to another array of unknown dimension

I have an array A of dimension (654 X 2).我有一个维度数组 A (654 X 2)。 Now within a loop, I have an 'if' statement.现在在一个循环中,我有一个“if”语句。 If for a value of 'i', the condition is true, I need to append the values of 'i'th row of A into a separate array B. That means the dimension of B is not known to me beforehand.如果对于“i”的值,条件为真,我需要将 A 的“第 i”行的值附加到单独的数组 B 中。这意味着我事先不知道 B 的维度。 So, how to initialize such array B in python.那么,如何在python中初始化这样的数组B。 If this is not the procedure, suggest me alternative ways to execute the same.如果这不是程序,请建议我执行相同的替代方法。

You could initialize B as empty array:您可以将 B 初始化为空数组:

B = []

Then when needs, just append it with the value:然后在需要时,只需将其附加值:

B.append( [x,y] )

you do not provide ANY code to start from, please read this to learn how to ask a question How to create a Minimal, Complete, and Verifiable example您没有提供任何代码作为起点,请阅读本文以了解如何提出问题如何创建最小、完整和可验证的示例

from the almost 0 information that you've provided从您提供的几乎 0 条信息中

you should try doing something like this:你应该尝试做这样的事情:

B = []
for i in range(n):
    if i % 2 == 0: # example of condition
        B += [ A[i] ]

print(B)

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

相关问题 numpy:将维度从一个数组整合到另一个数组 - Numpy: integrate dimension from one array into another 附加一个数组中的值,如果另一个数组中的值大于某个值,否则 append 该值 - Appending values from one array, if value in another array is greater than a certain value, else append that value 优雅的列表理解,根据另一个维度中的值提取数组一维中的值 - Elegant list comprehension to extract values in one dimension of an array based on values in another dimension 将 Z 维度中的一个数组与另一个数组相加或减去,其中纬度(Y 维度)和经度(X)不同 - Add or subtract one array in the Z dimension from another, where latitude (Y dimension) and longitude (X) are different 根据另一个数组中值的出现,从一个数组添加值 - Adding values from one array depending on occurrences of values in another array 一个数组中的 Select 值使用 numpy 中另一个数组的值 - Select values from one array using values of another array in numpy 在熊猫中将值从一列追加到另一列 - Appending values from one column to another in pandas 将值从一个列表追加到另一个 - Appending values from one list to another 将值从一个numpy数组填充到另一个 - Populate values from one numpy array into another 使用数组中的列值附加列表列表 - appending lists of list with column values from an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM