简体   繁体   English

如何将字符串转换为列表中的列表? (Python)

[英]How can I convert a string to a list in a list? (Python)

I want to convert pieces of a string into lists and those lists into a matrix.我想将字符串片段转换成列表,然后将这些列表转换成矩阵。 For example: My string is "75 95 64 17 47 82 18 35 87 10 20 04 82 47 65 19 01 23 75 03 34 88 02 77 73 07 63 67 99 65 04 28 06 16 70 92 41 41 26 56 83 40 80 70 33 41 48 72 33 47 32 37 16 94 29 53 71 44 65 25 43 91 52 97 51 14 70 11 33 28 77 73 17 78 39 68 17 57 91 71 52 38 17 14 91 43 58 50 27 29 48 63 66 04 68 89 53 67 30 73 16 69 87 40 31 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23" .例如:我的字符串是"75 95 64 17 47 82 18 35 87 10 20 04 82 47 65 19 01 23 75 03 34 88 02 77 73 07 63 67 99 65 04 28 06 16 70 92 41 41 26 56 83 40 80 70 33 41 48 72 33 47 32 37 16 94 29 53 71 44 65 25 43 91 52 97 51 14 70 11 33 28 77 73 17 78 39 68 17 57 91 71 52 38 17 14 91 43 58 50 27 29 48 63 66 04 68 89 53 67 30 73 16 69 87 40 31 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23" I want 75 in a list1.我想要 list1 中的75 95, 64 in list2, 17, 47, 82 in list3 etc. Then I want to put those list in one (head) list/matrix. list2 中的 95、64 17, 47, 82 95, 64中的 17、47、82 等。然后我想将这些列表放在一个(头)列表/矩阵中。 So it would be [[75][95,64][17,47,82]] .所以它将是[[75][95,64][17,47,82]] When I tried to do this the outcome was: [['75']['95','64']['17','47','82']] .当我尝试这样做时,结果是: [['75']['95','64']['17','47','82']]

The code I wrote:我写的代码:

String = "75 95 64 17 47 82 18 35 87 10 20 04 82 47 65 19 01 23 75 03 34 88 02 77 73 07 63 67 99 65 04 28 06 16 70 92 41 41 26 56 83 40 80 70 33 41 48 72 33 47 32 37 16 94 29 53 71 44 65 25 43 91 52 97 51 14 70 11 33 28 77 73 17 78 39 68 17 57 91 71 52 38 17 14 91 43 58 50 27 29 48 63 66 04 68 89 53 67 30 73 16 69 87 40 31 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23"
lst1 = list(String.split(" "))
matrix = []
x = 0
while lst1 != []:
    matrix.append(lst1[:1+x])
    lst1 = lst1[1+x:]
    x += 1
print(matrix)

My question is: What must I change in order to get the outcome that I want (so without ' ' in the matrix)?我的问题是:我必须改变什么才能得到我想要的结果(所以矩阵中没有 ' ')?

You were close.你很接近。 The only thing that you missed, was the conversion of the string values (eg '75' to 75 ).您唯一错过的是字符串值的转换(例如'75'75 )。

In order to do this, you could write it like this:为了做到这一点,你可以这样写:

    while lst1 != []:
        converted_list = [ int(item) for item in lst1[:1+x] ]
        matrix.append(converted_list)

This will convert each item from the list into an int, storing it in a new list that you can append to your matrix, while still being able to perform other operations on your list in the same way as you would have done previously.这会将列表中的每个项目转换为一个 int,将其存储在一个新列表中,您可以 append 到您的矩阵,同时仍然能够以与之前相同的方式对您的列表执行其他操作。

I just added the code bellow after your code:我刚刚在您的代码之后添加了以下代码:

for i in range(len(matrix)):
    for j in range(len(matrix[i])):
        matrix[i][j] = int(matrix[i][j])

I'm looking for a more pythonic way to doing this:)我正在寻找一种更pythonic的方式来做到这一点:)

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

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