+ 1

Implement function str_to_dict(some_str) which returnes dictionary, where keys are string characters, and values are...

Implement function str_to_dict(some_str) which returnes dictionary, where keys are string characters, and values are their quantity in the string: def str_to_dict(some_str): """ :param some_str: str :return: dict """ #YOUR CODE HERE print('Str to dict:', str_to_dict('text_text_text')) Expected output: Str to dict: {'t': 6, 'e': 3,...}

20th May 2020, 5:09 PM
Elizabeth
Elizabeth - avatar
5 Answers
+ 2
return {str:some_str.count(str) for str in set(some_str)}
20th May 2020, 5:54 PM
Elizabeth
Elizabeth - avatar
+ 5
what did you come up with until now so we can instruct you how to fix it?
20th May 2020, 5:18 PM
Sebastian Pacurar
Sebastian Pacurar - avatar
+ 5
It would be a good idea if you provide us with a sample of the input string, and also with a sample how the output dict should look like. Thanks
20th May 2020, 6:01 PM
Lothar
Lothar - avatar
+ 3
Loop through a set() of the string. Have a look at string methods e.g "count". Learn how to add to a dictionary.
20th May 2020, 5:52 PM
rodwynnejones
rodwynnejones - avatar
+ 3
Elizabeth, that is a great solution, it works perfect!
21st May 2020, 12:43 PM
Lothar
Lothar - avatar