1
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 3 years have passed since last update.

AtCoder用Python3 個人メモ

Last updated at Posted at 2019-05-26

ありとあらゆる基本を忘れた自分用。

入力の読み取り

整数の入力

n = int(input())

・スペース区切りの整数の入力

b, c = map(int, input().split())

・文字列の入力

s = input()

・文字列の分割

list = s.split("",)

・出力

print(a+b+c)

・文字列を数字のリストとして読み取る

a = [int(i) for i in input().split()] 

もしくは

a = list(int(i) for i in input().split())  

再帰関数の設定

import sys
sys.setrecursionlimit(10**8)

文字、数字の変換

文字の変換

ord("a")

出力方法

・配列を結合して出力する

','.join(map(str,list))
ans = ' '.join(map(str,setA))

数字から文字の変換

chr(65) #A
chr(90) #Z

chr(97) #a
chr(122) #z

大文字、小文字変換

s.upper()
s.lower()

文字列の反転

s[::-1]

文字列のソート

sorted(s)
sorted(s,reverse=True)

if文

・基本

if [条件式]:
  if 処理
elif:
  elif 処理
else:
  else 処理

・比較演算子

and
or
!= 
is not
\>
\>=
\<
\<=

for 文

for i in range(num):
for i in list:

・continue

for x in range(5):
    if x == 3:
        continue
    print(x)

・break


for x in range(5):
    if x == 3:
        break
    print(x)

型変換

int()
str()

list

・宣言

[]

・追加

list.append()

・リスト同士の結合

extend

・ソート

list.sort()

・逆順ソート

list.sort(reverse=True)

・第2引数でのソート

    for i in range(n):
        x,l  = map(int, input().split())
        list.append((x-l,x+l))
    list= sorted(list,key=lambda list:list[1])

・最大値

list.max()

・最小値

list.min()

・indexの取得

num_list.index(max(num_list))

・初期値のリスト

l = [0] * 10

・任意の初期値


list = [i for i in range(6)]

・listを文字列に変換する

ans_list = map(str, ans)
ans_str =' '.join(ans_list)

・合計

sum(list)

・要素数の取得


list.count('a')

・リストの部分取得

list[0:len(list)]

・リストの偶数番目の取得

list[0::2]

・リストの奇数番目取得

list[1::2]

・二次元配列の初期化

[[0] * w for i in range(h)]

・逆順での表示

list[::-1]

dict

・宣言

{}

・keyの取得

dict.keys()

・値の取得

dict.values()
dict['key']

・値の追加、上書き

dict[key]=value

・要素の削除

del dict['a']

・keyの確認

if 'a' in dict1

・sort
降順

dict = sorted(dict.items())

昇順

dict2 = sorted(lits_i .items(), reverse=True)

setからの変換

list(setA)

counter を利用する
listから利用することで、valに出現回数を持てる

from collections import Counter
list1 = ['a', 'b', 'c', 'd', 'a', 'a', 'b']
dict1 = Counter(a)

dict2 = Counter()


Conter()で初期化することでinの冗長な記載を防げる

dict2 = Counter()

dict2["a"]=1

most_common()でvalueの値が大きい順に取り出す。

for i,j count in dict:
    print(i,j)

key とvalでdictを作る

    val = [int(i) for i in input().split()]
    key = list(range(1,n+1))
    dict = dict(zip(key,val))
    a2 = sorted(a,reverse=True)

set

s = {1, 2, 2, 3, 1, 4}

list = [1, 2, 2, 3, 1, 4]
setList = set(list)

・差集合

a=[1,2,3]
b=[3,4,5]
set(a)&set(b)

・和集合

a=[1,2,3]
b=[3,4,5]
set(a)|set(b)

・順番を維持したままsetする

 sorted(set(a), key=a.index)

数値計算

・絶対値

abs()

・商

//

・余

%

・ルート計算


math.sqrt(3)

・階乗

import math

math.factorial(5)

・乗算

r=5
n=10
mod=10**9+7

pow(r,n,mod)

Boolean

・flaot型の整数値チェック

f_i.is_integer()

tuple

値のコピー

import copy
copy.deepcopy(a)

heapq

常にソートされているリスト、最小と最大を扱う場合に便利

import heapq

heapq.heapify(x)
heapq.heappop(heap)
heapq.heappush(heap, item)

こまったら
https://docs.python.org/ja/3/library/heapq.html

deque

from collections import deque

leftList = deque()
leftList.append(str(1))
leftList.appendleft(str(2))

queue

・FIFO
幅優先探索とかで利用

import queue

if __name__ == '__main__':
    n = int(input())
    q1=queue.Queue()
    for i in range(n):
      q1.put(i)

    while q1.qsize() > 0:
        print(q1.get())

・FIFO
深さ優先探索とかで利用

import queue

if __name__ == '__main__':
    n = int(input())
    q2=queue.LifoQueue()
    for i in range(n):
      q2.put(i)

    while q1.qsize() > 0:
        print(q2.get())

・Priority
優先度付き

import queue

if __name__ == '__main__':
    n = int(input())
    q3=queue.PriorityQueue()
    for i in range(n):
      q3.put(i)

    while q1.qsize() > 0:
        print(q3.get())

bisect

ソート済のリスト
https://docs.python.org/ja/3/library/bisect.html

import bisect
// insert
index = bisect.bisect_left(a, 3) 

index = bisect.bisect_right(a, 3) 

bisect.insort_left(a,3)


// insertなしの挿入位置
score=[i for i in range(0,n+1)]

index  = bisect(breakpoints, score)

score[k2]

重複ありの順列

    l = [0,1]
    pairs = itertools.product(list1, repeat=4)

重複なしの順列

    l = [0,1]
    pairs = list(itertools.product(l, repeat=k))

#組み合わせ
https://docs.python.org/ja/3/library/itertools.html#itertools.combinations

import itertools

l = [1, 2, 3, 4, 5]

pairs = []
for i in range(2, len(l)+1):
    pairs += list(itertools.combinations(l, i))

累積和

from itertools import accumulate
a =[0,1,2,3,4,5]
accumulate(a)

2進数などへの変換

bin(i)
oct(i)
hex(i)

論理演算

and
x & y
or
x | y
xor
x ^ y
not
x ~ y
bit shift
x << y
x >> y

1
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
1
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?