简体   繁体   中英

Ordering list of tuples by ascending order

I want to show IDs and tags and sort them by top tags by ascending order.

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),

One way is to use sorted with key parameter:

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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM