Please help to realise the following function:
Given a list of strings and lists, which may also contain strings and lists etc. Your job is to collect these strings into a dict, where key would be the string and value the amount of occurrences of that string in these lists.
def count_strings(data: list, pos=None, result: dict = None) -> dict:
"""
:param data: given list of lists
:param pos: figure out how to use it
:param result: figure out how to use it
:return: dict of given symbols and their count
"""
My attempts:
if result is None and pos is None:
result = {}
pos = 0
if pos > len(data):
return result
if isinstance(data[pos], str):
return count_strings(data, pos + 1, result)
elif isinstance(data, list):
return count_strings(data, 0, result)
The output should be something like this:
print(count_strings([[], ["J", "*", "W", "f"], ["j", "g", "*"], ["j", "8", "5", "6", "*"], ["*", "*", "A", "8"]]))
# {'J': 1, '*': 5, 'W': 1, 'f': 1, 'j': 2, 'g': 1, '8': 2, '5': 1, '6': 1, 'A': 1}
print(count_strings([[], [], [], [], ["h", "h", "m"], [], ["m", "m", "M", "m"]])) # {'h': 2, 'm': 4, 'M': 1}
print(count_strings([])) # {}
print(count_strings([['a'], 'b', ['a', ['b']]])) # {'a': 2, 'b': 2}