How can I compare elements in 2 lists in the same index in their respective lists in Python?

nddragoon

Say I have 2 lists

a = [1, 2, 3, 4, 7, 1]
b = [1, 2, 4, 5, 7, 1]

I want it to make a third list with only the elements that are equal in the same index in each list, in this case

c = [1, 2, 7, 1]

Is there a simple way to do this?

Derek Eden

simplest method would probably just be doing a list comprehension:

c = [x for x,y in zip(a,b) if x == y]

for the sake of a different approach and just out of interest..could also do it this way:

from itertools import compress
mask = [x==y for x,y in zip(a,b)]
c = list(compress(a,mask))

or if numpy is an option (good for larger lists):

import numpy as np
a,b = np.array(a), np.array(b)
c = a[np.equal(a,b)].tolist()

all ways give:

[1, 2, 7, 1]

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

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

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to compare the index of 2 sublists' elements from a list of lists

분류에서Dev

How to compare elements of two input lists?

분류에서Dev

How to compare properties of a subitem in 2 lists in linq

분류에서Dev

Groovy compare two lists to find lists with common elements

분류에서Dev

Joining 2 lists ,Python

분류에서Dev

How to add repeating occurences of elements in two lists in python

분류에서Dev

How to add repeating occurences of elements in two lists in python

분류에서Dev

Compare 2 linked lists using loop

분류에서Dev

How to compare Lists of Guids in C#

분류에서Dev

How can I copy and combine several .txt lists in alphabetical order?

분류에서Dev

Python Defining Lists in Lists

분류에서Dev

Comparing of the 2 lists of different objects but with a same fields

분류에서Dev

How to split elements of the group in many lists?

분류에서Dev

Python: How to create 2D array using existing lists

분류에서Dev

Nested Lists and their index calls

분류에서Dev

Python - XY array from 2 lists

분류에서Dev

How can I disable values in drop down lists based on user selection using JavaScript

분류에서Dev

Oddity in how Python3 handles lists

분류에서Dev

Python - How to delete the last element in a list of lists?

분류에서Dev

assigning lists elements as function arguments

분류에서Dev

TortoiseSVN: Where can I view kept 'Change Lists'?

분류에서Dev

Merge 2 lists of lists, element by element

분류에서Dev

How can I treat double quoted data and single quoted data same in Python 2.x?

분류에서Dev

Python adding lists of numbers with other lists of numbers

분류에서Dev

Multiple lists in the same navigation bar

분류에서Dev

Adding tuples in lists in Python

분류에서Dev

Python list to list of lists

분류에서Dev

remove elements from a data.frame with lists of lists

분류에서Dev

How to quickly convert from items in a list of lists to list of dictionaries in python?

Related 관련 기사

  1. 1

    How to compare the index of 2 sublists' elements from a list of lists

  2. 2

    How to compare elements of two input lists?

  3. 3

    How to compare properties of a subitem in 2 lists in linq

  4. 4

    Groovy compare two lists to find lists with common elements

  5. 5

    Joining 2 lists ,Python

  6. 6

    How to add repeating occurences of elements in two lists in python

  7. 7

    How to add repeating occurences of elements in two lists in python

  8. 8

    Compare 2 linked lists using loop

  9. 9

    How to compare Lists of Guids in C#

  10. 10

    How can I copy and combine several .txt lists in alphabetical order?

  11. 11

    Python Defining Lists in Lists

  12. 12

    Comparing of the 2 lists of different objects but with a same fields

  13. 13

    How to split elements of the group in many lists?

  14. 14

    Python: How to create 2D array using existing lists

  15. 15

    Nested Lists and their index calls

  16. 16

    Python - XY array from 2 lists

  17. 17

    How can I disable values in drop down lists based on user selection using JavaScript

  18. 18

    Oddity in how Python3 handles lists

  19. 19

    Python - How to delete the last element in a list of lists?

  20. 20

    assigning lists elements as function arguments

  21. 21

    TortoiseSVN: Where can I view kept 'Change Lists'?

  22. 22

    Merge 2 lists of lists, element by element

  23. 23

    How can I treat double quoted data and single quoted data same in Python 2.x?

  24. 24

    Python adding lists of numbers with other lists of numbers

  25. 25

    Multiple lists in the same navigation bar

  26. 26

    Adding tuples in lists in Python

  27. 27

    Python list to list of lists

  28. 28

    remove elements from a data.frame with lists of lists

  29. 29

    How to quickly convert from items in a list of lists to list of dictionaries in python?

뜨겁다태그

보관