我到目前为止:
def most_frequent(string):
d = dict()
for key in string:
if key not in d:
d[key] = 1
else:
d[key] += 1
return d
print most_frequent('aabbbc')
返回:
{'a': 2, 'c': 1, 'b': 3}
现在我需要:
我应该将此词典转换为元组还是列表?
答案 0 :(得分:13)
这是一行答案
sortedLetters = sorted(d.iteritems(), key=lambda (k,v): (v,k))
答案 1 :(得分:5)
这应该做得很好。
def frequency_analysis(string):
d = dict()
for key in string:
d[key] = d.get(key, 0) + 1
return d
def letters_in_order_of_frequency(string):
frequencies = frequency_analysis(string)
# frequencies is of bounded size because number of letters is bounded by the dictionary, not the input size
frequency_list = [(freq, letter) for (letter, freq) in frequencies.iteritems()]
frequency_list.sort(reverse=True)
return [letter for freq, letter in frequency_list]
string = 'aabbbc'
print letters_in_order_of_frequency(string)
答案 2 :(得分:3)
这是返回元组列表而不是字典的东西:
import operator
if __name__ == '__main__':
test_string = 'cnaa'
string_dict = dict()
for letter in test_string:
if letter not in string_dict:
string_dict[letter] = test_string.count(letter)
# Sort dictionary by values, credits go here http://stackoverflow.com/questions/613183/sort-a-dictionary-in-python-by-the-value/613218#613218
ordered_answer = sorted(string_dict.items(), key=operator.itemgetter(1), reverse=True)
print ordered_answer
答案 3 :(得分:3)
Python 2.7直接支持这个用例:
>>> from collections import Counter
>>> Counter('abracadabra').most_common()
[('a', 5), ('r', 2), ('b', 2), ('c', 1), ('d', 1)]
答案 4 :(得分:2)
chills42 lambda函数获胜,我想但作为替代方案,如何生成字典而不是计数作为键?
def count_chars(string):
distinct = set(string)
dictionary = {}
for s in distinct:
num = len(string.split(s)) - 1
dictionary[num] = s
return dictionary
def print_dict_in_reverse_order(d):
_list = d.keys()
_list.sort()
_list.reverse()
for s in _list:
print d[s]
答案 5 :(得分:2)
编辑这会做你想要的。我正在窃取 chills42 行并添加另一行:
sortedLetters = sorted(d.iteritems(), key=lambda (k,v): (v,k))
sortedString = ''.join([c[0] for c in reversed(sortedLetters)])
------------ 原始回答 ------------
要打印出已排序的字符串,请将另一行添加到 chills42 one-liner:
''.join(map(lambda c: str(c[0]*c[1]), reversed(sortedLetters)))
打印出'bbbaac'
如果你想要单个字母,'bac'使用这个:
''.join([c[0] for c in reversed(sortedLetters)])
答案 6 :(得分:2)
from collections import defaultdict
def most_frequent(s):
d = defaultdict(int)
for c in s:
d[c] += 1
return "".join([
k for k, v in sorted(
d.iteritems(), reverse=True, key=lambda (k, v): v)
])
修改强>
这是我的一个班轮:
def most_frequent(s):
return "".join([
c for frequency, c in sorted(
[(s.count(c), c) for c in set(s)], reverse=True
)
])
答案 7 :(得分:1)
以下是most_frequent
功能的代码:
>>> a = 'aabbbc'
>>> {i: a.count(i) for i in set(a)}
{'a': 2, 'c': 1, 'b': 3}
这个特殊的语法适用于py3k,但使用以前版本的语法编写类似的东西很容易。在我看来,它比你的更具可读性。
答案 8 :(得分:0)
def reversedSortedFrequency(string)
from collections import defaultdict
d = defaultdict(int)
for c in string:
d[c]+=1
return sorted([(v,k) for k,v in d.items()], key=lambda (k,v): -k)
答案 9 :(得分:0)
以下是修正版(感谢您指出错误)
def frequency(s):
return ''.join(
[k for k, v in
sorted(
reduce(
lambda d, c: d.update([[c, d.get(c, 0) + 1]]) or d,
list(s),
dict()).items(),
lambda a, b: cmp(a[1], b[1]),
reverse=True)])
我认为使用reduce
会使这种溶剂与其他溶剂相比有所不同......
行动中:
>>> from frequency import frequency
>>> frequency('abbbccddddxxxyyyyyz')
'ydbxcaz'
这个包括提取密钥(并计算它们)!另一个不错的属性是在同一行上初始化字典:)
另外:不包括,只是内置。
reduce
函数有点难以理解,在lambda
中设置字典值在python中也有点麻烦,但是,嗯,它有效!