аватар question@mail.ru · 01.01.1970 03:00

Comparison of 2 lists in Python

Hello, there are two lists

  anti = [ 'Red' ,  '"blue' ,  'green' ,  'White' ]  word = [ '' red ',  'White' ]     

you need to compare two lists and the matching elements to bring to a separate list, say, result. Such a question has already been, but I can’t understand how to get the same elements.

аватар answer@mail.ru · 01.01.1970 03:00

All the same, Python is dynamic and expressive. This is not taken away from him.

there are some.

here is your single-bedroom:

  Result =  LIST  ( set  (ANS) & amp;  set  (Word))  

This will be intersected DATA-HIGHLIGHTED = ""> [, ' 'White' ]

if you need a list of unique elements in the combination of two lists:

  [ '' "red ', ' 'White' ,  'green' ,  'Blue' ] result =  list  ( set  (ANS + Word))  
  

  [ 'green' ,  '"" "blue' ] result =  list  ( set  (Ans) ^  set  (Word)) r    

the usual difference (many from aus not included in Word ):

  [ '' green ',  'Blue' ] result =  line  ( set  (ANS) -  set  (word))     

option that preserves a smaller number of types of types:

  set  (word) result = [x  for  x  in  ans  if  x  in  in  sbuf)]                                       

Latest

Similar