如果以(字符)结尾,如何删除列表中的字符串字符?
How do I remove a character of strings in a list if it ends with (character)?
您好,我正在尝试删除字符“+”
>>> a = ['eggs+', 'I don't want to remove this ', 'foo', 'spam+', 'bar+']
>>> a = [i[:-1] for i in a if i.ends with('+')]
>>> a
['eggs', 'spam', 'bar']
>>>
为什么是"I don't want to remove this"之类的
被删除以及如何删除“+”
并留下其他一切,如
>>>['eggs', 'I don't want to remove this ', 'foo', 'spam', 'bar']
试试这个:
a = ['eggs+', 'I dont want to remove this ', 'foo', 'spam+', 'bar+']
a = [i[:-1] if i.endswith('+') else i for i in a]
a
['eggs', 'I dont want to remove this ', 'foo', 'spam', 'bar']
你有一些语法问题,if else 必须在迭代之前出现。
您好,我正在尝试删除字符“+”
>>> a = ['eggs+', 'I don't want to remove this ', 'foo', 'spam+', 'bar+']
>>> a = [i[:-1] for i in a if i.ends with('+')]
>>> a
['eggs', 'spam', 'bar']
>>>
为什么是"I don't want to remove this"之类的 被删除以及如何删除“+” 并留下其他一切,如
>>>['eggs', 'I don't want to remove this ', 'foo', 'spam', 'bar']
试试这个:
a = ['eggs+', 'I dont want to remove this ', 'foo', 'spam+', 'bar+']
a = [i[:-1] if i.endswith('+') else i for i in a]
a
['eggs', 'I dont want to remove this ', 'foo', 'spam', 'bar']
你有一些语法问题,if else 必须在迭代之前出现。