I'm trying to get a function that counts and shows hashtags of a list.
Example input:
["Hey, im in the #pool",
"beautiful #city",
"#city is nice",
"Have a nice #weekend",
"#weekend <3",
"Nice #"]
Output:
{"pool" : 1, "city" : 2, "weekend" : 2}
But if there is only a # followed by no words, it should not count as a hashtag.
Same with stuff before the hashtag, something like „%#“ is not allowed to count as a hashtag.
Hashtags are defined (a-z,A-Z,0-9) every other char ends the hashtag
My current code:
def analyze(posts):
tag = {}
for sentence in posts:
words = sentence.split(' ')
for word in words:
if word.startswith('#'):
if word[1:] in tag.keys():
tag[word[1:]] += 1
else:
tag[word[1:]] = 1
return(tag)
posts = ["Hey, im in the #pool",
"beautiful #city",
"#city is nice",
"Have a nice #weekend",
"#weekend <3",
"Nice #"]
print(analyze(posts))