python - 按升序对元组列表进行排序

标签 python python-3.x sorting tuples

我想显示 ID 和标签,并按顶部标签升序对它们进行排序。

word_counts = word_tuple.reduceByKey(lambda total, count: total + count)
word_counts.take(30)

[('id,tags', 1),
 ('"16586898","', 1),
 ('javascript', 3276),
 ('data-structures', 48),
 ('documentation', 1153),
 ('data-visualization', 10),
 ('"', 27109),
 ('"1828522","', 1),
 ('api', 634),
 ('console', 19),
 ('installation', 17),
 ('glassfish', 5),
 ('admin', 3),
 ('"25883048","', 1),
 ('regex', 500),
 ('bash', 375),
 ('sed', 56),
 ('"1879493","', 1),

最佳答案

一种方法是将 sortedkey 参数一起使用:

sorted(lst, key=lambda x: x[1])

# [('id,tags', 1),
#  ('"16586898","', 1),
#  ('"1828522","', 1),
#  ('"25883048","', 1),
#  ('"1879493","', 1),
#  ('admin', 3),
#  ('glassfish', 5),
#  ('data-visualization', 10),
#  ('installation', 17),
#  ('console', 19),
#  ('data-structures', 48),
#  ('sed', 56),
#  ('bash', 375),
#  ('regex', 500),
#  ('api', 634),
#  ('documentation', 1153),
#  ('javascript', 3276),
#  ('"', 27109)]

关于python - 按升序对元组列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49087838/

相关文章:

python范围问题

python - 如何在 SQLAlchemy 对象实例中查询一对多关系?

javascript - 重新排列 javascript 对象 - 使用 .sort() 的键不会保留在正在排序的数组上

javascript - 如何使用Javascript计算数组中的相邻数字?

python - 排序 XML 文件

python - 使用带有 sys.stdout 的 Python 'with' 语句

python - 如何在 GitPython 中创建 Git 拉取请求

python - 如何在特定时间发送消息 TelegramBot

python - PyQt5 tableView 显示底片是红色和 (x.xx)

mysql - libpython3.6-dev、libpython3.6 和 mysqlclient 未在 ubuntu 18.04 上安装