FP - Extra2 - Ex3 added

This commit is contained in:
TiagoRG 2023-01-08 22:39:18 +00:00
parent 95a36d551e
commit 0ce3cb4ab2
1 changed files with 33 additions and 0 deletions

View File

@ -23,3 +23,36 @@ print(twits[0]["text"])
# Algumas mensagens contêm hashtags: # Algumas mensagens contêm hashtags:
print(twits[880]["text"]) print(twits[880]["text"])
# A)
word_count: dict[str, int] = {}
for tweet in twits:
tweet_content: str = tweet["text"]
for word in tweet_content.split():
if word not in word_count:
word_count[word] = 0
word_count[word] += 1
word_list = list(word_count.keys())
print("A)\n" + str(word_list), end="\n\n")
# B)
ordered_list = sorted(word_list, key=lambda t: word_count[t], reverse=True)
print("B)\n" + str(ordered_list), end="\n\n")
# C)
ordered_hashtag_list = [word for word in ordered_list if word.startswith('#')]
print("C)\n" + str(ordered_hashtag_list), end="\n\n")
# D)
print("D)\n")
most_used = word_count[ordered_hashtag_list[0]]
for hashtag in ordered_hashtag_list:
print(f"{hashtag:<30} ({word_count[hashtag]:>2}) {'+' * ((word_count[hashtag]*18)//most_used)}")