【发布时间】:2019-08-03 18:00:59
【问题描述】:
我有两个类似的列表:
list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
list2 = [100, 200]
我想通过list2 的元素对list1 进行子串过滤,并获得如下预期输出:
outcome = ['bj-100-cy', 'sh-200-pd']
做的时候:
list1 = str(list1)
list2 = str(list2)
outcome = [x for x in list2 if [y for y in list1 if x in y]]
我得到这样的结果:['[', '1', '0', '0', ',', ' ', '2', '0', '0', ']']。
我怎样才能正确过滤它?谢谢。
参考相关:
Is it possible to filter list of substrings by another list of strings in Python?
【问题讨论】:
标签: python string list filter list-comprehension