18
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

paizaで個人的にお世話になった記法【Python編】

Last updated at Posted at 2021-07-23

Progateを一通り終えたら次はpaizaだ!

そんな声を聞いたのでやり始めた去年の夏。
Dランクですら回答がままならないのに凹んでた学習中の私。
色々躓きまくったが、ロジック力やデバック力やググり力が付いたので結果的にとても良く、今でもちょっとややこしい処理を書く時にはあの時の経験を活かして解決できていると自負してる。

Aランク目指してたまーにやろうかなと思ったから復習まとめ&これから勉強して行く初学者の方達に少しでも力になれればと、paizaの問題でよく使った入力と出力とかの記法を纏めます。(Bランク相当)
思いついたりしたら新しく足して行くかも

標準入力編

スペース区切りの数値をリストに
30 30 10
num_list = [int(i) for i input().split()]
print(num_list)

[30, 30, 10]

スペース区切りの数値をそれぞれの変数に
30 30 10
num1, num2, num3 = list(map(int, input().split()))
print(num1, num2, num3)

30, 30, 10

連続した数値の入力処理
3
6 
7 
8 
num_list =[ int(input()) for i in range(4)]
print(num_list)

[3, 6, 7, 8]

連続した入力を辞書型に
"A" 3
"B" 5
"C" 5
num_dic = {}
for i in range(3):
    word, num = input().split()
    num_dic[word] = int(num)

print(num_dic)
    

{"A": 1, "B": 2, "C": 3}

多次元配列
66737
56173
59414
25469
63955
num_list = []

for i in range(5):
    nums = list(map(int, input()))
    num_list.append(nums)
    
print(num_list)

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

出力編

辞書型
num_dic = {"A": 1, "B": 2, "C": 3}

for word, num in num_dic.items():
  print(word, num)

"A", 3
"B", 5
"C", 5

多次元配列

その1

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

for i in range(3):
  for j in range(3):
    print(num_list[i][j])

1
2
3
4
5
6
7
8
9

その2

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

for nums in num_list:
  print(nums)
  for num in nums:
    print(num)

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

末尾のスペース消せなくて困ってる場合
word_list = [["A", "B", "C"], ["D", "E", "F"], ["G", "I", "H"]]

for i in word_list:
    print(' '.join(map(str, i)))

A B C
D E F
G I H

番外編

アルファベットのリスト
alpha_list1 = []
alpha_list2 = []
for i,c in enumerate(range(ord('A'),ord('Z')+1)):
    alpha_list1.append(chr(c))
print(alpha_list1)
for i,c in enumerate(range(ord('a'),ord('z')+1)):
    alpha_list2.append(chr(c))
print(alpha_list2)

(iには0からの数字が入っている。なくても動く)

[['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

リストのソート

昇順

num_list = [3, 4, 2, 1]
num_list.sort()
print(num_list)

[1, 2, 3, 4]

降順

num_list = [3, 4, 2, 1]
num_list.sort(reverse=True)
print(num_list)

[4, 3, 2, 1]

18
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?