Skip to content

Instantly share code, notes, and snippets.

@Hwatwasthat
Created February 21, 2018 10:54
Show Gist options
  • Select an option

  • Save Hwatwasthat/7b1cc00cd99e3ba134127ff946abe19d to your computer and use it in GitHub Desktop.

Select an option

Save Hwatwasthat/7b1cc00cd99e3ba134127ff946abe19d to your computer and use it in GitHub Desktop.
List comparison created by Hwatwasthat - https://repl.it/@Hwatwasthat/List-comparison
import random
# Generate Lists
list1 = random.sample(range(50), 20)
list2 = random.sample(range(50), 20)
lists_compared = []
# Compare list a and b, put into a list from a set (compares as a set)
lists_compared = list(set(list1).intersection(list2))
# Sort the lists into numerical order for easier comprehension
list.sort(list1)
list.sort(list2)
list.sort(lists_compared)
# Print all lists
print("List 1:", list1, "\n")
print("List 2:", list2, "\n")
print("Number of matches:", str(len(lists_compared)), "\n")
print("Matches in Lists:", lists_compared, "\n")
# Single line comparison
[print(list1[x]) for x in range(len(list1)) if list1[x] not in list1[:x] and list1[x] in list2]
# 2 List comparison function
def list_comparison(a, b):
return[a[x] for x in range(len(a)) if a[x] not in a[:x] and a[x] in b]
# Demo of function
print(list_comparison(list1, list2))
# 2nd 2 list comparison function
def compared(a, b):
x = [x for x in a if x in a and x in b]
print(x)
compared(list1, list2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment