Unexpectedly (?) Python tips

python trivia

I won't write the details here. Please check for yourself. What I want to tell you is that you can do this! It is an introduction.

Sort with the specified key

what is that? It is a feeling. For example, when sorting a 2D array, you can decide which column to sort by.

aa = [[1,2,3], [2,5,3], [3,3,4]]

When there is an array called, hmm, I want to sort in ascending order by the second number,

aa.sort(key=lambda x:x[1])

I can go. Find out more by yourself

Maximum value with the specified key

The same guy as before. Other elements can be letters.

aa = [["a",2], ["c", 90], ["d", 8]]

Or something

aa.max(key=lambda x:x[1])
#["c",90]Should come back

Check this out for yourself

When you want to erase duplicate elements

Use the set function. Remember this is so convenient It makes it a collective type. Details omitted

aa = [1,1,1,1,2,3,4,5]
set(aa)
#{1,2,3,4,5}

What is good about this is that you can see the number of elements without duplication.

len(set(aa))
#5

The set type is insanely helpful

End ♡

Recommended Posts

Unexpectedly (?) Python tips
python tips
python tips
Python Tips
Python tips
Python debugging tips
Python and numpy tips
Python Tips (my memo)
Python PyTorch install tips
[Blender x Python] Blender Python tips (11/100)
Python
Receiving standard input tips @ python
[Tips] Handle Athena with Python
[Python + Selenium] Tips for scraping
Google Drive Api Tips (Python)
~ Tips for beginners to Python ③ ~
Tips on Python file input / output
LaTeX, Python tips in master's thesis
[TouchDesigner] Tips for for statements using python
Tips for calling Python from C
Write python list fast vim tips
[VS Code] ~ Tips when using python ~
kafka python
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python FlowFishMaster
Python service
Tips to make Python here-documents easier to read
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
Jupyter Tips 4
install python
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
numpy tips
Tips for dealing with binaries in Python
Installing Python 3.4.3.
Python memo
Python iterative
Jupyter Tips 5
Python algorithm
Python2 + word2vec
Scapy Tips
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals