[GO] Python> Sort by number and sort by alphabet> Use sorted ()

CheckIO (Python)> The Most Wanted Letter I tried> Searching for the most frequent alphabets> Using Counter in collections Looking at the related information on the subject, I received the following suggestions.

Let's suppose you got a list like this (maybe dict.items()): [("a", 2), ("b", 4), ("c", 5), ....] 1 Then you can get the answer with advanced sorting: sorted(array, key=lambda x: -x[1], x[0]) 1 And the required letter will be the first.

I have never used sorted ().

Reference: http://akiyoko.hatenablog.jp/entry/2014/09/26/235300

http://ideone.com/GoQ3Md

alist = [ ("b", 2), ("z", 5), ("a", 5), ("c", 4) ]
alist = sorted(alist, key=lambda x: (-x[1], x[0]))
print(alist)

run


[('a', 5), ('z', 5), ('c', 4), ('b', 2)]

Recommended Posts

Python> Sort by number and sort by alphabet> Use sorted ()
Python> Sort by number and sort by alphabet> Use sorted ()
Date and time ⇔ character string
Python 3 sorted and comparison functions
Python 3 sorted and comparison functions
Sort by date in python
About Python sort () and reverse ()
[Python] Sort iterable by multiple conditions
[python] week1-3: Number type and operation
Read and use Python files from Python
Difference between sort and sorted (memorial)
[Python] Difference between sorted and sorted (Colaboratory)
[Python] Use and and or when creating variables
Socket communication and multi-thread processing by Python
How to install and use pandas_datareader [Python]
Derivation of multivariate t distribution and implementation of random number generation by python
Use PIL and Pillow with Cygwin Python
[Python] Sort
[Python] How to use the enumerate function (extract the index number and element)
python: How to use locals () and globals ()
Socket communication by C language and Python
How to use Python zip and enumerate
How to use is and == in Python
Use Python and MeCab with Azure Functions
Divides the character string by the specified number of characters. In Ruby and Python.
[Python] Eliminate conditional branching by if by making full use of Enum and eval
Stock number ranking by Qiita tag with python
Firebase: Use Cloud Firestore and Cloud Storage from Python
[Python] Sort the table by sort_values (pandas DataFrame)
python development environment -use of pyenv and virtualenv-
Notify error and execution completion by LINE [Python]
[Python] How to use hash function and tuple.
[Python] How to sort instances by instance variables
Use Python and word2vec (learned) with Azure Databricks
Visualize how it is sorted by bubble sort
Split Python images and arrange them side by side
Sort by pandas
[Python] [Django] How to use ChoiceField and how to add options
Merge sort implementation / complexity analysis and experiment in Python
Python learning memo for machine learning by Chainer Chapters 1 and 2
Include and use external Kv files in Python Kivy
% And str.format () in Python. Which one do you use?
[Python] How to sort dict in list and instance in list
Shift the alphabet string by N characters in Python
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
The VIF calculated by Python and the VIF calculated by Excel are different .. ??
[Python] Sort spreadsheet worksheets by sheet name with gspread
Python> Get a list of files in multiple directories> Use glob | Sort by modification time
Python> Implementation> Does it contain numbers | Does it contain alphabet (upper and lower case)> Use sum (), map (), str.isalpha