+ 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,...}
5 Respuestas
+ 2
return {str:some_str.count(str) for str in set(some_str)}
+ 5
what did you come up with until now so we can instruct you how to fix it?
+ 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
+ 3
Loop through a set() of the string.
Have a look at string methods e.g "count".
Learn how to add to a dictionary.
+ 3
Elizabeth, that is a great solution, it works perfect!