问题:合并两个列表并删除重复项,而不删除原始列表中的重复项

我需要合并两个列表,其中第二个列表将忽略第一个列表的任何重复项。..有点难以解释,所以让我展示一个代码看起来像什么,以及我想要什么的示例。

first_list = [1, 2, 2, 5]

second_list = [2, 5, 7, 9]

# The result of combining the two lists should result in this list:
resulting_list = [1, 2, 2, 5, 7, 9]

您会注意到结果具有第一个列表,包括其两个“ 2”值,但是second_list也具有附加的2和5值这一事实并未添加到第一个列表中。

通常,对于这样的事情,我会使用集合,但是first_list上的集合会清除它已经具有的重复值。所以我只是想知道什么是实现此所需组合的最佳/最快方法。

谢谢。

I have two lists that i need to combine where the second list has any duplicates of the first list ignored. .. A bit hard to explain, so let me show an example of what the code looks like, and what i want as a result.

first_list = [1, 2, 2, 5]

second_list = [2, 5, 7, 9]

# The result of combining the two lists should result in this list:
resulting_list = [1, 2, 2, 5, 7, 9]

You’ll notice that the result has the first list, including its two “2” values, but the fact that second_list also has an additional 2 and 5 value is not added to the first list.

Normally for something like this i would use sets, but a set on first_list would purge the duplicate values it already has. So i’m simply wondering what the best/fastest way to achieve this desired combination.

Thanks.


回答 0

您需要将第二个列表中不在第一个列表中的那些元素添加到第一个列表中-集是确定它们是哪些元素的最简单方法,如下所示:

first_list = [1, 2, 2, 5]
second_list = [2, 5, 7, 9]

in_first = set(first_list)
in_second = set(second_list)

in_second_but_not_in_first = in_second - in_first

result = first_list + list(in_second_but_not_in_first)
print(result)  # Prints [1, 2, 2, 5, 9, 7]

或者,如果您更喜欢单线8-)

print(first_list + list(set(second_list) - set(first_list)))

You need to append to the first list those elements of the second list that aren’t in the first – sets are the easiest way of determining which elements they are, like this:

first_list = [1, 2, 2, 5]
second_list = [2, 5, 7, 9]

in_first = set(first_list)
in_second = set(second_list)

in_second_but_not_in_first = in_second - in_first

result = first_list + list(in_second_but_not_in_first)
print(result)  # Prints [1, 2, 2, 5, 9, 7]

Or if you prefer one-liners 8-)

print(first_list + list(set(second_list) - set(first_list)))

回答 1

resulting_list = list(first_list)
resulting_list.extend(x for x in second_list if x not in resulting_list)
resulting_list = list(first_list)
resulting_list.extend(x for x in second_list if x not in resulting_list)

回答 2

您可以使用集:

first_list = [1, 2, 2, 5]
second_list = [2, 5, 7, 9]

resultList= list(set(first_list) | set(second_list))

print(resultList)
# Results in : resultList = [1,2,5,7,9]

You can use sets:

first_list = [1, 2, 2, 5]
second_list = [2, 5, 7, 9]

resultList= list(set(first_list) | set(second_list))

print(resultList)
# Results in : resultList = [1,2,5,7,9]

回答 3

如果使用numpy,则可以将其简化为一行代码:

a = [1,2,3,4,5,6,7]
b = [2,4,7,8,9,10,11,12]

sorted(np.unique(a+b))

>>> [1,2,3,4,5,6,7,8,9,10,11,12]

You can bring this down to one single line of code if you use numpy:

a = [1,2,3,4,5,6,7]
b = [2,4,7,8,9,10,11,12]

sorted(np.unique(a+b))

>>> [1,2,3,4,5,6,7,8,9,10,11,12]

回答 4

first_list = [1, 2, 2, 5]
second_list = [2, 5, 7, 9]

print( set( first_list + second_list ) )
first_list = [1, 2, 2, 5]
second_list = [2, 5, 7, 9]

print( set( first_list + second_list ) )

回答 5

resulting_list = first_list + [i for i in second_list if i not in first_list]
resulting_list = first_list + [i for i in second_list if i not in first_list]

回答 6

对我来说最简单的是:

first_list = [1, 2, 2, 5]
second_list = [2, 5, 7, 9]

merged_list = list(set(first_list+second_list))
print(merged_list)

#prints [1, 2, 5, 7, 9]

Simplest to me is:

first_list = [1, 2, 2, 5]
second_list = [2, 5, 7, 9]

merged_list = list(set(first_list+second_list))
print(merged_list)

#prints [1, 2, 5, 7, 9]

回答 7

您还可以结合RichieHindle和Ned Batchelder的响应,得到保留顺序的平均情况 O(m + n)算法:

first_list = [1, 2, 2, 5]
second_list = [2, 5, 7, 9]

fs = set(first_list)
resulting_list = first_list + [x for x in second_list if x not in fs]

assert(resulting_list == [1, 2, 2, 5, 7, 9])

请注意,x in s它的最坏情况复杂度为O(m),因此此代码的最坏情况复杂度仍为O(m * n)

You can also combine RichieHindle’s and Ned Batchelder’s responses for an average-case O(m+n) algorithm that preserves order:

first_list = [1, 2, 2, 5]
second_list = [2, 5, 7, 9]

fs = set(first_list)
resulting_list = first_list + [x for x in second_list if x not in fs]

assert(resulting_list == [1, 2, 2, 5, 7, 9])

Note that x in s has a worst-case complexity of O(m), so the worst-case complexity of this code is still O(m*n).


回答 8

这可能有帮助

def union(a,b):
    for e in b:
        if e not in a:
            a.append(e)

union函数将第二个列表合并为第一个列表,而不复制a的元素(如果已经存在于a中)。与设置联合运算符相似。此功能不变b。如果a = [1,2,3] b = [2,3,4]。在union(a,b)使a = [1,2,3,4]和b = [2,3,4]之后

This might help

def union(a,b):
    for e in b:
        if e not in a:
            a.append(e)

The union function merges the second list into first, with out duplicating an element of a, if it’s already in a. Similar to set union operator. This function does not change b. If a=[1,2,3] b=[2,3,4]. After union(a,b) makes a=[1,2,3,4] and b=[2,3,4]


回答 9

根据配方

result_list = list(set()。union(first_list,second_list))

Based on the recipe :

resulting_list = list(set().union(first_list, second_list))


回答 10

    first_list = [1, 2, 2, 5]
    second_list = [2, 5, 7, 9]

    newList=[]
    for i in first_list:
        newList.append(i)
    for z in second_list:
        if z not in newList:
            newList.append(z)
    newList.sort()
    print newList

[1、2、2、5、7、9]

    first_list = [1, 2, 2, 5]
    second_list = [2, 5, 7, 9]

    newList=[]
    for i in first_list:
        newList.append(i)
    for z in second_list:
        if z not in newList:
            newList.append(z)
    newList.sort()
    print newList

[1, 2, 2, 5, 7, 9]


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。