[英]Splitting strings inside a list of lists by spaces
假設我有以下結構:
t = [['I will','take','care'],['I know','what','to','do']]
正如您在第一個列表中看到的那樣,我有'I will'
並且我希望它們分成兩個元素'I'
和'will'
,結果是:
[['I', 'will', 'take', 'care'], ['I', 'know', 'what', 'to', 'do']]
快速而骯臟的算法如下:
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)
但我想知道是否有更易讀、可能更有效的算法來解決這個問題。
您可以制作一個簡單的生成器來產生拆分,然后在列表理解中使用它:
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']]
你可以試試這個。 假設拆分總是發生在子列表的第一個元素上
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']]
如果拆分應該發生在子列表中的任何單詞上,試試這個。
[[j for i in lst for j in i.split()]for lst in t]
# [['I', 'will', 'take', 'care'], ['I', 'know', 'what', 'to', 'do']]
使用join
將每個內部列表連接到一個字符串並使用split
to list 拆分該字符串就可以了
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']]
您可以使用itertools.chain.from_iterable
在拆分后進行展平:
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']]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.