data = [['ABC', 12, 3, 100],
['DEF', 10, 5, 200],
['GHI', 13, 3, 1000]]
data.sort(key=lambda row: (row[2], row[3]), reverse=True)
print(data)
# [['DEF', 10, 5, 200], ['GHI', 13, 3, 1000], ['ABC', 12, 3, 100]]
Showing posts with label 2-dimensional list. Show all posts
Showing posts with label 2-dimensional list. Show all posts
12 June 2022
How to order a 2-dimensional list in Python?
https://scripteverything.com/python-2d-list-sort-by-multiple-columns-code-examples-no-imports-one-liners/#:~:text=To%20sort%20a%20two%2Ddimensional,to%20the%20required%20sort%20order.
01 May 2022
How to slice a number of columns from a 2-dimensional list in Python?
https://www.codegrepper.com/code-examples/python/python+multiaxis+slicing
# Basic syntax:
[your_list[i][start_col : end_col] for i in range(start_row, end_row)]
# Example usage:
your_list = [[1, 2, 3, 4, 5],[6, 7, 8, 9, 10],[11, 12, 13, 14, 15]]
# obtain rows 0-1 (inclusive) and cols 2-3 (inclusive)
[your_list[i][2:4] for i in range(0,2)]
--> [[3, 4], [8, 9]]
How to pick up a single column from a 2-dimensional list in Python?
https://stackoverflow.com/questions/30062429/how-to-get-every-first-element-in-2-dimensional-list
a = [[4.0, 4, 4.0], [3.0, 3, 3.6], [3.5, 6, 4.8]]
You can get the index [0]
from each element in a list comprehension
>>> [i[0] for i in a]
[4.0, 3.0, 3.5]
Subscribe to:
Posts (Atom)