Python. I want to know how to sort words by frequency, and if they are the same frequency, I want to know how to sort words alphabetically and print them out.

Asked 2 years ago, Updated 2 years ago, 115 views

start law year time joy time word family friend year tonight year law time joy work library word justice start time work president year start time president country joy family

If you give it as an input, you can use frequency, word as an element

[(2, 'work'), (2, 'word'), (2, 'family'), (1, 'justice'), (3, 'joy'), (4, 'year'), (1, 'library'), (1, 'country'), (1, 'tonight'), (3, 'start'), (5, 'time'), (2, 'president'), (2, 'law'), (1, 'friend')]

You can make a list like this

How can I give you the option to sort by frequency and sort by alphabetical order when the frequency is the same, like work and word?

Input starts law year time joy time word family friend tonight law time joy work library word justice start time work present year country joy family Cotton

Output should be time year joy start family law president word country friend justification library tonight

python sorting

2022-09-22 13:29

3 Answers

Press 'Do' below.

words = ["start","law","year","time","joy","time","word","family","friend","year","tonight","year","law","time","joy","work","library","word","justice","start","time","work","president","year","start","time","president","country","joy","family"]
words_count={}
for word in words:
    if word in words_count:
        words_count[word] += 1
    else:
        words_count[word] = 1
sorted_words = sorted([(k,v) for k,v in words_count.items()], key=lambda word_count: -word_count[1])
print([w[0] for w in sorted_words])

If you want to put in alphabetical alignment, you can use the comparator.

import functools

def comparator(x, y):
    return x[0]*x[1]-y[0]*y[1]


l = [[-50,5], [2,2], [1,2], [9,3]]
print(sorted(l, key=functools.cmp_to_key(comparator)))


2022-09-22 13:29

You can also do sorted twice as below.

from collections import Counter

s = "start law year time joy time word family friend year tonight year law time joy work library word justice start time work president year start time president country joy family"

sorted(sorted(Counter(s.split()).most_common(), key=lambda pair:pair[0], reverse=False), key=lambda pair:pair[1], reverse=True)
Out[59]: 
[('time', 5),
 ('year', 4),
 ('joy', 3),
 ('start', 3),
 ('family', 2),
 ('law', 2),
 ('president', 2),
 ('word', 2),
 ('work', 2),
 ('country', 1),
 ('friend', 1),
 ('justice', 1),
 ('library', 1),
 ('tonight', 1)]


2022-09-22 13:29

The asq (https://github.com/sixty-north/asq) module, which can be used like linq, is also useful.

from collections import Counter
from asq.initiators import query

s = "start law year time joy time word family friend year tonight year law time joy work library word justice start time work president year start time president country joy family"

query(Counter(s.split()).most_common()).order_by_descending(lambda pair:pair[1]).then_by(lambda pair:pair[0]).to_list()
Out: 
[('time', 5),
 ('year', 4),
 ('joy', 3),
 ('start', 3),
 ('family', 2),
 ('law', 2),
 ('president', 2),
 ('word', 2),
 ('work', 2),
 ('country', 1),
 ('friend', 1),
 ('justice', 1),
 ('library', 1),
 ('tonight', 1)]


2022-09-22 13:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.