Stack Overflow Asked by Allison Mueller on January 25, 2021
Edited for clarity
Let’s say I have a list like this:
[‘1, 2, 3’, ‘4, 5, 6’, ‘7, 8, 9’]
wherein each entry is one string of 3 numbers.
How would I get this list into an array (of floats), such that 1, 4, and 7 correspond to the first column; 2, 5, and 8 correspond to the second column; and 3, 6, 9 are in the last column?
I’ve searched through other questions, most of which aren’t quite answering my particular question.
You can use 2D arrays to store the columns.
arr = ['1, 2, 3', '4, 5, 6', '7, 8, 9']
new_arr = [[],[],[]]
for column in arr:
column = column.split(", ")
new_arr[0].append(float(column[0]))
new_arr[1].append(float(column[1]))
new_arr[2].append(float(column[2]))
print(new_arr)
#prints [[1.0, 4.0, 7.0], [2.0, 5.0, 8.0], [3.0, 6.0, 9.0]]
Answered by Vader27 on January 25, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP