I am working on paiza. This is a memo for myself. I will continue to add it.
#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_))}
# reverse=True in descending order
nums = sorted(input_line, reverse=True)
#Loop with counters and elements
for i, s in enumerate(input):
--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')
# {'1': 0, '2': 0, 'neutral': 100}
for key, value in elect.items():
data = range(1, 10)
count = len([x for x in data if x % 3 == 0]) #Count multiples of 3
print(count)
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]
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
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)
print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.ascii_letters)
print(string.digits)
print(string.hexdigits)
Run
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
0123456789abcdefABCDEF
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)
I tried to summarize the code that I often write when competing in Python (https://qiita.com/y-tsutsu/items/aa7e8e809d6ac167d6a1)
Recommended Posts