-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset _data_structure.py
42 lines (33 loc) · 1.22 KB
/
set _data_structure.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
37
38
39
40
41
42
#basic way
besic_way="""
my_list = ['a', 'b', 'c', 'b', 'd', 'm', 'n', 'n']
duplicates = []
for value in my_list:
if my_list.count(value) >1:
if value not in duplicates:
duplicates.append(value)
print(duplicates)
"""
print(besic_way)
#***************************
doc = """
set is a really useful data structure.
sets behave mostly like lists with the distinction that they can not contain duplicate values.
It is really useful in a lot of cases. For instance you might want to check whether there are duplicates in a list or not. You have two options.
The first one involves using a for loop. Something like this:
"""
print(doc)
my_list = ['a', 'b', 'c', 'b', 'd', 'm', 'n', 'n']
duplicates = set([x for x in my_list if my_list.count(x)>1])
print(duplicates)
#Sets also have a few other methods. Below are some of them.
#1
valid = set(['yellow', 'red', 'blue', 'green', 'black'])
user_input=set(['red', 'brown'])
print(user_input.intersection(valid))
#2
valid = set(['yellow', 'red', 'blue', 'green', 'black'])
user_input=set(['red', 'brown'])
print(user_input.difference(valid))
"""There are a few other methods as well"""
#*********** H A P P Y C O D I N G **********