python - 计数器列表 python 2.7

标签 python list python-2.7 counter

我有这样的列表:

Pasang = [0, 4, 4, 5, 1, 7, 6, 7, 5, 7, 4, 9, 0, 10, 1, 10,...., 23, 9, 23, 7, 23]

我从该列表中计算项目:

satuan = Counter(pasang)

然后我得到:

Counter({5: 10, 6: 7, 0: 5, 1: 5, 7: 5, 10: 4, 11: 4, 15: 4,...,14: 1, 21: 1})

我想从柜台拿 key ,所以我这样做:

satu = satuan.keys()

然后我得到这样的排序列表:

[0, 1, 2, 4, 5,...,21, 22, 23]

但我需要这样的输出(未排序):

[5, 6, 0, 1,...,14, 21]

抱歉我的英语不好。

最佳答案

您可能需要:

[key for key, freq in c.most_common()]

其中 cCounter 实例。

most_common 将按频率降序返回键和频率对。然后使用理解提取关键部分。

关于python - 计数器列表 python 2.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30421901/

相关文章:

Python dateutil rrule错误

python - Pandas DataFrame 合并重置索引

java - 我正确使用 list.remove 吗?

python - python如何将属性写入对应的标签? (文中的详细示例可能有助于更好地理解它。)

python 合并两个列表(偶数/奇数元素)

python - Pandas DataFrame 浮点格式

python lmfit "object too deep for desired array"

python 获取列表中的字典值

python - python中的联合熵

python - 通过 Tweepy 获取 Twitter 中的所有关注者 ID