简体   繁体   中英

Splitting strings inside a list of lists by spaces

Suppose I have the following structure:

t = [['I will','take','care'],['I know','what','to','do']]

As you see in the first list I have 'I will' and I want them to split into two elements 'I' and 'will' , st the result is:

[['I', 'will', 'take', 'care'], ['I', 'know', 'what', 'to', 'do']]

Quick and dirty algorithm is the following:

train_text_new = []


for sent in t:
  new = []
  for word in sent:
    temp = word.split(' ')
    for item2 in temp:
      new.append(item2)


  train_text_new.append(new)

But I would like to know if there is a more readable and maybe more efficient algorithm for solving this problem.

You could make a simple generator that yields the splits, then use that in a list comprehension:

t = [['I will','take','care'],['I know','what','to','do']]

def splitWords(l):
    for words in l:
        yield from words.split()

[list(splitWords(sublist)) for sublist in t]
# [['I', 'will', 'take', 'care'], ['I', 'know', 'what', 'you', 'to', 'do']]

You can try this. Assuming splitting always happens to the first element of the sublist

t = [['I will','take','care'],['I know','what','to','do']]
[start.split()+rest for start,*rest in t]
# [['I', 'will', 'take', 'care'], ['I', 'know', 'what', 'to', 'do']]

If splitting should happen to any word in sublist try this.

[[j for i in lst for j in i.split()]for lst in t]
# [['I', 'will', 'take', 'care'], ['I', 'know', 'what', 'to', 'do']]

joining every inner list to a string usin join and splitting that string using split to list will do the trick

t = [['I will','take','care'],['I know','what','to','do']]
res = [' '.join(i).split() for i in t]
print(res)
# output [['I', 'will', 'take', 'care'], ['I', 'know', 'what', 'to', 'do']]

You could use itertools.chain.from_iterable to do the flattening after splitting:

from itertools import chain

t = [['I will','take','care'],['I know','what','to','do']]

print([list(chain.from_iterable(x.split() for x in y)) for y in t])

Output:

[['I', 'will', 'take', 'care'], ['I', 'know', 'what', 'to', 'do']]

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