python histogram one-liner
问题 There are many ways to write a Python program that computes a histogram. By histogram, I mean a function that counts the occurrence of objects in an iterable and outputs the counts in a dictionary. For example: >>> L = 'abracadabra' >>> histogram(L) {'a': 5, 'b': 2, 'c': 1, 'd': 1, 'r': 2} One way to write this function is: def histogram(L): d = {} for x in L: if x in d: d[x] += 1 else: d[x] = 1 return d Are there more concise ways of writing this function? If we had dictionary comprehensions