python - duplicate returns more numbers than required -


a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]  if len(set(a)) > len(set(b)):  # finds biggest list     largest =     smallest = b else:     largest = b     smallest =  common = largest  in largest:     if not in smallest:         common.remove(i)  print(common) 

prints: [1, 2, 3, 5, 7, 8, 10, 12, 13] 7, 9, 10, 11, 12 shouldnt in list. because aint in smaller list.

what doing wrong?

your code relies on assumption statements like

largest=a

copy list largest. this not true in python. rather statement makes largest reference old list a.

your code, copy lists properly, should this:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]  if len(set(a)) > len(set(b)):  # finds biggest list     largest = list(a)     smallest = list(b) else:     largest = list(b)     smallest = list(a)  common = list(largest)  in largest:     if not in smallest:         common.remove(i)  print(common) 

you result

[1, 2, 3, 5, 8, 13]


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -