はむこの勉強記録

http://bit.ly/2ktf20t の写し

AtCoder Reginner Contest 071 - C - 怪文書 / Dubious Document

arc071.contest.atcoder.jp

pythonのdictのイテレータはキーについてソートしてくれていない!dictがunordered_setなのかな

pythonにはin演算子がある。これはfind!=it.end()みたいなbool型。また、dict.keys()でキーのリストが手に入る。値のリストは、dict.values()。 ‘orange’ in dict1.keys()

pythonでのsortはsortedというグローバル関数を使う。

pythonでの文字列のsortは、'‘.join(sorted(s))とする。なぜかsのsortedがリストっで返してくるので。

pythonでは文字と文字列の区別がない。

‘ab’ * 3などで、'ababab'を生成できる。

順序付き辞書なるものが存在する。

from collections import OrderedDict

od = OrderedDict()

od[“apple”] = 1

od[“orange”] = 2

od[“banana”] = 3

string->listは簡単にできて、

list(‘hello’)

[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

ちなみに逆は、以下らしい

a = [‘a’, ‘b’, ‘c’, ’d']

‘’.join(a)

‘abcd’

index付きforループのためには、以下のようなことができる(O(n2)なので注意)。

lst = [‘A’,‘B’,‘C’]

{k: v for v, k in enumerate(lst)}

{‘A’: 0, ‘C’: 2, ‘B’: 1}

‘A’:0みたいなのは、結局(‘A’, 0)というタプルと同等。

カウンター専用のデータ構造が有るらしい。 stackoverflow.com

from collections import Counter

Counter([‘apple’,‘red’,‘apple’,‘red’,‘red’,‘pear’])

Counter({‘red’: 3, ‘apple’: 2, ‘pear’: 1})

listには.countという出現回数を取得する関数がある

char, integerの変換はchr, ordを使う(なんでchar, intじゃないの…)

chr(97)

‘a’

ord(‘a’)

97

ascii_lowercaseなるものがpredefined

import string

string.ascii_lowercase

‘abcdefghijklmnopqrstuvwxyz’