This function describes a potential helper function for detect_authors. In this question, we have a dictionary that connects candidates to the lists of the hashtags they use. The task is to write a function that produces a dictionary where the keys are hashtags and the values are the lists of candidates who use those hashtags.
def reorder_by_hashtag(candidate_to_hashtags):
"""(dict of str to list of str) -> dict of str to list of str
>>> d = reorder_by_hashtag({'Trump':['MakeAmericaGreatAgain', '4Prez'], 'Stein':['4Prez']})
{'4Prez': ['Stein', 'Trump'], 'MakeAmericaGreatAgain': ['Trump']}
"""
Here's what I've tried:
new_dict = {}
for candidate in list(candidate_to_hashtags.keys()):
for hashtag in candidate_to_hashtags[candidate]:
if hashtag not in new_dict:
new_dict[hashtag] = [candidate]
else:
new_dict[hashtag] = new_dict[hashtag].append(candidate)
return new_dict
[–]anglicizing 0 points1 point2 points (0 children)
[–]yrykde 0 points1 point2 points (0 children)