Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

sort a list of array python

from operator import itemgetter
A = [[10, 8], [90, 2], [45, 6]]
print("Sorted List A based on index 0: % s" % (sorted(A, key=itemgetter(0))))
B = [[50, 'Yes'], [20, 'No'], [100, 'Maybe']]
print("Sorted List B based on index 1: % s" % (sorted(B, key=itemgetter(1))))

"""
Output:
Sorted List A based on index 0: [[10, 8], [45, 6], [90, 2]]
Sorted List B based on index 1: [[100, 'Maybe'], [20, 'No'], [50, 'Yes']]
"""
Comment

sort an array python

#List
myList = [1,5,3,4]
myList.sort()
print(myList)
	#[1,3,4,5]
Comment

how to sort a list in python

l=[1,3,2,5]
l= sorted(l)
print(l)
#output=[1, 2, 3, 5]
#or reverse the order:
l=[1,3,2,5]
l= sorted(l,reverse=True)
print(l)
#output=[5, 3, 2, 1]
Comment

how to sort a list in python

old_list = [3,2,1]
old_list.sort()
Comment

sort a list python

"""Sort in ascending and descending order"""
list_test = [2, 1, 5, 3, 4]

#ascending is by default for sort
#Time Complexity: O(nlogn)
list_test.sort()

#For descending order
#Time Complexity: O(nlogn)
list_test.sort(reverse=True)

#For user-define order
list_test.sort(key=..., reverse=...) 
Comment

sort lists in python

stuff_1 = [3, 4, 5, 2, 1]
stuff_1.sort()
print(stuff_1)      # Output: [1, 2, 3, 4, 5]
stuff_2 = ['bob', 'john', 'ann']
stuff_2.sort()
print(stuff_2)      # Output: ['ann', 'bob', 'john']

stuff_4 = ['book', 89, 5.3, True, [1, 2, 3], (4, 3, 2), {'dic': 1}]
# print(stuff_4.sort())
# This will give us an error
# TypeError: '<' not supported between instances of 'int' and 'str'
Comment

sort an array in python

Sort an array:
array = [4, 5, 7, 6, 2, 3, 8]
print(sorted(array))
Comment

PREVIOUS NEXT
Code Example
Python :: user input python 
Python :: colab version python 
Python :: root mean squared error 
Python :: getting started with machine learning 
Python :: # How to Prints the current working directory in python 
Python :: when button is clicked tkinter python 
Python :: python import file from parent directory 
Python :: loop through list of dictionaries python 
Python :: python check if array is subset of another 
Python :: python using datetime as id 
Python :: read emails from gmail python 
Python :: check all values in dictionary python 
Python :: python soap 
Python :: python get parent directory 
Python :: empty dictionary python 
Python :: python - change the bin size of an histogram+ 
Python :: max of three numbers in python 
Python :: numpy vector multiplication 
Python :: odd or even in python 
Python :: cv2.namedWindow 
Python :: pandas data profiling 
Python :: effektivwert python 
Python :: cut rows dataframe 
Python :: numpy check if an array is all zero 
Python :: python rdp server 
Python :: SystemError: tile cannot extend outside image 
Python :: numpy 3 dimensional array 
Python :: pygame size of image 
Python :: python 3 f string float format 
Python :: how to get the ip address of laptop with python 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =