2024-03-28 14:41:28 发布
网友
我有两张单子。在
x=['billed_qty','billed_amt','sale_value'] y=['george billed_qty', 'sam billed_amt', 'ricky sale_value', 'donald billed_qty']
我需要消除列表y中出现在列表x中的单词,并希望结果列表为:
我怎样才能做到这一点?在
谢谢
将regex与list comprehension一起使用:
regex
list comprehension
comp = re.compile('|'.join(x)) z = [re.sub(comp, '', i).strip() for i in y] print(z) ['george','sam','ricky','donald']
在列表理解中使用str.join和str.split:
str.join
str.split
z = [' '.join(w for w in s.split() if w not in x) for s in y] print(z)
输出:
为什么不:
print([' '.join(set(i.split()).difference(set(x))) for i in y])
将
regex
与list comprehension
一起使用:在列表理解中使用
str.join
和str.split
:输出:
^{pr2}$为什么不:
输出:
^{pr2}$相关问题 更多 >
编程相关推荐