developer tip

원본 목록에서 중복을 제거하지 않고 두 목록을 결합하고 중복 제거

copycodes 2020. 9. 20. 10:04
반응형

원본 목록에서 중복을 제거하지 않고 두 목록을 결합하고 중복 제거


두 번째 목록에 첫 번째 목록의 중복이 무시되는 위치를 결합해야하는 두 개의 목록이 있습니다. .. 설명하기가 조금 어렵습니다. 코드가 어떻게 생겼는지, 그 결과 제가 원하는 것을 보여 드리겠습니다.

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의 세트는 이미 가지고있는 중복 값을 제거합니다. 그래서 저는이 원하는 조합을 달성하는 가장 좋은 / 가장 빠른 방법이 무엇인지 궁금합니다.

감사.


첫 번째 목록에 포함되지 않은 두 번째 목록의 요소를 첫 번째 목록에 추가해야합니다. 집합은 다음과 같이 요소가 무엇인지 결정하는 가장 쉬운 방법입니다.

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))

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

세트를 사용할 수 있습니다.

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]

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

print( set( first_list + second_list ) )

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]

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

나에게 가장 간단한 것은 :

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]

이것은 도움이 될 수 있습니다

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]


레시피 에 따라 :

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


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).


    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]


L1 = [1,2,3,3,4,4]
L2 = [3,4,5,6,6,6]
L1.extend(L2)
L3 =[]
[L3.append(num) for num in L1 if num not in L3]
print L3
[1, 2, 3, 4, 5, 6]
[Finished in 0.5s]

simply like belove :

resulting_list = list(set(resulting_list))


list1 = [1, 2, 1]
list2 = [2, 3, 4, 3]

def extendList(list1, list2):
   return list(set(list1+list2))

list12 = extendList(list1, list2)
print(list12)

Output: [1, 2, 3, 4]

참고URL : https://stackoverflow.com/questions/1319338/combining-two-lists-and-removing-duplicates-without-removing-duplicates-in-orig

반응형