string - Adding to a Python Dictionary -
def countfrequency(l): fdict = {} x in range(0, len(l)): key, value in fdict: if l[x] == fdict[str(x)]: value = value + 1 else: fdict[l[x]] = 1 return fdict
i'm trying count frequency of occurrences of particular symbol in given string , create dictionary out of this. reason, function returns empty dictionary. think problem arises adding new value dictionary, not sure how troubleshoot it/fix it.
input: countfrequency('mississippi') output: {}
do this:
def countfrequency(l): fdict = {} x in range(0, len(l)): if str(l[x]) in fdict.keys(): fdict[str(l[x])] = fdict[str(l[x])] + 1 else: fdict[str(l[x])] = 1 return fdict
Comments
Post a Comment