This is a personal reminder of Python 3 libraries and functions used in competitive programming and more.
python
#List from start to end
# [1,...,100]If you want start=1,end=100
li = list(range(start,end+1,step))
python
li.reverse()
#Or
li[::-1]
python
sum(li)
python
#Sort the original list ascending order
li.sort()
#descending order
li.sort(reverse=True)
#Returns a list sorted in ascending order (the original list remains unchanged)
li_2 = sorted(li)
#descending order
li_2 = sorted(li,reverse=True)
python
from functools import cmp_to_key
#Descending sort
def comp(x,y):
if x >= y:
#If you don't have to replace it-Returns 1
return -1 #When you want to sort in descending order, x>=If it is in the order of y, you can leave it as it is,-Returns 1
else:
#Returns 1 if you want to swap
return 1 #When you want to sort in ascending order, x<If it is in the order of y, I want to replace it, so return 1
#Sorting based on comparison function
li.sort(key=cmp_to_key(comp))
Recommended Posts