Python knowledge notes that can be used with AtCoder

Preface

I will write it because it has the meaning of leaving a memo of knowledge that I forget to write even though I think that it is not often used in AtCoder. The explanation is shallow, so if you want to know more, please see other articles.

Search for the number of the element in the list

It is often checked whether the variable a is included in the list of data by ʻif a in data`. However, I was impressed if there was something that I could easily write back about what number it was in, so I will introduce it.

qiita1.py


box = [4, 2, 1, 0, 3]
print(box.index(3))
#4

By using ʻindex ()` in this way, the answer that the value of 3 is the fourth is returned.

Sort by specifying multiple keys in a multidimensional list

It's very difficult to say the headline, but in simple terms, it feels like you can set priorities when sorting a multidimensional list. I think this is still difficult, but ...

box = [[1,1,8], [8,6,4], [5,9,8]]

If you have such a multidimensional list, you might think like this. "I want to sort them in ascending order, but I want them to be in ascending order ** in the order of the third number, the first number, and the second number." When this happens, the lambda expression comes into play.

qiita2.py


box = [[1,1,8], [8,6,4], [5,9,8]]

box_sort = sorted(box)
print(box_sort)
#[[1, 1, 8], [5, 9, 8], [8, 6, 4]]

box_sort = sorted(box, key=lambda box_i:(box_i[2], box_i[0], box_i[1]))
print(box_sort)
#[[8, 6, 4], [1, 1, 8], [5, 9, 8]]

You can do this. The result will change because the priority when sorting is sorted in the order of writing. There is a similar subject in the heading below, so please see that as well. I won't explain about lambda expressions. References ([Python] Sort multidimensional list by multiple keys)

When you want to specify multiple keys in a multidimensional list and use either ascending or descending order.

box=[[1,1,8], [8,6,4], [5,9,8]]

If you have such a multidimensional list, you might think like this. "I want you to make ** ascending, descending, ascending ** in the order of the third number, the first number, and the second number." If this happens, you can't do it with the heading above, right? ?? I think

qiita3.py


box = [[1,1,8], [8,6,4], [5,9,8]]

box_sort = sorted(box, key=lambda box_i:(box_i[2], box_i[0], box_i[1]))
print(box_sort)
#[[8, 6, 4], [1, 1, 8], [5, 9, 8]]

box_sort = sorted(box, key=lambda box_i:(box_i[2], -box_i[0], box_i[1]))
print(box_sort) 
#[[8, 6, 4], [5, 9, 8], [1, 1, 8]]

By adding - in this way, the value is multiplied by -1, so the larger the value, the smaller the value. You can use this in descending and ascending order. For the problem of ABC128_B, I think that it will be a good example to study if you solve it using this lambda expression.

I want to manipulate the dictionary type

After creating a dictionary type using Counter etc., I remember struggling for a while with how to process the dictionary type, so I will write it easily. keys (): Only the key for each element can be retrieved. values (): Only the values for each element can be retrieved. ʻItems () `: Both key and value for each element can be retrieved. It is used when taking out using these three or turning with for.

References (Python dictionary (dict) for loop processing (keys, values, items) )

Finally

Since it was the first time, I used polite language, but I think it will be sloppy from the next time. Since this is my first post, please contact me if you have any incorrect information or bad manners. Please note that the memo is also meaningful.

Postscript

There was a mistake in the contents of strip (), it turned out that there is no need to use strip () because the error does not actually occur where the previous description says that an error occurs. Therefore, the description there has been deleted. Dissemination of uncertain information We are very sorry.

Recommended Posts

Python knowledge notes that can be used with AtCoder
File types that can be used with Go
Mathematical optimization that can be used for free work with Python + PuLP
Japanese can be used with Python in Docker environment
I made a familiar function that can be used in statistics with Python
[Python] Basic knowledge used in AtCoder
SSD 1306 OLED can be used with Raspberry Pi + python (Note)
Scripts that can be used when using bottle in Python
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
[Python] Make a graph that can be moved around with Plotly
Can be used with AtCoder! A collection of techniques for drawing short code in Python!
I made a shuffle that can be reset (reverted) with Python
Python standard input summary that can be used in competition pro
Python standard module that can be used on the command line
Understand the probabilities and statistics that can be used for progress management with a python program
I created a template for a Python project that can be used universally
Acoustic signal processing module that can be used with Python-Sounddevice ASIO [Application]
Acoustic signal processing module that can be used with Python-Sounddevice ASIO [Basic]
Functions that can be used in for statements
List packages that can be updated with pip
How to install a Python library that can be used by pharmaceutical companies
Basic algorithms that can be used in competition pros
Color list that can be set with tkinter (memorial)
ANTs image registration that can be used in 5 minutes
Can be used in competition pros! Python standard library
[Django] About users that can be used on template
Limits that can be analyzed at once with MeCab
Convert images from FlyCapture SDK to a form that can be used with openCV
File sharing server made with Raspberry Pi that can be used for remote work
List of tools that can be used to easily try sentiment analysis of Japanese sentences in Python (try with google colab)
Light blue with AtCoder @Python
Solve AtCoder ABC 186 with Python
Format summary of formats that can be serialized with gensim
It seems that Skeleton Tracking can be done with RealSense
Basic knowledge of DNS that can not be heard now
pd.tseries.offsets.DateOffset can be quite slow if not used with caution
[Python] Variadic arguments can be used when unpacking iterable elements
Goroutine (parallel control) that can be used in the field
Goroutine that can be used in the field (errgroup.Group edition)
I investigated the pretreatment that can be done with PyCaret
Let's make a diagram that can be clicked with IPython
Install Mecab and CaboCha on ubuntu16.04LTS so that it can be used from python3 series
[Python3] Code that can be used when you want to resize images in folder units
[Atcoder] [C ++] I made a test automation tool that can be used during the contest
TensorFlow 2.2 can't be installed with Python 3.8!
[Python] Draw elevation data on a sphere with Plotly and draw a globe that can be rotated round and round
Until torch-geometric can be used only with Windows (or Mac) CPU
[Python] I made my own library that can be imported dynamically
I made a package that can compare morphological analyzers with Python
Investigation of DC power supplies that can be controlled by Python
Web scraping with Python ① (Scraping prior knowledge)
I tried to expand the database so that it can be used with PES analysis software
Make a Spinbox that can be displayed in Binary with Tkinter
33 strings that should not be used as variable names in python
(Note) Be careful with python argparse
A timer (ticker) that can be used in the field (can be used anywhere)
Solve AtCoder Problems Recommendation with python (20200517-0523)
I bought and analyzed the year-end jumbo lottery with Python that can be executed in Colaboratory
Install packages that need to be compiled with Python3 with pip [Windows]
requirements.txt can be commented out with #
Solved AtCoder ABC 114 C-755 with Python3