Processing of python3 that seems to be usable in paiza

python3 Something that seems to be usable

I am working on paiza. This is a memo for myself. I will continue to add it.

Input array storage process

#Split input and store in array
x, y, z = [int(x) for x in input_line.rstrip().split()]
print(x + y + z)

#Store input in an array without splitting
tem = [int(n) for n in input_line.replace(' ', '')]

#Multi-line input
input_ = [input() for i in range(len_)]

#Store in dictionary type
input_ = int(input())
dic = {input_[i].split()[0]: int(input_[i].split()[1]) for i in range(len(input_))}

Array sort

# reverse=True in descending order
nums = sorted(input_line, reverse=True)

for statement

#Loop with counters and elements
for i, s in enumerate(input):

for statement else

--The inside of else is executed when exiting the for statement --If you break in the middle, it will not be executed

for i in range(3):
    if i == 3:
        break; 
else:
    print('a')

Dictionary type for statement

# {'1': 0, '2': 0, 'neutral': 100}
for key, value in elect.items():

This and that using list comprehension

Count the conditions of specific data

data = range(1, 10)
count = len([x for x in data if x % 3 == 0]) #Count multiples of 3
print(count)

Flat double list

data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(data)

flat = [flatten for inner in data for flatten in inner]
print(flat)

Run


[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Operations around the list

End specification

a = [1, 2, 3, 4, 5]
print(a[len(a) - 1])    #Common end specifications
print(a[-1])            #Specifying the end using a negative index
print(a[-2])            #Second from the end

Run


5
5
4

slice

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a[3:7])
print(a[3:])    #Omission of end point
print(a[:7])    #Omission of start point
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a[2:8:2])     
#Get 2nd or more and less than 8th in 2step (skip one)

String constant

print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.ascii_letters)
print(string.digits)
print(string.hexdigits)

Run


abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
0123456789abcdefABCDEF

Supplement for dictionary division

Error handling when the key does not exist when dividing by a loop You can force it with try-except

count = []
for k in b_dic.keys():
	try:
		temp_count = a_dic[k] / b_dic[k]
                # math.floor:Truncate after the decimal point
		count.append(math.floor(temp_count)) 
	except KeyError:
		count.append(0)

reference

I tried to summarize the code that I often write when competing in Python (https://qiita.com/y-tsutsu/items/aa7e8e809d6ac167d6a1)

Recommended Posts

Processing of python3 that seems to be usable in paiza
What seems to be a template of the standard input part of the competition pro in python3
[Python] It seems that global variables cannot be referenced in Multiprocessing
A function that measures the processing time of a method in python
Python 3.9 dict merge (`|`) seems to be useful
Status of each Python processing system in 2020
Easy padding of data that can be used in natural language processing
[Python] A program that calculates the number of socks to be paired
Summary of how to import files in Python 3
View the result of geometry processing in Python
Summary of how to use MNIST in Python
A clever way to time processing in Python
[Road to Intermediate] Python seems to be all objects
[Python] There seems to be something called __dict__
A solution to the problem that the Python version in Conda cannot be changed
I want to create a priority queue that can be updated in Python (2.7)
How to find the coefficient of the trendline that passes through the vertices in Python
I tried to implement what seems to be a Windows snipping tool in Python
File processing in Python
Text processing in Python
Queue processing in Python
Various processing of Python
Solve addition (equivalent to paiza rank D) in Python
Summary of tools needed to analyze data in Python
Full-width and half-width processing of CSV data in Python
How to get the number of digits in Python
Syntax that Perl users tend to forget in Python
Solve multiplication (equivalent to paiza rank D) in Python
[Chapter 5] Introduction to Python with 100 knocks of language processing
One liner that outputs 1000000 digits of pi in Python
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Python] tkinter Code that is likely to be reused
[Python] pandas Code that is likely to be reused
[Chapter 2] Introduction to Python with 100 knocks of language processing
Things to keep in mind when processing strings in Python2
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Summary of date processing in Python (datetime and dateutil)
Things to keep in mind when processing strings in Python3
[Chapter 4] Introduction to Python with 100 knocks of language processing
Geographic information visualization of R and Python that can be expressed in Power BI
Learning history to participate in team application development in Python ~ After finishing "Introduction to Python 3" of paiza learning ~
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
A mechanism to call a Ruby method from Python that can be done in 200 lines
List of tools that can be used to easily try sentiment analysis of Japanese sentences in Python (try with google colab)
[Python] I examined the practice of asynchronous processing that can be executed in parallel with the main thread (multiprocessing, asyncio).
Summary of things that need to be installed to run tf-pose-estimation
How to test that Exception is raised in python unittest
UTF8 text processing in python
Parallel processing of Python joblib does not work in uWSGI environment. How to process in parallel on uWSGI?
Solve number sorting (equivalent to paiza rank D) in Python
To flush stdout in Python
python> Is it possible to make in-line comments?> It seems that it has to be on multiple lines
List of my articles that may be useful in competition pros (updated from time to time)
Login to website in Python
Solve word counts (equivalent to paiza rank C) in Python
How to set up a simple SMTP server that can be tested locally in Python
Asynchronous processing (threading) in python
Summary of points to keep in mind when writing a program that runs on Python 2.5
Equivalence of objects in Python
[Python] Programming to find the number of a in a character string that repeats a specified number of times.