简体   繁体   中英

How to create and array of arrays by appending from an existing array of arrays

I want to take an array of int arrays and either turn it into or make an array of sting arrays. I know how to turn the ints into strings but what im struggling with is as follows: when I append from the original array of arrays, it makes the new array ONE array, with ALL of the contents of the array of arrays. I would like it to be an array of arrays as well, not one array. Note: xAxisValuesContainer and yAxisValuesContainer are both array of arrays where each sub array is of type number/int.

strXaxis = []
for i in range(len(xAxisValuesContainer)):
  for j in range(len(xAxisValuesContainer[i])):
    strXaxis.append(str(xAxisValuesContainer[i][j]))

strYaxis = []
for i in range(len(yAxisValuesContainer)):
  for j in range(len(yAxisValuesContainer[i])):
    strYaxis.append(str(yAxisValuesContainer[i][j]))

First of all, these are lists, not arrays.

You need to make a nested list in the outer loop.

You can do all of this with nested list comprehensions:

strXaxis = [[str(value) for value in row] for row in xAxisValuesContainer]]
strXaxis = [[str(value) for value in row] for row in yAxisValuesContainer]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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