简体   繁体   English

如何将带有数字的字符串转换为数组?

[英]How to turn a string, with numbers, into an array?

I'd like to turn the following string into an array with 3 columns and multiple rows if possible in python.如果可能的话,我想在 python 中将以下字符串转换为具有 3 列和多行的数组。 I'm attempting to turn the following into a workable numerical data set - using an array, and currently I'm unable to figure out how to turn it from a string --> array.我正在尝试将以下内容转换为可行的数字数据集-使用数组,目前我无法弄清楚如何将其从字符串-> 数组中转换。 I'm seeing that it's nearly in the format that I'd like it in. Is there a way to turn everything in between each apostrophe (ex. '-1,38,31857' as one row, and '-1,40,39304') as another row, and compile all of those into an array?我看到它几乎是我想要的格式。有没有办法在每个撇号之间转换所有内容(例如,'-1,38,31857' 作为一行,'-1,40 ,39304')作为另一行,并将所有这些编译成一个数组?

 '-1,38,31857',
 '-1,40,39304',
 '1582871,64,445338',
 '-1,37,29632',
 '-1,1,82',
 '-1,18,3613',
 '-1,6,544',
 '-1,23,7025',
 '-1,34,20775',
 '1979527,23,6361',
 '-1,10,1330',
 '-1,17,3300',
 '-1,11,1426',
 '-1,8,853',
 '-1,24,7087',
 '-1,1,0',
 '-1,1,0',
 '198113,79,1927770',
 '-1,1,0',
 '1763114,1,42',
 '1803615,4,357',
 '-1,1,0',
 '-1,1,0',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1'] ```

Using list comprehension and split() :使用列表理解和split()

>>> [[int(n) for n in row.split(",")] for row in strings]
[[-1, 38, 31857], [-1, 40, 39304], [1582871, 64, 445338], [-1, 37, 29632], [-1, 1, 82], [-1, 18, 3613], [-1, 6, 544], [-1, 23, 7025], [-1, 34, 20775], [1979527, 23, 6361], [-1, 10, 1330], [-1, 17, 3300], [-1, 11, 1426], [-1, 8, 853], [-1, 24, 7087], [-1, 1, 0], [-1, 1, 0], [198113, 79, 1927770], [-1, 1, 0], [1763114, 1, 42], [1803615, 4, 357], [-1, 1, 0], [-1, 1, 0], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1]]

Another option is to use the csv module:另一种选择是使用csv模块:

>>> import csv
>>> list(csv.reader(strings, quoting=csv.QUOTE_NONNUMERIC))
[[-1.0, 38.0, 31857.0], [-1.0, 40.0, 39304.0], [1582871.0, 64.0, 445338.0], [-1.0, 37.0, 29632.0], [-1.0, 1.0, 82.0], [-1.0, 18.0, 3613.0], [-1.0, 6.0, 544.0], [-1.0, 23.0, 7025.0], [-1.0, 34.0, 20775.0], [1979527.0, 23.0, 6361.0], [-1.0, 10.0, 1330.0], [-1.0, 17.0, 3300.0], [-1.0, 11.0, 1426.0], [-1.0, 8.0, 853.0], [-1.0, 24.0, 7087.0], [-1.0, 1.0, 0.0], [-1.0, 1.0, 0.0], [198113.0, 79.0, 1927770.0], [-1.0, 1.0, 0.0], [1763114.0, 1.0, 42.0], [1803615.0, 4.0, 357.0], [-1.0, 1.0, 0.0], [-1.0, 1.0, 0.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0]]

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

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