【问题标题】:How to check if element(b) in list contains an element (a) from the same list. than remove the element (a)如何检查列表中的元素(b)是否包含同一列表中的元素(a)。比删除元素 (a)
【发布时间】:2021-01-25 22:36:42
【问题描述】:

如果元素在另一个元素中,我需要删除它

    def c_counter_check(new_filters):         #new filters is an list
        for i in new_filters_splited[:]:
            index = new_filters_splited.index(i) #get index of an i
            mylist = new_filters_splited[:index] + new_filters_splited[index + 1:] # create a compare list without i
            for a in mylist[:]:
                if a.__contains__(i):    #check if an element from mylist contains an i
                    new_filters_splited.remove(i)      #if yes than remove i from new_filters_splited
        return new_filters_splited

如果我的列表是['a', 'ab', 'ac', 'ab', 'abac'],我需要删除'a' and 'ab' and 'abac' and 'ac',结果应该只是['a']

【问题讨论】:

  • 您对['a', 'ab', 'ac', 'ab'] 的输入是['a', 'ab'] 吗?使用输入从列表中删除元素?
  • 如果ab 在查找列表中,您是要从源列表中仅删除第一次出现的ab 还是删除所有出现的ab?
  • 请重复how to ask 和intro tour。您的描述不清楚:您没有指定任务。您没有指定当前的问题。
  • 请提供预期的minimal, reproducible example。显示中间结果与您的预期不同的地方。我们应该能够复制和粘贴您的代码的连续块,执行该文件,并重现您的问题以及跟踪问题点的输出。这让我们可以根据您的测试数据和所需的输出来测试我们的建议。
  • @JoeFerndz 只是其中之一

标签: python list loops for-loop nested-loops


【解决方案1】:

仅从 input_list 中删除第一次出现的查找值

如果我们只想从查找中删除第一次出现的项目,那么我们需要维护查找列表。第一次在查找列表中找到项目时,我们将其从 output_list 中排除。此外,我们还应该从查找列表中删除该项目。这样,如果有其他相同的值出现,它可以填充到 output_list 中。

为此,必须对 filter_list 函数进行一些修改。

filter_list 函数的更新版本将是:

def filter_list (input_list, lookup_items):
    
    output_list = []               #initialize the output_list
    
    for a in input_list:           #iterate thru the input_list
        if a in lookup_items:      #check if value in input_list is part of lookup_items
            lookup_items.remove(a) #remove it from lookup_items and don't append to output_list
        else:
            output_list.append(a)  #append to output list
    
    return output_list             #return the output_list that meets our criteria

现在让我们测试一下:

print ("Output List  : ", filter_list(['a', 'ab', 'ac', 'ab'], ['a','ab']))

输出将是:

Output List  :  ['ac', 'ab']

如您所见,ab 的第二次出现被保留并传回。

另一个测试:

print ("Output List  : ", filter_list(['x', 'xy', 'xyz', 'yz'], ['x','yz']))

输出将是:

Output List  :  ['xy', 'xyz']

使用带参数的函数调用的新答案

如果您不确定输入列表和查找项是什么,那么您可以定义一个函数并使用 input_list 和 lookup_list 调用该函数。假设在您需要进行过滤操作之前,您将拥有这两个值。

在下面列出的函数中,我们将发送 input_list 和 lookup_list。这将过滤掉 lookup_list 中的所有元素,只返回不在列表中的元素:

def filter_list (input_list, lookup_items):

    return [a for a in input_list if a not in lookup_items]

让我们测试一下。

print ("Output List  : ", filter_list(['a', 'ab', 'ac', 'ab'], ['a','ab']))

为此,我发送以下数据:

print ("Input List   : ['a', 'ab', 'ac', 'ab']")
print ("Lookup Items : ['a','ab']")

Input List   : ['a', 'ab', 'ac', 'ab']
Lookup Items : ['a','ab']

输出应该是['ac']

正如预期的那样,输出是:

Output List  :  ['ac']

让我们运行另一个测试:

print ("Output List  : ", filter_list(['x', 'xy', 'xyz', 'yz'], ['x','yz']))

预期的输出是:

Output List  :  ['xy', 'xyz']

从列表中排除项目的上一个答案

以下是使用查找删除所有项目的方法。

input_list = ['a', 'ab', 'ac', 'ab']
lookup_items = ['a', 'ab']

output_list = [a for a in input_list if a not in lookup_items]

print ('Input List   : ', input_list)
print ('Lookup Items : ', lookup_items)
print ('Output List  : ', output_list)

这是一个列表推导。展开后的代码如下:

#output_list = [a for a in input_list if a not in lookup_items]

#The above line is a list comprehension. It can be translated to a for loop as follows:

output_list = []
for a in input_list:
    if a not in lookup_items:
        output_list.append(a)

在这里,我将删除 lookup_items 列表中的所有项目。一旦您明确是否只需要删除第一项,我就可以修改代码以显示这一点。

这个输出将是:

Input List   :  ['a', 'ab', 'ac', 'ab']
Lookup Items :  ['a', 'ab']
Output List  :  ['ac']

【讨论】:

  • 我不能用这个,因为我不知道查找或输入列表,它们几乎是随机的
  • 你的输入列表是发送给函数的随机列表吗?而且您的查找列表也是发送到您的函数的随机列表?
  • 我不知道查找列表,输入列表是随机的
【解决方案2】:
def deduplicate(list_to_deduplicate):
    elems = set()
    rest = []
    for el in list_to_deduplicate:
        if el not in elems:
            elems.add(el)
            rest.append(el)
    return rest

def c_counter_check(list_to_filter):
    res = []
    lst_s = deduplicate(list_to_filter)
    for index, attribute in enumerate(lst_s):
        mylist = lst_s[:index] + lst_s[index + 1:]
        global checker
        checker = 0
        if any(attribute in b for b in mylist):
            res.append(attribute)
            for b in mylist:
                if attribute.__contains__(b):
                    res.remove(attribute)
                    break
        else:
            if not any(attribute in r for r in res):
                res.append(attribute)
                if any(b in attribute for b in mylist):
                    res.remove(attribute)
    return res

这是完整的工作解决方案 感谢来自 discord 的 @ConfusedReptile 创建 deduplicate 函数

【讨论】:

    猜你喜欢
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 2018-05-09
    • 2021-02-18
    • 2021-12-13
    相关资源
    最近更新 更多