I am trying to remove specific characters from items in a list, using another list as a reference. Currently I have:
我试图从列表中的项目中删除特定字符,使用另一个列表作为参考。目前我有:
forbiddenList = ["a", "i"]
tempList = ["this", "is", "a", "test"]
sentenceList = [s.replace(items.forbiddenList, '') for s in tempList]
print(sentenceList)
which I hoped would print:
我希望打印出来:
["ths", "s", "test"]
of course, the forbidden list is quite small and I could replace each individually, but I would like to know how to do this "properly" for when I have an extensive list of "forbidden" items.
当然,禁止列表非常小,我可以单独替换每个,但我想知道当我有一个“禁止”项目的广泛列表时,“正确”如何做到这一点。
2 个解决方案
#1
You could use a nested list comprehension.
您可以使用嵌套列表解析。
>>> [''.join(j for j in i if j not in forbiddenList) for i in tempList]
['ths', 's', '', 'test']
It seems like you also want to remove elements if they become empty (as in, all of their characters were in forbiddenList
)? If so, you can wrap the whole thing in even another list comp (at the expense of readability)
看起来你也想删除元素,如果它们变空(例如,所有的字符都在forbiddenList中)?如果是这样,你可以将整个东西包装在另一个列表中(以可读性为代价)
>>> [s for s in [''.join(j for j in i if j not in forbiddenList) for i in tempList] if s]
['ths', 's', 'test']
#2
>>> templist = ['this', 'is', 'a', 'test']
>>> forbiddenlist = ['a', 'i']
>>> trans = str.maketrans('', '', ''.join(forbiddenlist))
>>> [w for w in (w.translate(trans) for w in templist) if w]
['ths', 's', 'test']
This is a Python 3 solution using str.translate
and str.maketrans
. It should be fast.
这是使用str.translate和str.maketrans的Python 3解决方案。它应该很快。
You can also do this in Python 2, but the interface for str.translate
is slightly different:
您也可以在Python 2中执行此操作,但str.translate的界面略有不同:
>>> templist = ['this', 'is', 'a', 'test']
>>> forbiddenlist = ['a', 'i']
>>> [w for w in (w.translate(None, ''.join(forbiddenlist))
... for w in templist) if w]
['ths', 's', 'test']
#1
You could use a nested list comprehension.
您可以使用嵌套列表解析。
>>> [''.join(j for j in i if j not in forbiddenList) for i in tempList]
['ths', 's', '', 'test']
It seems like you also want to remove elements if they become empty (as in, all of their characters were in forbiddenList
)? If so, you can wrap the whole thing in even another list comp (at the expense of readability)
看起来你也想删除元素,如果它们变空(例如,所有的字符都在forbiddenList中)?如果是这样,你可以将整个东西包装在另一个列表中(以可读性为代价)
>>> [s for s in [''.join(j for j in i if j not in forbiddenList) for i in tempList] if s]
['ths', 's', 'test']
#2
>>> templist = ['this', 'is', 'a', 'test']
>>> forbiddenlist = ['a', 'i']
>>> trans = str.maketrans('', '', ''.join(forbiddenlist))
>>> [w for w in (w.translate(trans) for w in templist) if w]
['ths', 's', 'test']
This is a Python 3 solution using str.translate
and str.maketrans
. It should be fast.
这是使用str.translate和str.maketrans的Python 3解决方案。它应该很快。
You can also do this in Python 2, but the interface for str.translate
is slightly different:
您也可以在Python 2中执行此操作,但str.translate的界面略有不同:
>>> templist = ['this', 'is', 'a', 'test']
>>> forbiddenlist = ['a', 'i']
>>> [w for w in (w.translate(None, ''.join(forbiddenlist))
... for w in templist) if w]
['ths', 's', 'test']