简体   繁体   English

子串过滤器列表元素由Python中的另一个列表

[英]Substring filter list elements by another list in Python

I have two lists looking like: 我有两个列表看起来像:

list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
list2 = [100, 200]

I want to substring filter list1 by elements of list2 and get expected output as follows: 我想通过list2的元素对list1进行子字符串过滤,并得到预期的输出,如下所示:

outcome = ['bj-100-cy', 'sh-200-pd']

When doing: 做的时候:

list1 = str(list1)
list2 = str(list2)
outcome = [x for x in list2 if [y for y in list1 if x in y]]

I get a result like this: ['[', '1', '0', '0', ',', ' ', '2', '0', '0', ']'] . 我得到这样的结果: ['[', '1', '0', '0', ',', ' ', '2', '0', '0', ']'] How can I filter it correctly? 我该如何正确过滤? Thanks. 谢谢。

Reference related: 参考相关:

Is it possible to filter list of substrings by another list of strings in Python? 是否可以通过Python中的另一个字符串列表过滤子字符串列表?

List comprehension and any : 列表理解和any

[i for i in list1 if any(i for j in list2 if str(j) in i)]

any to check if any element of list2 is a substring of the list1 item ( __contains__ ) being iterated over. any检查list2任何元素是否是迭代的list1项( __contains__ )的子字符串。

Example: 例:

In [92]: list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
    ...: list2 = [100, 200]
    ...: 

In [93]: [i for i in list1 if any(i for j in list2 if str(j) in i)]
Out[93]: ['bj-100-cy', 'sh-200-pd']

You can use any : 你可以使用any

list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
list2 = [100, 200]
list2 = [str(x) for x in list2]

outcome = [s for s in list1 if any(x in s for x in list2)]

any returns True if any of the conditions you give it are True . any如果您给出的任何条件为True则返回True

list1 = str(list1)
list2 = str(list2)

You are converting your list into a string with the above statements. 您正在将列表转换为包含上述语句的字符串。 So when you iterate in a for loop, you are iterating each characters, instead of each word. 因此,当您在for循环中进行迭代时,您将迭代每个字符,而不是每个字。

So you should remove string conversion and instead do a list comprehension as follows. 因此,您应该删除字符串转换,而是按如下方式执行列表推导。 Also, in your outcome file instead of checking if the word in list2 is in list1, you are checking the opposite. 此外,在结果文件中,而不是检查list2中的单词是否在list1中,您正在检查相反的情况。 So you got like 100 and 200 as chars which are in list 2. 所以你得到100和200作为列表2中的字符。

list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
list2 = [100, 200]
outcome = [x for x in list1 for y in list2 if str(y) in x]

You can try this one: 你可以尝试这个:

list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
list2 = [100, 200]

outcome = []
for item in list1:
    if any(str(i) in item for i in list2):
        outcome.append(item)

output: 输出:

['bj-100-cy', 'sh-200-pd']

Another alternative list comprehension : 另一个替代列表理解:

>>> list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
>>> list2 = [100, 200]
>>> occur = [i for i in list1  for j in list2 if str(j) in i]
>>> occur
['bj-100-cy', 'sh-200-pd']

You can use builtin filter method to filter the list based on your condition. 您可以使用内置过滤器方法根据您的条件过滤列表。 Your condition requires python in operator to search for needle ([100, 200]) in haystack ([['bj-100-cy','bj-101-hd',...]]) . 你的情况需要Python in操作符来搜索针([100, 200])在草堆([['bj-100-cy','bj-101-hd',...]]) We can use contains method to simplify search syntax. 我们可以使用contains方法来简化搜索语法。

Code

from operator import contains
filter(lambda x: any(contains(x,str(y)) for y in list2), list1)

Example

>>> list1 = ['bj-100-cy','bj-101-hd','sh-200-pd','sh-201-hp']
>>> list2 = [100, 200]
>>> for item in filter(lambda x: any(contains(x,str(y)) for y in list2), list1):
...     print(item)
...
bj-100-cy
sh-200-pd

You can use regex: 你可以使用正则表达式:

import re

list1 = ['bj-100-cy', 'bj-101-hd', 'sh-200-pd', 'sh-201-hp']
list2 = [100, 200]

pattern = re.compile('|'.join(map(str, list2)))
list(filter(pattern.search, list1))
# ['bj-100-cy', 'sh-200-pd']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据列表元素是否包含 Python 中另一个列表中的 substring 来过滤掉列表元素 - How to filter out list elements based on if they contain a substring from another list in Python 根据python中的另一个列表匹配列表的元素,其中一个列表的元素是另一个列表的子字符串 - match element of a list based on another list in python where elements of one list is the substring of elements of another list 如何基于python中的另一个列表过滤列表中的列表元素? - How to filter the list elements within list based on another list in python? python - 按第二个列表的 substring 过滤列表 - python - filter list by substring of second list 尝试根据元素是否在 Python 中的另一个列表中来过滤一个列表 - Trying to filter one list according to whether elements are in another list in Python 迭代一个 Python 列表并根据另一个列表过滤元素 - Iterate a Python list and filter the elements based on another list 如何基于python中另一个列表中的元素过滤列表 - How to filter a list based on elements in another list in python 使用 python 从另一个列表中过滤列表中的元素 - filter elements from list from another list using python Python中列表的过滤器元素 - Filter elements of a list in Python 在Python中使用列表元素过滤列表 - Filter a List in Python With List Elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM