用于python的嵌套生成器或迭代器

问题描述 投票:1回答:4

我有下一个清单:

texts = [['abcdD', 'asdfaD'], ['qerqD', 'asdfafdasD']]

我想从所有字符串的右边部分删除所有字符D

对于一个列表,我可以轻松地做到:

res1 = [el.strip('D') for el in texts[0]] # ['abcd', 'asdfa']

现在我正在为每个文本尝试相同的事情:

res2 = [el.strip('D') for text in texts for el in text]

但它返回一个列表(结合我的两个!):

['abcd', 'asdfa', 'qerq', 'asdfafdas']

当我需要下一个:

[['abcd', 'asdfa'], ['qerq', 'asdfafdas']]

怎么做对吗?

python string text generator iterable
4个回答
© www.soinside.com 2019 - 2024. All rights reserved.