在这篇文章中,我们开发了一个程序来计算列表中每个元素的频率。使用字典在这里,我们将项目作为字典的键,它们的频率作为值。示例 在线演示list = ['a', 'b', 'a', 'c', 'd', 'c', 'c'] frequency = {} for item in list: if (item in frequency): frequency[item] += 1 else: frequency[item] = 1 for key, value in frequency.items(): print("% s -> % d" % (key, value))输出运行上述代码,得到以下结果:a -> 2 b -> 1 c ... 阅读更多