2021年4月29日星期四

Intersect lists in specific way

I am given two lists and need to combine them where the output is first the nonoverlapping elements in list1, then nonoverlapping elements in list2, then all of the elements that overlap in the same order as in list 1.

For example:

first = ['x', 'c', 'y', 5, 4]  second = [4, 'c', 4, 2, 'y']  

The answer would be

['x', 'y', 5, 4, 2, 'y', 'c', 4]  

What I tried:

for i in first[:]:      if i in second:          first.remove(i)  for i in second[:]:      if i in first:          second.remove(i)  return first + second  

Example: Example

https://stackoverflow.com/questions/67327128/intersect-lists-in-specific-way April 30, 2021 at 09:38AM

没有评论:

发表评论