Python3 programming functions personal summary

at first

This is a personal reminder of Python 3 libraries and functions used in competitive programming and more.

List in order

python


#List from start to end
# [1,...,100]If you want start=1,end=100
li = list(range(start,end+1,step))

Invert list elements

python


li.reverse()
#Or
li[::-1]

Sum of elements in the list

python


sum(li)

Sort the list

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)

Sort the list with your own comparison function

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

Python3 programming functions personal summary
Python web programming article summary
Python Competitive Programming Site Summary
Python Summary
Python functions
Python summary
Python package management tool personal summary
Python programming note
Python tutorial summary
"Python Machine Learning Programming" Summary Note (Jupyter)
Programming in python
#Python basics (functions)
[Beginner] Python functions
Python personal Q.A
python related summary
python personal notes
Python Easy-to-use functions
Python basics: functions
Python basics summary
Python> Personal Notes> PEP-0008 Summary> Programming Recommendations Section Only / PEP8 Check Tool Trial
Python Beginner's Guide (Functions)
Summary about Python scraping
Python Django tutorial summary
Pandas Personal Notes Summary
missingintegers python personal notes
3. 3. AI programming with Python
Programming study site summary
[Python] Memo about functions
Summary about Python3 + OpenCV3
keyhac Personal settings summary
Competitive programming diary python 20201213
Python memorandum (personal bookmark)
Python function argument summary
Python programming with Atom
[Personal notes] Python, Django
Competitive programming diary python 20201220
Competitive programming with python
Python directory operation summary
Python AI framework summary
Python iteration related summary
# 4 [python] Basics of functions
Programming primary information summary
Python built-in functions ~ Zip ~
Python programming in Excel
Summary of Python arguments
LEGO Mindstorms 51515 Python Programming
[Python] Dynamic programming ABC015D
Wrap Python built-in functions
Competitive programming diary python
Programming with Python Flask
Curry arbitrary functions with Python ....
Programming with Python and Tkinter
Python Programming Workshop-Super Introductory Vol.3
Python> lambda> tiny functions / callback functions
Getting Started with Python Functions
Python Machine Learning Programming Chapter 2 Classification Problems-Machine Learning Algorithm Training Summary
Summary of python file operations
Summary of Python3 list operations
Atcoder Acing Programming Contest Python
What's new in Python 3.10 (Summary)
[Python] Dynamic programming knapsack problem