我有一个列表列表:
batches = [['der\n', 'und\n', 'die\n', 'des\n', 'mit\n', 'in\n', 'nach\n', 'eine\n', 'wird\n', 'auf\n'], ['bei\n', 'im\n', 'den\n', 'das\n', 'von\n', 'einer\n', 'sich\n', 'dem\n', 'ist\n', 'über\n']
等有10个列表的1个列表,每个列表包含10个字符串/令牌(我刚刚提供了一个示例)。
我还有另一个包含一些名词的列表,如果要与名词列表中的字符串匹配,我想批量更改任何字符串。但是我希望批次的结构保持不变,该怎么办?
for x in batches:
for y in x:
if y.rstrip() in noun_list:
y += '- this is a noun'
但是这不会改变任何东西。我还尝试了列表理解:
[''.join(y + '- this is a noun') if y.rstrip() in noun_list else [''.join(y)] for x in batches for y in x]
但是结构改变了。
Python中的字符串是不可变的。您需要替换列表元素,而不是更新临时变量y
。因此,您需要使用enumerate()
:
enumerate()