python 计算一个list 里相同字母出现的次数

比如 count(
freqs(["egg", "bacon", "spam", "spam", "eggs", "spam", "bacon","egg"])=>
[["egg",2],["bacon",2],["spam",2],["eggs",1]] 我知道应该用WHILE + FOR
但不怎么会写 谢谢

第1个回答  2013-03-27
>>> l = ["egg", "bacon", "spam", "spam", "eggs", "spam", "bacon","egg"]
>>> nl = list(set(l))
>>> result = []
>>> for i in nl:
... c = 0
... for j in l:
... if i == j:
... c += 1
... result.append([i,c])
...
>>> result
[['bacon', 2], ['eggs', 1], ['egg', 2], ['spam', 3]]本回答被提问者采纳
相似回答