繁体   English   中英

Python:查找最频繁的字节?

[英]Python: find most frequent bytes?

我正在寻找一种(最好是简单的)方法来查找和排序python流元素中最常见的字节。

例如

>>> freq_bytes(b'hello world')
b'lohe wrd'

甚至

>>> freq_bytes(b'hello world')
[108,111,104,101,32,119,114,100]

我目前有一个函数,该函数以list[97] == occurrences of "a"形式返回列表。 我需要对它进行排序。

我认为我基本上需要翻转列表,因此list[a] = b --> list[b] = a同时删除重复项。

在collections模块中尝试Counter类

from collections import Counter

string = "hello world"
print ''.join(char[0] for char in Counter(string).most_common())

请注意,您需要Python 2.7或更高版本。

编辑:忘记了most_common()方法返回值/计数元组的列表,并使用列表推导来获取值。

def frequent_bytes(aStr):
    d = {}
    for char in aStr:
        d[char] = d.setdefault(char, 0) + 1

    myList = []
    for char, frequency in d.items():
        myList.append((frequency, char))
    myList.sort(reverse=True)

    return ''.join(myList)

>>> frequent_bytes('hello world')
'lowrhed '

我只是尝试了一些显而易见的事情。 不过,@ kindall的答案很糟糕。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM