如何在python中从列表中删除包含特定字符的字符串

2024-10-02 00:29:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了一个不同字符串的列表。他们中的许多人在f.e.“X”中有一个特殊的角色。如何在python中从列表中删除获取特定字符的字符串

f.e。 ['ABCXDU'、'ZUTRC'、'AZUX'、'ABNIU'] 我想要的输出-->;该列表的字符串中没有x


Tags: 字符串gt角色列表字符abniuzutrcazux
3条回答

使用

[x for x in some_list if "X" not in x]

创建一个新的列表,其中只包含那些没有X的内容

new_list = [s for s in your_input_list if 'X' not in s]

或者你可以这样做:

new_list = list(filter(lambda s: 'X' not in s, your_input_list))

您可以使用列表理解

new_list = [item for item in my_list if "X" not in item]

相关问题 更多 >

    热门问题