简体   繁体   English

如何在Python中将数组拆分为列和行?

[英]How to split array into columns and rows in Python?

I have a 2-dim matrix as an input for my function (that input is another function that returns an array with rows and columns). 我有一个2维矩阵作为我函数的输入(该输入是另一个函数,它返回包含行和列的数组)。 I would like to split that input (array) into width and height so I can use a for loop to access each element. 我想将该输入(数组)分为宽度和高度,以便可以使用for循环访问每个元素。 My for loop works fine, however I would like it to work for any array . 我的for循环工作正常,但是我希望它适用于任何array Now, it runs for a randomised array for which function I specified earlier. 现在,它针对我之前指定的函数的随机数组运行。 Maybe a split function would be useful? 也许分割功能会有用吗?

The beginning of my code is 我的代码的开头是

def array_new(column, row):
    new_state = array_rand(column, row) #previous created function

    for r in range(0,row):
        for c in range(0,column):
        ....

I would like to have something like 我想吃点东西

def array_new(array):

and then split the array into rows and columns. 然后将数组拆分为行和列。

Hope my question is understandable. 希望我的问题是可以理解的。

A python list is already "split" into rows. python列表已经“拆分”为行。 You can iterate over them with a for loop: 您可以使用for循环遍历它们:

def array_new(array):
    for row in array:
        # do something with the row

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

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