Repository navigation
Expand file tree
/
Copy pathuniqueWord.py
More file actions
47 lines (42 loc) · 1.48 KB
/
Copy pathuniqueWord.py
File metadata and controls
47 lines (42 loc) · 1.48 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
# Unique Words Collector
# Take a paragraph input from the user. Split it into words, remove duplicates, sort them
# alphabetically, and count the total number of unique words.
para =input("Enter the paragraph:")
para = para.lower()
words =para.split()
# unique_words =set(words)
# unique_words =sorted(unique_words)
# count_unique =len(unique_words)
# print("sorted:",unique_words)
# print("Total number of unique words:",count_unique)
# unique_words=set(words)
# unique_list=list(unique_words)
# for i in range(len(unique_list)):
# for j in range(len(unique_list) - 1):
# if unique_list[j] > unique_list[j + 1]:
# temp = unique_list[j]
# unique_list[j] = unique_list[j + 1]
# unique_list[j + 1] = temp
# print("Sorted:",unique_list)
# print("Total number of unique words:",len(unique_list))
unique_words = []
frequency = []
for w in words:
if w not in unique_words:
unique_words.append(w)
count = 0
for i in words:
if i== w:
count += 1
frequency.append(count)
print("Words and their frequencies:")
for i in range(len(unique_words)):
print(f"{unique_words[i]}: {frequency[i]}")
# unique_list=list(unique_words)
for i in range(len(unique_words)):
for j in range(len(unique_words) - 1):
if unique_words[j] > unique_words[j + 1]:
temp = unique_words[j]
unique_words[j] = unique_words[j + 1]
unique_words[j + 1] = temp
print("Sorted:",unique_words)