Python 按字母顺序返回字典的输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16600174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
return output of dictionary to alphabetical order
提问by user2101517
The following code prints out the word in the txt file and then how many instances there are of that word (e.g. a, 26) the problem is that it doesn't print it out in alphabetical order. Any help would be much appreciated
下面的代码打印出 txt 文件中的单词,然后该单词有多少个实例(例如 a, 26),问题在于它没有按字母顺序打印出来。任何帮助将非常感激
import re
def print_word_counts(filename):
s=open(filename).read()
words=re.findall('[a-zA-Z]+', s)
e=[x.lower() for x in (words)]
e.sort()
from collections import Counter
dic=Counter(e)
for key,value in dic.items():
print (key,value)
print_word_counts('engltreaty.txt')
采纳答案by mgilson
You just need to sort the items. The builtin sortedshould work wonderfully:
您只需要对项目进行排序。内置sorted应该很好地工作:
for key,value in sorted(dic.items()):
...
If you drop the e.sort()line, then this should run in approximately the same amount of time. The reason that it doesn't work is because dictionaries are based on hashtables which store items in order of their hash values (with some more complicated stuff when hash collisions occur). Since the hashing function is never specified anywhere, it means that you can't count on a dictionary keeping any order that you try to give it and that the order is implementation and version dependent. For other simple cases, the collectionsmodule has an OrderedDictsubclass which does keep insertion order. however, that won't really help you here.
如果您放弃该e.sort()线路,那么这应该在大约相同的时间内运行。它不起作用的原因是因为字典基于按hash哈希值顺序存储项目的表(当发生哈希冲突时会出现一些更复杂的东西)。由于从未在任何地方指定散列函数,这意味着您不能指望字典保持您尝试给出的任何顺序,并且该顺序取决于实现和版本。对于其他简单情况,collections模块有一个OrderedDict子类,它确实保持插入顺序。然而,这不会真正帮助你在这里。
回答by jamylak
Note Counteris a subclass of dictso sorting before you add to Counter:
NoteCounter是dictso 排序的子类,然后再添加到Counter:
e.sort()
dic=Counter(e)
won't achieve order.
不会达到秩序。
import re
from collections import Counter
def print_word_counts(filename):
c = Counter()
with open(filename) as f: # with block closes file at the end of the block
for line in f: # go line by line, don't load it all into mem at once
c.update(w.lower() for w in re.findall('[a-zA-Z]+', line))
for k, v in sorted(c.items()): # sorts
print k, v
print_word_counts('engltreaty.txt')

