gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:05:47 25 4
gpt4 key购买 nike

我想显示 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com