【问题标题】:Sort list with keys/values in alphabetical order按字母顺序使用键/值对列表进行排序
【发布时间】:2017-07-16 21:45:26
【问题描述】:

我正在尝试获取一个图表,以在 Y 轴上列出 A-Z,A 在顶部,Z 在底部(靠近 (0,0))。

现在是一团糟,因为我正在学习如何做到这一点,但这里是相关代码:

from collections import Counter
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
from collections import OrderedDict
from operator import itemgetter

myFile = "D:/User/Documents/Data/English Words/words_alpha.txt"

ie_prefix = {}
ie_prefix.setdefault("letter", [])
ie_prefix.setdefault("word",[])
ie_prefix.setdefault("beforec",[])
ie_prefix_sorted = {}

def get_words(file_import):
    global total_words, ie_after_c, ie_after_not_c
  #if I don't do this and just keep the below, I get an error: 'total_words' is not defined when running the print(..., total_words, "total words and", ...) line.
    total_words = 0
    ie_after_not_c = 0
    ie_after_c = 0
    results = []
    with open(file_import) as inputfile:
        for line in inputfile:
            total_words = total_words + 1
            if line.find("ie") != -1:
                pos = line.find("ie")
                ie_prefix["letter"].append(line[pos-1:pos])
                ie_prefix["word"].append(line.strip('\n'))
                if line[pos-1:pos] == "c":
                    ie_prefix["beforec"].append(line.strip('\n'))
                    ie_after_c += 1
                elif line[pos-1:pos] != "c":
                    ie_after_not_c += 1

    ie_prefix_sorted = OrderedDict(sorted(ie_prefix.items()))
    return ie_prefix, total_words, ie_after_not_c, ie_after_c, ie_prefix_sorted


def create_graph(total_words, y_axis, x_axis):
    y_pos = np.arange(len(y_axis))
    fig, ax = plt.subplots()
    plt.barh(y_pos, x_axis, align='center', alpha=0.5)
    plt.yticks(y_pos, y_axis)
    plt.ylabel('Usage')
    plt.title('I before E rule')
    plt.legend()
    plt.show()

get_words(myFile)

# https://stackoverflow.com/questions/20316299/formatting-output-of-counter
ie_count = Counter(ie_prefix["letter"])

ie_count_sorted = sorted(ie_count)  #sorted(ie_count.items()) ## THis will just sort the KEYS I believe
ie_letters = list(ie_count_sorted)

###    
## How to use the SORTED IE Count in the graph, so it goes from A-Z where A is at the TOP, and Z is at the BOTTOM of Y-Axis (Z closest to (0,0))?  
create_graph(total, ie_count, ie_count.values())

仅供参考,这里是print(ie_count)

计数器({'r': 2417, 't': 1771, 'l': 1304, 'f': 1034, 'd': 778, 'h': 765, 'p': 753, 'c ':729,'n':647,'m':536,'g':492,'s':470,'k':443,'v':273,'b':260,'z': 154,'u':134,'w':93,'o':75,'x':73,'y':49,'e':29,'a':26,'':3,' j': 2, 'i': 1})

我不知道如何将 ie_count 重新排列为按字母顺序排列,同时将值(2417、171 等)与键(字母)保持一致。

【问题讨论】:

  • 我正在学习 Python,所以可能错过了一些非常明显的东西(比如我想要做的事情的名称/类型是什么)。我将不胜感激任何关于此类的说明,或反对投票的原因。
  • 我没有投票,而是 get_words 函数(有点不必要,因为我们无权访问您的文件,因此您可以简单地将其从问题中删除)您可以插入您的ie_count 而不是 函数 create_graph 会很有趣
  • @MSeifert - 啊,对不起!没想到我漏掉了。我会编辑。仅供参考,单词列表是来自 GitHub 的 this file

标签: python python-3.x sorting dictionary


【解决方案1】:

您可以对.items进行排序,并使用zip解包键和值:

from collections import Counter
from operator import itemgetter

ie_count = Counter({'r': 2417, 't': 1771, 'l': 1304, 'f': 1034, 'd': 778, 'h': 765, 'p': 753, 'c': 729, 'n': 647, 'm': 536, 'g': 492, 's': 470, 'k': 443, 'v': 273, 'b': 260, 'z': 154, 'u': 134, 'w': 93, 'o': 75, 'x': 73, 'y': 49, 'e': 29, 'a': 26, '': 3, 'j': 2, 'i': 1})

cnts_sorted = sorted(ie_count.items(), key=itemgetter(0))
print(cnts_sorted)
# [('', 3), ('a', 26), ('b', 260), ('c', 729), ('d', 778), ('e', 29), 
#  ('f', 1034), ('g', 492), ('h', 765), ('i', 1), ('j', 2), ('k', 443), 
#  ('l', 1304), ('m', 536), ('n', 647), ('o', 75), ('p', 753), ('r', 2417), 
#  ('s', 470), ('t', 1771), ('u', 134), ('v', 273), ('w', 93), ('x', 73), 
#  ('y', 49), ('z', 154)]


letters, vals = zip(*cnts_sorted)
print(letters)
# ('', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
#  'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
print(vals)
# (3, 26, 260, 729, 778, 29, 1034, 492, 765, 1, 2, 443, 1304, 536, 647, 75,
# 753, 2417, 470, 1771, 134, 273, 93, 73, 49, 154)

【讨论】:

  • 天哪,太好了!我以前见过zip(),但会更多地关注它。最后一部分是如何向后重新排列。我试过reversed(letters)/reversed(vals),但得到“TypeError:'reversed'类型的对象没有len()`。
  • “向后重新排列”是什么意思?你的意思是倒序排列?这就像将reverse=True 传递给sorted 一样简单:sorted(ie_count.items(), key=itemgetter(0), reverse=True)。或者只是使用“切片”来反转元组:reversed_letters = letters[::-1]reversed_vals = vals[::-1]
  • 啊,行了!非常感谢! :D(我将reverse = True 添加到sorted()
猜你喜欢
  • 1970-01-01
  • 2013-05-02
  • 2014-02-08
  • 2011-04-10
  • 1970-01-01
  • 2022-11-18
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多