我可以移除一个列表中的部分元素,使用另一个列表中的元素。

huangapple go评论73阅读模式
英文:

How can I remove part of an element in a list using the element of another list

问题

list_A = ['I hate the world', 'I love my mother']
list_B = ['I hate', 'love my']

list_output = [x.replace(b, '').strip() for x in list_A for b in list_B if b in x]

print(list_output)
英文:

So I have list_A and list_B and I need to remove whats in list_B from list_A Example:

list_A = ['I hate the world', 'I love my mother']
list_B = ['I hate', 'love my']

output: list_output = ['the world', 'I mother']

I tried to do

list_A = ['I hate the world', 'I love my mother']

list_B = ['I hate', 'love my']

list_output = [x for x in list_A if x not in list_B] 


print(list_output)

I have also tried a few other things I found on the internet, but they all threw errors or just didn't do anything, and list comprehensions just seemed the most like what I was trying to achieve.

答案1

得分: 0

这是一种方法来做到这一点。您必须使用嵌套循环,以便您可以检查所有的组合,直到找到匹配项。请注意,我正在做的是保留额外的空格。您可以添加代码来从两个部分中删除这些空格。

list_A = ['I hate the world', 'I love my mother']
list_B = ['I hate', 'love my']

list_output = []
for phrase in list_A:
    for clause in list_B:
        if clause in phrase:
            i = phrase.find(clause)
            j = phrase[:i] + phrase[i+len(clause):]
            list_output.append(j)
            break
    else: # in case nothing was found
        list_output.append(phrase)

print(list_output)

输出:

[' the world', 'I  mother']
英文:

Here is one way to do it. You have to use nested loops so you can check all combinations until you find a match. Note that what I'm doing leaves the extra spaces in. You can add code to remove those from both halves.

list_A = ['I hate the world', 'I love my mother']
list_B = ['I hate', 'love my']

list_output = []
for phrase in list_A:
    for clause in list_B:
        if clause in phrase:
            i = phrase.find(clause)
            j = phrase[:i] + phrase[i+len(clause):]
            list_output.append(j)
            break
    else: # in case nothing was found
        list_output.append(phrase)

print(list_output)

Output:

[' the world', 'I  mother']

答案2

得分: 0

案例 - I

假设移除是逐元素进行的,即只需从list_A的第一个元素中移除list_B的第一个元素,然后从list_A的第二个元素中移除list_B的第二个元素,以此类推。使用正则表达式的简单解决方案如下:

[re.sub(f"{list_B[i]}\s*", "", list_A[i]) for i in range(len(list_A))]

案例 - II

必须检查并从list_A中的单个元素中移除list_B中的每个元素。代码如下:

for i in range(len(list_A)):
    for string_b in list_B:
        list_A[i] = re.sub(f"{string_b}\s*", "", list_A[i])

注意- 在*re.sub()函数中,模式末尾的“\s”用于移除额外的空格,以使输出的格式完全符合所需。

英文:

Using regular expressions, there are two cases with different answers-

Case - I

Assuming that the removal would be element-wise i.e. only the first element of list_B needs to be removed from list_A, then the second element of list_B needs to be removed from second element of list_A and so on. A simple solution using regular expressions would be-

[re.sub(f"{list_B[i]}\s*", "", list_A[i]) for i in range(len(list_A))]

Case - II

Each element in list_B must be checked and removed from a single element in list_A. The code would be-

for i in range(len(list_A)):
    for string_b in list_B:
        list_A[i] = re.sub(f"{string_b}\s*", "", list_A[i])

Note- the "\s*" at the end of the pattern in re.sub() helps to remove extra whitespaces to make the format of the output exactly as desired.

huangapple
  • 本文由 发表于 2023年6月16日 12:28:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定