
本文介绍了如何使用Python处理包含年份和计数的字典列表,目标是从中提取年份和计数,并将它们按年份升序排列,最终得到两个排序后的列表,以便于后续使用matplotlib进行数据可视化。本文将提供详细的代码示例和解释,帮助你理解和应用这种数据处理方法。
假设你有一个包含字典的列表,每个字典都包含一个年份作为键,以及对应的计数作为值。例如:
data = [
{'1994': 5}, {'1972': 1}, {'1974': 1}, {'2008': 2}, {'1957': 3},
{'1993': 1}, {'2003': 2}, {'1966': 1}, {'1999': 4}, {'2001': 3},
{'1980': 2}, {'2010': 2}, {'2002': 3}, {'1975': 1}, {'1990': 1},
{'1954': 2}, {'1977': 1}, {'1995': 4}, {'1991': 2}, {'1946': 1},
{'1997': 2}, {'1998': 2}, {'2014': 2}, {'1960': 1}, {'1968': 2},
{'1931': 2}, {'1942': 1}, {'2011': 1}, {'1936': 1}, {'2006': 3},
{'1981': 2}, {'1985': 1}, {'2000': 4}, {'1979': 2}, {'1940': 1},
{'1988': 2}, {'1950': 1}, {'1964': 1}, {'2017': 1}, {'2012': 2},
{'1986': 1}, {'1984': 2}, {'1941': 1}, {'1959': 1}, {'1958': 1},
{'1992': 1}, {'1983': 1}, {'2016': 2}, {'2007': 1}, {'1971': 1},
{'1962': 2}, {'2004': 1}, {'1944': 1}, {'1976': 1}, {'1952': 1},
{'2009': 2}, {'1987': 1}, {'1973': 1}, {'1948': 1}, {'1921': 1}
]首先,我们需要从这个列表中提取年份和计数,并将年份转换为整数类型。可以使用列表推导式来实现:
years = [int(list(d.keys())[0]) for d in data] counts = [list(d.values())[0] for d in data]
这段代码将 data 列表中每个字典的键(年份)提取出来,并转换为整数类型,存储在 years 列表中。同时,将每个字典的值(计数)提取出来,存储在 counts 列表中。
立即学习“Python免费学习笔记(深入)”;
接下来,我们需要根据年份对数据进行排序。为了保持年份和计数之间的一致性,我们需要同时对 years 和 counts 列表进行排序。一种常用的方法是使用 sorted 函数和 lambda 表达式:
sorted_indices = sorted(range(len(years)), key=lambda k: years[k]) sorted_years = [years[i] for i in sorted_indices] sorted_counts = [counts[i] for i in sorted_indices]
这段代码首先使用 range(len(years)) 生成一个索引列表,然后使用 sorted 函数对这个索引列表进行排序。key=lambda k: years[k] 指定了排序的依据是 years 列表中对应索引的值(即年份)。sorted 函数返回排序后的索引列表 sorted_indices。
然后,我们使用列表推导式,根据排序后的索引列表 sorted_indices,重新构建排序后的 sorted_years 和 sorted_counts 列表。
下面是完整的代码示例:
data = [
{'1994': 5}, {'1972': 1}, {'1974': 1}, {'2008': 2}, {'1957': 3},
{'1993': 1}, {'2003': 2}, {'1966': 1}, {'1999': 4}, {'2001': 3},
{'1980': 2}, {'2010': 2}, {'2002': 3}, {'1975': 1}, {'1990': 1},
{'1954': 2}, {'1977': 1}, {'1995': 4}, {'1991': 2}, {'1946': 1},
{'1997': 2}, {'1998': 2}, {'2014': 2}, {'1960': 1}, {'1968': 2},
{'1931': 2}, {'1942': 1}, {'2011': 1}, {'1936': 1}, {'2006': 3},
{'1981': 2}, {'1985': 1}, {'2000': 4}, {'1979': 2}, {'1940': 1},
{'1988': 2}, {'1950': 1}, {'1964': 1}, {'2017': 1}, {'2012': 2},
{'1986': 1}, {'1984': 2}, {'1941': 1}, {'1959': 1}, {'1958': 1},
{'1992': 1}, {'1983': 1}, {'2016': 2}, {'2007': 1}, {'1971': 1},
{'1962': 2}, {'2004': 1}, {'1944': 1}, {'1976': 1}, {'1952': 1},
{'2009': 2}, {'1987': 1}, {'1973': 1}, {'1948': 1}, {'1921': 1}
]
years = [int(list(d.keys())[0]) for d in data]
counts = [list(d.values())[0] for d in data]
sorted_indices = sorted(range(len(years)), key=lambda k: years[k])
sorted_years = [years[i] for i in sorted_indices]
sorted_counts = [counts[i] for i in sorted_indices]
print(sorted_years)
print(sorted_counts)本文介绍了如何使用Python处理包含年份和计数的字典列表,并将其按年份升序排列。这种方法可以应用于各种需要对数据进行排序和提取的场景,尤其是在数据可视化方面,可以方便地将数据转换为 matplotlib 等绘图库可以接受的格式。通过理解和掌握这些技巧,你可以更有效地处理和分析数据。
以上就是使用Python对字典列表按键排序并提取数据用于绘图的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号