简体   繁体   中英

Splitting list of strings and re-arranging them in series of lists in python

The lists 'v1', 'v2', 'v3', and 'v4' are standard lists.

v1 = ['Elisa', 'Liza', 'Izabela', 'Elisabeth', 'Elizabeth', 'Lisa', 'Lizzy', 'Isabella', 'Isabelle', 'Isabela', 'Liz']
v2 = ['Abbey', 'Abbie', 'Abigail', 'Abby', 'Gail']
v3 = ['Jonathan', 'Jon', 'John', 'Jonny', 'Johnny', 'Nathan']
v4 = ['Alejandra','Alexandra','Alexis','Alya','Alexa','Lexi','Allie','Ally']

List 'a' is given as input:

a = ['Jonathan', 'Jim', 'Jennifer', 'Alya', 'Renee', 'Natasha', 'Phil', 'Lisa', 'Joe', 'Ana', 'Paul', 'Gail', 'Roderick', 'Patricia']

There will be always just one value from each standard list appearing anywhere in list 'a'. The expected output is a group of lists that will have sets of values partitioned by values that are found in list 'a' from lists 'v1', 'v2', 'v3', 'v4'. Position of these sets will be rearranged in the order of 'v1' value set -> 'v2' value set -> 'v3' value set -> 'v4' value set.

Expected output has list 'a' split in 4 lists - a_out1, a_out2, a_out3, and a_out4 in the following manner:

a_out1 = ['Lisa', 'Joe', 'Ana', 'Paul'] 
a_out2 = ['Gail', 'Roderick', 'Patricia']
a_out3 = ['Jonathan', 'Jim', 'Jennifer'] 
a_out4 = ['Alya', 'Renee', 'Natasha', 'Phil']

Example 2:

a = ['Abby', 'Robin', 'Natasha', 'Frank', 'Ana', 'Jennifer', 'Elizabeth', 'Tanya', 'Jim', 'Will', 'Rob', 'Joe', 'Alexa', 'Roger', 'Adam', 'Paul', 'James', 'Kara', 'John', 'Jeff', 'Rick', 'Steve']

Expected output:

a_out1 = ['Elizabeth', 'Tanya', 'Jim', 'Will', 'Rob', 'Joe']
a_out2 = ['Abby', 'Robin', 'Natasha', 'Frank', 'Ana', 'Jennifer']
a_out3 = ['John', 'Jeff', 'Rick', 'Steve'] 
a_out4 = ['Alexa', 'Roger', 'Adam', 'Paul', 'James', 'Kara']

How to obtain such results using python lists?

Here's a script that does this:

v = [['Elisa', 'Liza', 'Izabela', 'Elisabeth', 'Elizabeth', 'Lisa', 'Lizzy', 'Isabella', 'Isabelle', 'Isabela', 'Liz'],
    ['Abbey', 'Abbie', 'Abigail', 'Abby', 'Gail'],
    ['Jonathan', 'Jon', 'John', 'Jonny', 'Johnny', 'Nathan'],
    ['Alejandra','Alexandra','Alexis','Alya','Alexa','Lexi','Allie','Ally']]

a = ['Jonathan', 'Jim', 'Jennifer', 'Alya', 'Renee', 'Natasha', 'Phil', 'Lisa', 'Joe', 'Ana', 'Paul', 'Gail', 'Roderick', 'Patricia']
# a = ['Abby', 'Robin', 'Natasha', 'Frank', 'Ana', 'Jennifer', 'Elizabeth', 'Tanya', 'Jim', 'Will', 'Rob', 'Joe', 'Alexa', 'Roger', 'Adam', 'Paul', 'James', 'Kara', 'John', 'Jeff', 'Rick', 'Steve']

which = {w:i for i in range(len(v)) for w in v[i]}
a_out = [[] for _ in v]
idx = 0

for w in a:
    idx = which.get(w,idx)
    a_out[idx].append(w)

Note that the sublists v[0], v[1], v[2], v[3] are your reference lists v1, v2, v3, v4. Similarly, the sublists of a_out are your desired outputs. For example, running the above and then for r in a_out: print(r) yields

['Lisa', 'Joe', 'Ana', 'Paul']
['Gail', 'Roderick', 'Patricia']
['Jonathan', 'Jim', 'Jennifer']
['Alya', 'Renee', 'Natasha', 'Phil']

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