拆分子列表同时保留原始列表?

时间:2018-02-20 23:20:31

标签: python list

我有一个名为coordinates的大型列表,其中每个条目如下所示。致电print coordinates[1]

["0 0 255 \\n'", " '229 125 5 \\n'", " '229 126 6 \\n'", " '229 127 7 \\n'", " '229 128 8 \\n'", " '229 129 7 \\n'", " '229 130 8 \\n'", " '229 131 9 \\n'", " '229 132 9 \\n'", " '229 133 12 \\n'", " '229 134 9 \\n'", " '229 137 5 \\n'", " '629 140 5 \\n'", " '631 140 5 \\n'", " '632 140 5 \\n'", " '633 140 8 \\n'", " '422 141 5 \\n'", " '628 141 9 \\n'", " '629 141 11 \\n'", " '630 141 12 \\n'", " '631 141 12 \\n'", " '632 141 12 \\n'", " '633 141 11 \\n'", " '634 141 5 \\n'", " '422 142 9 \\n'", " '628 142 9 \\n'", " '629 142 12 \\n'", " '630 142 13 \\n'", " '631 142 11 \\n'", " '632 142 12 \\n'", " '633 142 11 \\n'", " '422 143 9 \\n'", " '628 143 8 \\n'", " '629 143 10 \\n'", " '630 143 11 \\n'", " '631 143 12 \\n'", " '632 143 11 \\n'", " '633 143 9 \\n'", " '422 144 5 \\n'", " '628 144 8 \\n'", " '629 144 12 \\n'", " '630 144 12 \\n'", " '631 144 10 \\n'", " '632 144 9 \\n'", " '633 144 6 \\n'", " '629 145 8 \\n'", " '630 145 8 \\n'", " '631 145 8 \\n'", " '632 145 8 \\n'", " '632 146 5 \\n'", " '"]

所以每个条目都是一个子列表。然后我可以拨打coordinates[1][1]来获取'229 125 5 \n'

我现在想创建另一个子列表,将每个coordinate[i][j]分成新的子列表,这些子列表由显示的每个数字组成。我希望coordinates[1][1]返回['299', '125', '5'],然后我可以调用coordinates[i][j][k]形式的内容来获取一个数字。但我想把这一切都作为我的大型名单的一部分。我怎么能这样做?

以下是我尝试过的内容,它没有改变coordinates的任何内容(它没有创建我想要的其他子列表):

for i in range(len(coordinates)):
    for j in range(len(coordinates[i])):
        coordinates[i][j].split(' ')

谢谢!

3 个答案:

答案 0 :(得分:1)

您可以使用re.findall

import re
s = ["0 0 255 \\n'", " '229 125 5 \\n'", " '229 126 6 \\n'", " '229 127 7 \\n'", " '229 128 8 \\n'", " '229 129 7 \\n'", " '229 130 8 \\n'", " '229 131 9 \\n'", " '229 132 9 \\n'", " '229 133 12 \\n'", " '229 134 9 \\n'", " '229 137 5 \\n'", " '629 140 5 \\n'", " '631 140 5 \\n'", " '632 140 5 \\n'", " '633 140 8 \\n'", " '422 141 5 \\n'", " '628 141 9 \\n'", " '629 141 11 \\n'", " '630 141 12 \\n'", " '631 141 12 \\n'", " '632 141 12 \\n'", " '633 141 11 \\n'", " '634 141 5 \\n'", " '422 142 9 \\n'", " '628 142 9 \\n'", " '629 142 12 \\n'", " '630 142 13 \\n'", " '631 142 11 \\n'", " '632 142 12 \\n'", " '633 142 11 \\n'", " '422 143 9 \\n'", " '628 143 8 \\n'", " '629 143 10 \\n'", " '630 143 11 \\n'", " '631 143 12 \\n'", " '632 143 11 \\n'", " '633 143 9 \\n'", " '422 144 5 \\n'", " '628 144 8 \\n'", " '629 144 12 \\n'", " '630 144 12 \\n'", " '631 144 10 \\n'", " '632 144 9 \\n'", " '633 144 6 \\n'", " '629 145 8 \\n'", " '630 145 8 \\n'", " '631 145 8 \\n'", " '632 145 8 \\n'", " '632 146 5 \\n'", " '"]
new_s = filter(None, map(lambda x:re.findall('\d+', x), s))

输出:

[['0', '0', '255'], ['229', '125', '5'], ['229', '126', '6'], ['229', '127', '7'], ['229', '128', '8'], ['229', '129', '7'], ['229', '130', '8'], ['229', '131', '9'], ['229', '132', '9'], ['229', '133', '12'], ['229', '134', '9'], ['229', '137', '5'], ['629', '140', '5'], ['631', '140', '5'], ['632', '140', '5'], ['633', '140', '8'], ['422', '141', '5'], ['628', '141', '9'], ['629', '141', '11'], ['630', '141', '12'], ['631', '141', '12'], ['632', '141', '12'], ['633', '141', '11'], ['634', '141', '5'], ['422', '142', '9'], ['628', '142', '9'], ['629', '142', '12'], ['630', '142', '13'], ['631', '142', '11'], ['632', '142', '12'], ['633', '142', '11'], ['422', '143', '9'], ['628', '143', '8'], ['629', '143', '10'], ['630', '143', '11'], ['631', '143', '12'], ['632', '143', '11'], ['633', '143', '9'], ['422', '144', '5'], ['628', '144', '8'], ['629', '144', '12'], ['630', '144', '12'], ['631', '144', '10'], ['632', '144', '9'], ['633', '144', '6'], ['629', '145', '8'], ['630', '145', '8'], ['631', '145', '8'], ['632', '145', '8'], ['632', '146', '5']]

答案 1 :(得分:1)

所以,当你使用" split"你没有改变价值。该方法返回一个基于您使用的参数创建的列表,并返回该列表,但它是一个新值,位于内存中的不同空间中。

为了将来使用这些值,您需要将它们分配给某些东西。

使用您的代码可以尝试这样的事情:

System.NotImplementedException: The method or operation is not implemented.
   at MySql.Data.EntityFrameworkCore.Scaffolding.Internal.MySQLDatabaseModelFactory.Create(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.Create(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ModelScaffolder.Generate(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas, String projectPath, String outputPath, String rootNamespace, String contextName, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The method or operation is not implemented.

这仍然是非常糟糕的代码,因为它难以阅读,并且没有利用python的优秀设施来处理数据(你应该尽可能避免使用地图并使用地图)但是我我想用这种方式来帮你看出你的错误。

我认为你得到的其他答案是更好的代码,你应该向他们学习。但这会增加,因为它可以帮助您了解您的东西失败的地方。

此外,如结果所示,拆分(&#39;&#39;)不足以清理您的数据。我认为re的答案很好。

答案 2 :(得分:0)

coordinates = [x[:-3].split() for x in original_list]