简体   繁体   中英

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). 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. Furthermore, if you want to get integers, you simply need to 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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