简体   繁体   中英

How can I split nested list at a substring?

a = [['dog===frog', 'cat===dog'], ['bird===bat', 'ball===call']]

Where len(a) can be as large as needed and len(a[I]) can be as large as needed..

how can I get out b = [['dog','frog','cat','dog'],['bird','bat','ball','call']] ?

I have tried somethings along the line of

[' '.join(x).split('===') for x in new_list]

and just general list comprehension with .join, but have had no luck.

b = [sum([x.split('===') for x in sublist], []) for sublist in a]

should give you what you want. Works like this:

  • split('===') makes list from every string
  • you then add those using sum: sum([['dog', 'frog'], ['cat', 'dog']], []) is basically ['dog', 'frog'] + ['cat', 'dog']
  • sum([x.split('===') for x in sublist], []) uses list comprehension to make a split list from all pieces of small list ( ['dog===frog', 'cat===dog'] ), which is the fed to sum
  • and it's all wrapped in another comprehension that runs it for every part of your big list a

You could use a nested list comprehension :

a = [['dog===frog', 'cat===dog'], ['bird===bat', 'ball===call']]
result = [[chunk for chunks in map(lambda e: e.split('='), sub) for chunk in chunks if chunk] for sub in a]
print(result)

Output

[['dog', 'frog', 'cat', 'dog'], ['bird', 'bat', 'ball', 'call']]

You can use chain.from_iterable to flatten the results of splitting the strings in a list into a single list

from itertools import chain

[list(chain.from_iterable(s.split('===') for s in sub)) for sub in a]
# [['dog', 'frog', 'cat', 'dog'], ['bird', 'bat', 'ball', 'call']]

这是一个使用列表推导的单行代码。

[[word for element in sublist for word in element.split('===')] for sublist in a]

import numpy as np
a = [['dog===frog', 'cat===dog'], ['bird===bat', 'ball===call']]
a = [ i.split('===') for i in np.array(a).ravel()]

Output:

[['dog', 'frog'], ['cat', 'dog'], ['bird', 'bat'], ['ball', 'call']]
def flatten(seq):
    """list -> list                                                                                                                                                                           
    return a flattend list from an abitrarily nested list                                                                                                                                     
    """
    if not seq:
        return seq
    if not isinstance(seq[0], list):
        return [seq[0]] + flatten(seq[1:])
    return flatten(seq[0]) + flatten(seq[1:])

b=[[j.split("===") for j in i] for i in a]
c=[flatten(i) for i in b]
c
    [['dog', 'frog', 'cat', 'dog'], ['bird', 'bat', 'ball', 'call']]

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