-
Notifications
You must be signed in to change notification settings - Fork 1
/
c1_10_remove_duplicates.py
71 lines (62 loc) · 1.46 KB
/
c1_10_remove_duplicates.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#%% remove duplicate
"""
sets
hashable
unhashable with key
"""
#%%
# hashable types
print(set([45,0,0,45,3,2,5,2,3]))
# order is not kept
#%%
# solution : create a generator that yields the next item of my iterable on certain conditions
def dedupe(items):
seen = set()
for item in items:
if item not in seen:
yield item
seen.add(item)
print(list(dedupe([99, 1,2,3,4,5,4,5,4,5,4,3,5,2,10])))
# %%
# unhashable type
set(({"a":1}, {"a":1}, {"a":2}))
# %%
list(dedupe([{"a":1}, {"a":1}, {"a":2}]))
# %%
# let's modify our dedupe function
def dedupe(items, key=None):
seen = set()
for item in items:
val = item if key is None else key(item)
if val not in seen:
yield item
seen.add(val)
# %%
list(dedupe([{"a":1}, {"a":1}, {"a":2}], key = lambda x: x["a"]))
#%%
import operator
class Car:
def __init__(self, name, color):
self.name = name
self.color = color
def __repr__(self):
return f"<Car: {self.name} of color {self.color}>"
a = Car("BMW", "blue")
b = Car("Audi", "blue")
c = Car("BMW", "green")
d = Car("BMW", "blue")
#%%
print(list(dedupe([a, b, c, d])))
#%%
print(list(dedupe([a, b, c, d], key= operator.attrgetter("color"))))
#%%
print(list(dedupe([a, b, c, d], key= operator.attrgetter("name"))))
#%%
print(list(dedupe([a, b, c, d], key= operator.attrgetter("name", "color"))))
# %%
tboxlist = []
textboxes = ["tbox1", "tbox2", "tbox3", "tbox4", "tbox4", "tbox1", "tbox6"]
for tbox in dedupe(textboxes):
tboxlist.append(tbox)
print(tboxlist)
# %%