-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path05_set.py
37 lines (30 loc) · 857 Bytes
/
05_set.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import sys
# set datastructure
#aSet = {''}
#print(type(aSet)) # set
someList = ['a', 'a', 'b', 'c', 'd', 'e', 'e']
someSet = set(someList)
print(someSet)
# brute force version
duplicateList = []
for value in someList:
if someList.count(value) > 1:
if value not in duplicateList:
duplicateList.append(value)
print(duplicateList)
# more nice code by using set
someList = ['a', 'a', 'b', 'c', 'd', 'e', 'e']
duplicateSet = set([x for x in someList if someList.count(x) > 1])
print(list(duplicateSet))
#-------------
# set methods
#-------------
#>>> intersection
valid = set(['yellow', 'red', 'blue', 'green', 'black'])
inSet = set(['red', 'brown'])
print(inSet.intersection(valid))
#>>> difference
valid = set(['yellow', 'red', 'blue', 'green', 'black'])
inSet = set(['red', 'brown'])
print(inSet.difference(valid))