-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicate_Words_And_Letters.py
More file actions
58 lines (48 loc) · 2.23 KB
/
Duplicate_Words_And_Letters.py
File metadata and controls
58 lines (48 loc) · 2.23 KB
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
# Find duplicates in a list and show counts.
names = ['bob', 'susan', 'bob', 'dan', 'susan']
uniques = []
duplicates = []
for name in names:
if name not in uniques:
uniques.append(name)
elif name in uniques:
duplicates.append(name)
print('Unique list: ', uniques)
print('Duplicate list: ', duplicates)
# Since we need to find if the word exists in the object - lists are inefficient
# at this since we have to search through each element
# Now we will use sets to do the same problem.
unique_names = set(names)
print(unique_names)
# Now we just have uniques names through Data Structure Magic.
# But we don't have the counts OR the duplicates. So... if only we only track it with an accompanying counter.
# Note we cant do a set difference since we would convert the list of names to a set and thus wipe duplicates.
name_count = {} # declare empty dictionary
for name in names:
if name not in name_count:
name_count[name] = 1
else:
name_count[name] += 1
print('name_count', name_count.items())
print('name_count 1 ', list(name_count.items())[0])
final_names = [f'{key}({value})' for key, value in name_count.items()]
print('final names: ', ' '.join(final_names))
print('-----------Default Dictionary-----------------')
from collections import defaultdict
name_count = defaultdict(int) # declare empty dictionary
for name in names:
name_count[name] += 1 # The default dict adds in the name and initializes the value to 0.
# Then it adds a 1
print('name_count', name_count)
final_names = [f'{name}({count})' for name, count in name_count.items()]
print('final names: ', ' '.join(final_names))
print('-----------Counter for duplicate names-----------------')
from collections import Counter
name_count = Counter(names) #Counter is dictionary that stores the "count" of each key as it corresponding value.
print('name_count', name_count)
final_names = [f'{name}({count})' for name, count in name_count.items()]
print('final names: ', ' '.join(final_names))
print('-----------Counter for duplicate letters-----------------')
duplicate_leters = Counter('indiVISABility'.lower())
final_letters = [f'{letter}({count})' for letter, count in duplicate_leters.items()]
print('Finals Letters are: ', ' '.join(final_letters))