Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对计数器中的键和值进行排序_Python_Python 3.x_Sorting_Word Count - Fatal编程技术网

Python 对计数器中的键和值进行排序

Python 对计数器中的键和值进行排序,python,python-3.x,sorting,word-count,Python,Python 3.x,Sorting,Word Count,这是我正在处理的代码,我希望输出作为降序计数,如果计数相同,则按名称排序 from collections import Counter import re from nltk.corpus import stopwords import operator text = "The quick brown fox jumped over the lazy dogs bowl. The dog was angry with the fox considering him lazy." def tok

这是我正在处理的代码,我希望输出作为降序计数,如果计数相同,则按名称排序

from collections import Counter
import re
from nltk.corpus import stopwords
import operator
text = "The quick brown fox jumped over the lazy dogs bowl. The dog was angry with the fox considering him lazy."
def tokenize(text):
    tokens = re.findall(r"\w+|\S", text.lower())
    #print(tokens)
    tokens1 = []
    for i in tokens:
        x = re.findall(r"\w+|\S", i, re.ASCII)
        for j in x:
            tokens1.append(j)

    return tokens
tok = tokenize(text)

punctuations = ['(',')',';',':','[',']',',', '...', '.', '&']

keywords = [word for word in tok if not word in punctuations]

cnt = Counter()
d= {}
for word in keywords:
    cnt[word] += 1 
print(cnt)
freq = operator.itemgetter(1)

for k, v in sorted(cnt.items(), reverse=True, key=freq):
    print("%3d  %s" % (v, k))
电流输出:

  4  the
  2  fox
  2  lazy
  1  quick
  1  brown
  1  jumped
  1  over
  1  dogs
  1  bowl
  1  dog
  1  was
  1  angry
  1  with
  1  considering
  1  him
所需输出:

  4  the
  2  fox
  2  lazy
  1  angry
  1  bowl
  1  brown
  1  considering
  1  dog
  1  dogs

等等。

使用返回元组的排序函数。元组中的第一项是计数(字典中的值)的倒数,第二项是字符串(字典中的键)。您可以通过删除变量
freq
,删除调用sorted中的关键字
reverse
,并提供一个小lambda函数,为字典中的每个项目返回(-value,key)。程序的最后几行是:

print(cnt)
for k, v in sorted(cnt.items(), key=lambda item: (-item[1], item[0])):
    print("%3d  %s" % (v, k))

lambda函数中使用-符号的原因是为了获得正确的排序顺序,因为默认的排序顺序是从低到高。

与基础字典一样,
计数器
不是有序的数据结构。如果顺序很重要,请参见例如。或者考虑按代码< >文本排序。索引的索引>代码>值。