简体   繁体   English

通过创建tuplles的排序列表在python中对字典进行排序不起作用

[英]Sorting Dictionary in python by making a sorted list of tuplles doesn't work

I've been using the solution provided in Sorting_Dictionary to sort a dictionary according to values.I know dictionaries cannot be as such sorted but a list of sorted tupples can be obtained. 我一直在使用Sorting_Dictionary中提供的解决方案根据值对字典进行排序。我知道字典无法进行这种排序,但可以获得排序的tupple列表。

Complete code: 完整的代码:

import sys
import pprint


def helper(filename):
    Word_count={}
    f=open(filename)
    for line in f:
        words=line.split()
        for word in words:
            word=word.lower()
            Word_count.setdefault(word,0)
            Word_count[word]+=1
    f.close()
    return Word_count

def print_words(filename):
    Word_count_new=helper(filename)
    sorted_count=sorted(Word_count_new.items(),key=Word_count_new.get,reverse=True)
    for word in sorted_count:
      pprint.pprint(word)

def print_top(filename):
    word_list=[]
    Word_count=helper(filename)
    word_list=[(k,v) for k,v in Word_count.items()]
    for i in range(20):
        print word_list[i] + '\n'
###

# This basic command line argument parsing code is provided and
# calls the print_words() and print_top() functions which you must define.
def main():
  if len(sys.argv) != 3:
    print 'usage: ./wordcount.py {--count | --topcount} file'
    sys.exit(1)

  option = sys.argv[1]
  filename = sys.argv[2]
  if option == '--count':
    print_words(filename)
  elif option == '--topcount':
    print_top(filename)
  else:
    print 'unknown option: ' + option
    sys.exit(1)

if __name__ == '__main__':
  main()

This function produces problem: 此函数产生问题:

def print_words(filename):
    Word_count_new=helper(filename)
    sorted_count=sorted(Word_count_new.items(),key=Word_count_new.get,reverse=True)
    for word in sorted_count:
        pprint.pprint(word)

here helper is a method which returns a dictionary which is to be sorted. 这里的helper是一种返回要排序的字典的方法。 The dictionary is like this {Dad:1, Mom:2, baby:3} 字典是这样的{爸爸:1,妈妈:2,婴儿:3}

But this doesn't produce a sorted list of tupples. 但这不会产生排序的清单。 Instead the output is somewhat random like this 相反,输出有点像这样

('he', 111)
("hot-tempered,'", 1)
('made', 29)
('wise', 2)
('whether', 11)
('wish', 21)
('scroll', 1)
('eyes;', 1)
('this,', 17)
('signed', 2)
('this.', 1)

How can we explain this behaviour? 我们如何解释这种行为?

 sorted_count = sorted(Word_count_new.items(), key=lambda x: x[1], reverse=True)

According to the documentation for sorted ( https://docs.python.org/3/library/functions.html#sorted ), the second argument is a function that creates a comparison key from each list element , so not the dict as a whole. 根据sorted的文档( https://docs.python.org/3/library/functions.html#sorted ),第二个参数是一个从每个列表元素创建比较键的函数,因此dict不能作为整个。

Word_count_new.items() returns an iterable (in python3, list in python2) of tuples, which is what's passed to your key function. Word_count_new.items()返回一个可迭代的元组(在python3中为python2中的列表),这是传递给您的键函数的内容。 If you want your comparison key to be based of the work frequency (the second element), you want to return the second element in this function ( x[1] where x is the individual tuple getting compared). 如果希望比较键基于工作频率(第二个元素),则要在此函数中返回第二个元素( x[1] ,其中x是要比较的单个元组)。

To also explain the random output you got, your key was Word_count_new.get . 为了解释您得到的随机输出,您的密钥是Word_count_new.get Since your dict does not have tuples as keys, the default value will be None. 由于您的字典没有元组作为键,因此默认值为“无”。

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

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