我有两个列表,如下所示:
list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
list2 = [100, 200]
我想要子字符串过滤器list1
通过元素list2
并得到预期输出如下:
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', ']']
。
如何才能正确过滤呢?谢谢。
相关参考:
是否可以通过Python中的另一个字符串列表来过滤子字符串列表? https://stackoverflow.com/questions/46028725/is-it-possible-to-filter-list-of-substrings-by-another-list-of-strings-in-python