0
0

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 1 year has passed since last update.

PythonでAtCoderをたまにしか書かないのでコードのメモを置く

Last updated at Posted at 2024-01-20

はじめに

私は普段AtCoderをC言語で書くのだがPythonの方が効率的・書きやすそうなときにはPythonで書くことがある。だが、そもそもAtCoderを暇な時にしかやらないということもあり、よくPythonの書き方を忘れるので今回Qiitaに自分用のメモを書くことにした。

Pythonメモ

C言語で言うところのscanf()の書き方
a = input() #1つの変数の場合
a = int(input()) #1つの変数でかつ、型がintのとき
x, y = input().split() #2つの変数の場合
x, y = map(int, input().split()) #2つの変数でかつ、型がintのとき
a = list(map(int, input().split())) #1つの変数(配列)に複数入力する(型はint)
配列
my_list = [1, 2, 3]
my_list.append(4)  #リストの末尾に4を追加

my_list = [1, 2, 4]
my_list.insert(2, 3)  #インデックス2の位置に3を挿入

my_list = [1, 2, 3, 5, 6]
my_list.insert(my_list.index(3) + 1, 4) # 3の後ろに4を追加

l = [0] * 10 # l = [0,0,0,0,0,0,0,0,0,0] 

print(*l) # 0 0 0 0 0 0 0 0 0 0

sorted() #昇順ソート
sorted(x, reverse=True) #降順ソート


とあるキーワードを含むかの探索
import re
 if re.search(r'\d{対象探索単語数}', 探索対象の変数):
    # または
 if re.search(r'\d{対象探索単語数}$', 探索対象の変数): # $をつけることで末尾を見る

# \wが\アルファベット文字(A-Z, a-z)、数字(0-9)、アンダースコア(_)の探索、\dは数字のみ
文字の置き換え
a.replace('変更予定の単語','変更後の単語')
 # または
import re
re.sub(r'\d{}$', 変更後の単語, 変更予定の変数名)

for
for i in range(開始インデックス, 終了インデックス, 増減率)

その他早見表
# 配列の文字のそれぞれの個数を数えたい
count_dic = SortedDict()
for a in A:
    if a not in count_dic:
        count_dic[a] = 0
    count_dic[a] += 1

0
0
1

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?