简体   繁体   English

如何使用 python 即时创建多维数组?

[英]how to create a multidimensional array on the fly using python?

I have a loop which generates a value_list each time it runs, at the end of each iteration i want to append all the lists into a one multi dimensional array我有一个循环,每次运行时都会生成一个 value_list,在每次迭代结束时,我想 append 将所有列表放入一个多维数组中

I have:我有:

  • value_list = [1,2,3,4] in 1st iteration第一次迭代中的value_list = [1,2,3,4]
  • value_list = [5,6,7,8] in 2nd iteration value_list = [5,6,7,8]在第二次迭代中
  • value list = [9,10,11,12] in 3rd iteration第 3 次迭代中的value list = [9,10,11,12]
  • etc... ETC...

At the end of each iteration I want one multi dimensional array like在每次迭代结束时,我想要一个多维数组,例如

  • value_list_copy = [[1,2,3,4]] in the 1st iteration value_list_copy = [[1,2,3,4]]在第一次迭代中
  • value_list_copy = [[1,2,3,4],[5,6,7,8]] in the 2nd iteration value_list_copy = [[1,2,3,4],[5,6,7,8]]在第二次迭代
  • value_list_copy = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
  • etc... ETC...

How could I achieve this?我怎么能做到这一点?

Thanks谢谢

You can use a nested comprehension and itertools.count :您可以使用嵌套理解和itertools.count

from itertools import count, islice

cols = 4
rows = 5

c = count(1)
matrix = [[next(c) for _ in range(cols)] for _ in range(rows)]
# [[1, 2, 3, 4], 
#  [5, 6, 7, 8], 
#  [9, 10, 11, 12], 
#  [13, 14, 15, 16], 
#  [17, 18, 19, 20]]

The cool kids might also want to zip the count iterator with itself:酷孩子可能还想zip本身的计数迭代器:

list(islice(zip(*[c]*cols), rows))
# [(1, 2, 3, 4), 
#  (5, 6, 7, 8), 
#  (9, 10, 11, 12), 
#  (13, 14, 15, 16), 
#  (17, 18, 19, 20)]

If you are using Python3.8 then use Walrus assignment ( := ).如果您使用的是Python3.8 ,则使用Walrus assignment ( := )。

For Syntax and semantic .对于Syntax and semantic

count=0
rows=5
cols=4
[[(count:=count+1) for _ in range(cols)] for _ in range(rows)]

Output: Output:

[[1, 2, 3, 4],
 [5, 6, 7, 8],
 [9, 10, 11, 12],
 [13, 14, 15, 16],
 [17, 18, 19, 20]]

Without using := .不使用:=

rows=5
cols=4
[list(range(i,i+cols)) for i in range(1,rows*cols,cols)]

Try this:尝试这个:

limit = 10
length_of_elements_in_each_list = 4
[range(i, i+length_of_elements_in_each_list) for i in range(1, limit)]

You can set a limit and length_of_elements_in_each_list according to your need.您可以根据需要设置限制和 length_of_elements_in_each_list。

Try this below:在下面试试这个:

value_list_copy = []

for i in range(n): # ----------> Assuming n is the number of times your loop is running
    value_list_copy.append(value_list)  # ------ Append your value list in value_list_copy in every iteration

Here you will get an array of arrays.在这里,您将获得一个 arrays 数组。

print(value_list_copy)

Here are two other possible solutions:以下是另外两种可能的解决方案:

Double for loop approach双循环方法

rows, cols, start = 3, 4, 1

value_list_copy = []
for j in range(rows):
    value_list = []
    for i in range(start, cols + start):
        value_list.append((j*cols)+i)
    value_list_copy.append(value_list)
    print(
        f'value_list = {value_list}\n'
        f'value_list_copy = {value_list_copy}\n'
    )

List comp method列表比较方法

rows, cols, start = 3, 4, 1

value_list_copy_2 = [
    [
        (j*cols)+i for i in range(start, cols + start)
    ] for j in range(rows)
]
print(f'value_list_copy_2 = {value_list_copy_2}')

Python Tutor Link to example code Python 导师链接到示例代码

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

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