我正在尝试使用另一个列表作为引用,从列表中的项目中删除特定字符。目前我有:
forbiddenList = ["a", "i"]
tempList = ["this", "is", "a", "test"]
sentenceList = [s.replace(items.forbiddenList, '') for s in tempList]
print(sentenceList)
我希望能打印出来:
["ths", "s", "test"]
当然,禁地名单是相当小的,我可以取代每一个单独,但我想知道如何做到这一点“适当”的时候,我有一个广泛的“禁地”项目清单。你知道吗
这是一个使用^{} 和^{} 的python3解决方案。应该很快。你知道吗
您也可以在Python 2中执行此操作,但是^{} 的接口略有不同:
可以使用嵌套列表。你知道吗
如果元素变为空,您似乎还想删除它们(例如,它们的所有字符都在
forbiddenList
中)?如果是这样的话,你甚至可以用另一个列表来包装整个内容(以牺牲可读性为代价)相关问题 更多 >
编程相关推荐