# -*- coding: utf-8 -*-
import random
# Find the difference between two lists, neither of which are repeated elements
# Randomly select 20 lists in range(100) as the test set, and the remaining 80 are used as the training set
# Do not use keywords as variable names, such as list, set
list_all = range(100)
test_list = (list_all, 20) # Randomly select 20 unrepeated ones
# Method 1
train_list = []
for item in list_all:
if item not in test_list:
train_list.append(item)
print train_list
# Method 2: List comprehension (this should be mastered)
train_list2 = [item for item in list_all if item not in test_list] # Remove not to find intersection
print train_list2
# Method 3: First convert to set (both lists have no duplicate elements. If list_all has duplicate elements and does not want to remove duplicate elements, you cannot use this)
train_list3 = list(set(list_all) ^ set(test_list))
print train_list3
# The intersection of the set, the difference set
# Note that converting to set will remove the duplicate elements. Sometimes you don’t want to remove the duplicate elements, so you have to use the above method one or method two.
a = [1, 1, 2, 2, 3, 3, 4, 4]
b = [1, 2]
print list(set(a).intersection(set(b)))
print list(set(a).union(set(b)))
print list(set(b) ^ set(a)) # Different sets, this does not distinguish the order, and anyone who writes it in the first place will be greatly reduced.
print list(set(a).difference(set(b))) # Difference set, note that the front is subtracted from the back, and you cannot make the wrong order