简体   繁体   English

Function 用于列表中的拆分数字和每个数字的唯一编号/代码

[英]Function for split digits in list and unique number/code for every digit

I have a data input which I want to bring into a specific shape.我有一个数据输入,我想把它变成一个特定的形状。

Data looks like this:数据如下所示:

01211231202143244400222255340042523252102440536423024350201113345340514003134
20023230143300003201455331343005145134541545264403161213336031512541125234215
01203204313112402314341530533423155434004002652564464622316236363153203455225

My code:我的代码:

with open('Dataday8.in') as file: 
    data = [i for i in file.read().split()]

After the code it looks like:在代码之后它看起来像:

data = ['01211231202143244400222255340042523252102440536423024350201113345340514003', '2002323014330000320145533134300514513454154526440316121333603151254112523421', '0120320431311240231434153053342315543400400265256446462231623636315320345522']

Buy I want it to be seperates after every digit (but each line should be in one bracket). Buy 我希望它在每个数字之后分开(但每一行都应该在一个括号中)。 Does not matter what I try my code never brings me to the goal.不管我尝试什么,我的代码永远不会让我达到目标。

After this I would like to give every digit in the list a unique number that I can work with it in a loop.在此之后,我想给列表中的每个数字一个唯一的数字,我可以在循环中使用它。

Thanks for any help谢谢你的帮助

This is the code I tried to split after every digit in the list:这是我尝试在列表中的每个数字之后拆分的代码:

[x for x in data.split('\n')]

I am simply reporting Maurice Meyer 's answer which perfectly solves the problem for better future readability.我只是简单地报告Maurice Meyer的回答,它完美地解决了问题,以便将来更好地阅读。 Furthermore, if you want to get integers, you simply need to di此外,如果你想得到整数,你只需要 di

data = [list(int(i)) for i in file.read().split()]
data = [[int(elem) for elem in row] for row in data]

Although already solved by Sala , i think your problem of int object is not iterable could be solved with this:尽管Sala已经解决了,但我认为您的int object is not iterable的问题可以通过以下方式解决:

data = list([int(elem) for elem in row] for row in (i for i in file.read().split()))

You can also write it like this:你也可以这样写:

data = list(list(int(elem) for elem in row) for row in (i for i in file.read().split()))

This is a one-liner but of course you can break it down.这是单线的,但您当然可以将其分解。

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

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