Check list overlap with list comprehensions

Moussa Kare Gueye

I have a script in python that generate 2 random lists with different sizes and return a third list that contains only the elements that are common between the 2 lists (without duplicates) using list comprehensions

Example:

a = [3, 8, 9, 6, 5, 3, 7, 8, 2, 10]
b = [7, 13, 20, 12, 12, 2, 6, 1, 2, 8, 19, 3, 15, 16, 14, 22, 22, 4, 9, 15, 8, 13]

My result list is

c = [7, 2, 6, 2, 8, 3, 9, 8]

But it should be

c = [7, 6, 2, 8, 3, 9]

Here is what I've done:

c = [i for i in max(a, b) if i in min(a, b) and i not in c]

Thanks in advance!

David S

You could use sets in the following way:

c = list(set(a).intersection(set(b)))

This will give you:

[2, 3, 6, 7, 8, 9]

This works because set items are unordered, unchangeable, and do not allow duplicate values. Combine that with the intersection method you will get the result.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Erlang list comprehensions strange behaviour

List Comprehensions break inner loop

Python nesting list comprehensions with variable depth

Haskell : List Comprehensions 및 고차 함수

Python 2.7 Grammar Geek-List Comprehensions의 Lambda

Python List Comprehensions, 하위 목록 행 및 열

Check if values in a list exisiting in another list

Check whether a list starts with the elements of another list

List Comprehensions가 내부 루프를 끊습니다.

Python List Comprehensions를 사용하여 목록에서 항목 제거

list comprehensions에서 필터와 함께 groupby () 사용

Haskell list comprehensions에서 Maybe 타입을 다루는 방법

Python Script는 많은 시간이 소요됩니다. / List comprehensions

List Comprehensions, Recursion 및 delete 함수와 관련된 Haskell의 순열

Python List Comprehensions 創建一個理解編程的能力列表

Check empty vector in initializer list

List of servers to check for SSH availability

ansible check max number list

python list comprehensions에서 누락 된 값에 대한 깨끗한 솔루션

List comprehensions (Python)를 사용하여 목록 목록에서 요소 삭제

python list comprehensions에서 누락 된 값에 대한 깨끗한 솔루션

List comprehensions Python을 사용하여 Numpy 배열 값을 datetime형 값으로 변환

Check for placement overlap on large grid

check for multiple substring match in a list while iterating list

Syntax check ~/.config/mimeapps.list

How exactly does Python check through a list?

How to check all checkboxs in list in android

Check a List of a class for a specific variable with LINQ

Prolog: how to check if a word is contained in a list

TOP 리스트

  1. 1

    셀레늄의 모델 대화 상자에서 텍스트를 추출하는 방법은 무엇입니까?

  2. 2

    Blazor 0.9.0 및 ASP.NET Core 3 미리보기 4를 사용한 JWT 인증

  3. 3

    openCV python을 사용하여 텍스트 문서에서 워터 마크를 제거하는 방법은 무엇입니까?

  4. 4

    C # 16 진수 값 0x12는 잘못된 문자입니다.

  5. 5

    Excel : 합계가 N보다 크거나 같은 상위 값 찾기

  6. 6

    오류 : MSB4803 : MSBuild의 .NET Core 버전에서 "ResolveComReference"작업이 지원되지 않습니다.

  7. 7

    R에서 Excel로 내보낼 때 CET / CEST 시간 이동이 삭제됨

  8. 8

    node.js + postgres : "$ 1"또는 그 근처에서 구문 오류

  9. 9

    확대 후 하이 차트에서 Y 축이 잘못 정렬 됨

  10. 10

    EPPlus에서 행 높이를 설정할 때 이상한 동작

  11. 11

    Ionic 2 로더가 적시에 표시되지 않음

  12. 12

    MS Access 부분 일치 2 테이블

  13. 13

    EPPlus에서 병합 된 셀의 행 높이 자동 맞춤

  14. 14

    ExecuteNonQuery- 연결 속성이 초기화되지 않았습니다.

  15. 15

    ResponseEntity를 사용하고 InputStream이 닫히는 지 확인하는 적절한 스트리밍 방법

  16. 16

    PrematureCloseException : 연결이 너무 일찍 닫혔습니다.

  17. 17

    오류 : "const wchar_t *"유형의 인수가 "WCHAR *"유형의 매개 변수와 호환되지 않습니다.

  18. 18

    Java에서 이미지를 2 색으로 변환

  19. 19

    overflow-y를 사용할 때 스크롤 버벅 거림 줄이기 : scroll;

  20. 20

    Java에서 Apache POI를 사용하여 테이블 크기 및 간격을 단어로 설정하는 방법

  21. 21

    Android Kotlin은 다른 활동에서 함수를 호출합니다.

뜨겁다태그

보관