52ky 发表于 2022-5-4 15:28:35

空格分隔列表中的字符串

<div>假设我有以下结构:</div><div>
</div><div>t = [['I will','take','care'],['I know','what','to','do']]</div><div>
</div><div>正如您在第一个列表中看到的,我有 'I will' 并希望它们分成两个元素 'I' 和 'will' ,结果是:</div><div>
</div><div>[['I', 'will', 'take', 'care'], ['I', 'know', 'what', 'to', 'do']]</div><div>
</div>快速脏算法如下: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()


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



页: [1]
查看完整版本: 空格分隔列表中的字符串