简体   繁体   English

如何扩展某些选定行的列大小以及如何添加2D数组python2.7的其他行

[英]how to extend column size for some selected rows and also add additional rows of a 2D array python2.7

I need a 2D array with a fixed array name (say 'grids'), which can take variable row and variable column size based on certain conditions. 我需要具有固定数组名称(例如“ grids”)的2D数组,该数组可以根据特定条件获取可变的行和可变的列大小。 Like if the first condition is satisfied, then that will add a row to 'grids' and if other condition satisfies, then a new column has to be added - ONLY to that particular row of 'grids'. 就像如果满足第一个条件一样,那将在“网格”中添加一行,而如果其他条件满足,则必须添加新列-仅添加到“网格”的特定行中。 I considered a 15X15 2D array (assuming 15 being the largest row as well as column that could possibly be added) and initialized all with zeros. 我考虑了15X15 2D数组(假设15是最大的行以及可能添加的列),并用零初始化了所有数组。

Then whenever first condition satisfied, I added some value to the element of the row, and if next condition satisfied, then added some set of columns to a specific row, while the rest elements being zero. 然后,只要满足第一个条件,就向该行的元素添加一些值,如果满足下一个条件,则向特定的行添加一些列集,而其余元素为零。 While I assume this being a big and long way of doing, and storing unnecessary elements in the array, I also wish to have the rows to have continuous non-zero elements so that I can use consequent (row) elements to calculate, say the distance between them. 虽然我认为这是一个漫长而漫长的工作,并且将不必要的元素存储在数组中,但我也希望行具有连续的非零元素,以便我可以使用后续的(行)元素进行计算,例如他们之间的距离。

I wonder if there is any easy way of dealing with this problem? 我想知道是否有任何简单的方法来解决这个问题? - adding rows to an existing array, each row having varying column, array name being same! -将行添加到现有数组中,每行具有不同的列,数组名称相同! Thanks 谢谢

Maybe you tried that already but : 也许您已经尝试过了,但是:

grids = [[1,2,3], [4,5,6,7,8], [10,11,12]]
print grids 

# Will give you : [[1,2,3], [4,5,6,7,8], [10,11,12]]

Here, you have different number of columns for each row, does it help ? 在这里,每行有不同的列数,这有帮助吗?

Adding a column to the 2nd row is like : 在第二行添加一就像:

grids[1].append(9)
# Will give you : [[1,2,3], [4,5,6,7,8,9], [10,11,12]]

Adding a row will be like : 添加一行将类似于:

grids.append([13,14])
# Will give you : [[1,2,3], [4,5,6,7,8,9], [10,11,12], [13, 14]]

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

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