Home > Software engineering >  How do I split this list into another list within a list?
How do I split this list into another list within a list?

Time:03-02

I want to split the following list

['1', '9', '8', '0', '9', '3', '7', '6', '5', '0', '8', '7', '3', '3', '0', '3', '0', '4', ':', '0', '0', '4', '5', '6', '4', '3', '0', '|', '8', '1', '1', '3', '0', '9', '1', '9', '4', '1', '8', '2', '6', '5', '6', '0', '1', '1', ':', '0', '0', '4', '5', '6', '0', '|', '8', '6', '7', '0', '5', '6', '0', '3', '5', '4', '4', '5', '7', '3', '5', '4', '4', '5', ':', '0', '0', '4', '5', '6', '6', '1', '0', '|', '8', '1', '7', '1', '4', '7', '3', '5', '2', '6', '1', '8', '8', '9', '3', '4', '0', '1', ':', '0', '0', '4', '5', '6', '3', '0', '|', '4', '0', '9', '2', '9', '2', '7', '0', '9', '3', '4', '2', '4', '7', '8', '3', '5', '8', ':', '0', '0', '4', '5', '6', '4', '9', '0', '|', '3', '1', '5', '0', '7', '9', '5', '9', '2', '9', '6', '6', '6', '1', '9', '1', '4', '7', ':', '0', '0', '4', '5', '6', '3', '0', '|', '5', '6', '3', '8', '1', '6', '8', '7', '7', '8', '0', '6', '0', '5', '9', '5', '2', '0', ':', '0', '0', '4', '5', '6', '6', '3', '0', '|', '2', '3', '7', '2', '5', '1', '8', '0', '7', '4', '0', '5', '9', '3', '2', '5', '6', '4', ':', '0', '0', '4', '5', '6', '4', '5', '0', '|', '1', '7', '2', '0', '0', '2', '2', '7', '5', '4', '1', '2', '2', '7', '9', '2', '9', '6', ':', '0', '0', '4', '5', '6', '2', '0', '|', '3', '0', '2', '0', '5', '0', '8', '7', '2', '3', '8', '3', '2', '4', '2', '2', '4', '0', ':', '0', '0', '4', '5', '6', '3', '1', '0', '|', '9', '4', '1', '4', '0', '2', '8', '4', '3', '2', '2', '1', '4', '8', '7', '7', '0', '6', ':', '0', '0', '4', '5', '6', '3', '9', '0', '|', '4', '6', '0', '0', '2', '3', '2', '4', '7', '2', '0', '3', '9', '9', '1', '5', '5', '2', ':', '0', '0', '4', '5', '6', '2', '0', '|', '4', '2', '3', '5', '3', '6', '6', '0', '6', '7', '0', '4', '8', '2', '8', '4', '1', '6', ':', '0', '0', '4', '5', '6', '4', '7', '0']

Into another list called userIDs where the 18 first characters (the IDs) can be put into a list with the smaller numbers until the | symbol.

Example:

[[198093765087330304,00456430],[(next ID),(next smaller number)]]

CodePudding user response:

You can use a nested list comprehension:

>>> [part.split(":") for part in "".join(my_list).split("|")]
[['198093765087330304', '00456430'], ['811309194182656011', '004560'],
 ['867056035445735445', '00456610'], ['817147352618893401', '0045630'],
 ['409292709342478358', '00456490'], ['315079592966619147', '0045630'],
 ['563816877806059520', '00456630'], ['237251807405932564', '00456450'],
 ['172002275412279296', '0045620'], ['302050872383242240', '00456310'],
 ['941402843221487706', '00456390'], ['460023247203991552', '0045620'],
 ['423536606704828416', '00456470']]

CodePudding user response:

My solution puts your list into one string and then splits it twice, first with "|" and then with ":"

my_list = ['1', '9', '8', '0', '9', '3', '7', '6', '5', '0', '8', '7', '3', '3', '0', '3', '0', '4', ':', '0', '0', '4', '5', '6', '4', '3', '0', '|', '8', '1', '1', '3', '0', '9', '1', '9', '4', '1', '8', '2', '6', '5', '6', '0', '1', '1', ':', '0', '0', '4', '5', '6', '0', '|', '8', '6', '7', '0', '5', '6', '0', '3', '5', '4', '4', '5', '7', '3', '5', '4', '4', '5', ':', '0', '0', '4', '5', '6', '6', '1', '0', '|', '8', '1', '7', '1', '4', '7', '3', '5', '2', '6', '1', '8', '8', '9', '3', '4', '0', '1', ':', '0', '0', '4', '5', '6', '3', '0', '|', '4', '0', '9', '2', '9', '2', '7', '0', '9', '3', '4', '2', '4', '7', '8', '3', '5', '8', ':', '0', '0', '4', '5', '6', '4', '9', '0', '|', '3', '1', '5', '0', '7', '9', '5', '9', '2', '9', '6', '6', '6', '1', '9', '1', '4', '7', ':', '0', '0', '4', '5', '6', '3', '0', '|', '5', '6', '3', '8', '1', '6', '8', '7', '7', '8', '0', '6', '0', '5', '9', '5', '2', '0', ':', '0', '0', '4', '5', '6', '6', '3', '0', '|', '2', '3', '7', '2', '5', '1', '8', '0', '7', '4', '0', '5', '9', '3', '2', '5', '6', '4', ':', '0', '0', '4', '5', '6', '4', '5', '0', '|', '1', '7', '2', '0', '0', '2', '2', '7', '5', '4', '1', '2', '2', '7', '9', '2', '9', '6', ':', '0', '0', '4', '5', '6', '2', '0', '|', '3', '0', '2', '0', '5', '0', '8', '7', '2', '3', '8', '3', '2', '4', '2', '2', '4', '0', ':', '0', '0', '4', '5', '6', '3', '1', '0', '|', '9', '4', '1', '4', '0', '2', '8', '4', '3', '2', '2', '1', '4', '8', '7', '7', '0', '6', ':', '0', '0', '4', '5', '6', '3', '9', '0', '|', '4', '6', '0', '0', '2', '3', '2', '4', '7', '2', '0', '3', '9', '9', '1', '5', '5', '2', ':', '0', '0', '4', '5', '6', '2', '0', '|', '4', '2', '3', '5', '3', '6', '6', '0', '6', '7', '0', '4', '8', '2', '8', '4', '1', '6', ':', '0', '0', '4', '5', '6', '4', '7', '0']
s = '' # define string
for element in my_list:  # for each element in list add it to a string
    s  = element
s = s.split(sep="|")  # split the string with '|' separator
for i in range(len(s)):
    s[i] = s[i].split(sep=':')  # split each element of list with ':' separator
print(list(s))

Result: [['198093765087330304', '00456430'], ['811309194182656011', '004560'], ['867056035445735445', '00456610'], ['817147352618893401', '0045630'], [...

CodePudding user response:

q = ['1', '9', '8', '0', '9', '3', '7', '6', '5', '0', '8', '7', '3', '3', '0', '3', '0', '4', ':', '0', '0', '4', '5', '6', '4', '3', '0', '|', '8', '1', '1', '3', '0', '9', '1', '9', '4', '1', '8', '2', '6', '5', '6', '0', '1', '1', ':', '0', '0', '4', '5', '6', '0', '|', '8', '6', '7', '0', '5', '6', '0', '3', '5', '4', '4', '5', '7', '3', '5', '4', '4', '5', ':', '0', '0', '4', '5', '6', '6', '1', '0', '|', '8', '1', '7', '1', '4', '7', '3', '5', '2', '6', '1', '8', '8', '9', '3', '4', '0', '1', ':', '0', '0', '4', '5', '6', '3', '0', '|', '4', '0', '9', '2', '9', '2', '7', '0', '9', '3', '4', '2', '4', '7', '8', '3', '5', '8', ':', '0', '0', '4', '5', '6', '4', '9', '0', '|', '3', '1', '5', '0', '7', '9', '5', '9', '2', '9', '6', '6', '6', '1', '9', '1', '4', '7', ':', '0', '0', '4', '5', '6', '3', '0', '|', '5', '6', '3', '8', '1', '6', '8', '7', '7', '8', '0', '6', '0', '5', '9', '5', '2', '0', ':', '0', '0', '4', '5', '6', '6', '3', '0', '|', '2', '3', '7', '2', '5', '1', '8', '0', '7', '4', '0', '5', '9', '3', '2', '5', '6', '4', ':', '0', '0', '4', '5', '6', '4', '5', '0', '|', '1', '7', '2', '0', '0', '2', '2', '7', '5', '4', '1', '2', '2', '7', '9', '2', '9', '6', ':', '0', '0', '4', '5', '6', '2', '0', '|', '3', '0', '2', '0', '5', '0', '8', '7', '2', '3', '8', '3', '2', '4', '2', '2', '4', '0', ':', '0', '0', '4', '5', '6', '3', '1', '0', '|', '9', '4', '1', '4', '0', '2', '8', '4', '3', '2', '2', '1', '4', '8', '7', '7', '0', '6', ':', '0', '0', '4', '5', '6', '3', '9', '0', '|', '4', '6', '0', '0', '2', '3', '2', '4', '7', '2', '0', '3', '9', '9', '1', '5', '5', '2', ':', '0', '0', '4', '5', '6', '2', '0', '|', '4', '2', '3', '5', '3', '6', '6', '0', '6', '7', '0', '4', '8', '2', '8', '4', '1', '6', ':', '0', '0', '4', '5', '6', '4', '7', '0']

you can do this series of operations: First, you convert everything into a string, replace colons with commas and then split at |. Then pack each individual entry into a separate list.

[[a] for a in ''.join(q).replace(':', ',').split('|')]

Edit: finding first entry only:

[[int(a.split(':')[0])] for a in ''.join(q).split('|')]
  • Related