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】便利記法集等(個人的メモ)

Last updated at Posted at 2021-11-20

個人的メモです. 随時追加します.

#文字列を数値に置換

  • yesなら0, noなら1みたいなやつ
import pandas as pd

data = pd.DataFrame({"ans": ["yes","no", "no", "yes", "no"]})
print("before\n", data)
data["ans"] = data["ans"].map({"yes":0, "no":1})
print("\nafter\n", data)

実行結果

before
    ans
0  yes
1   no
2   no
3  yes
4   no

after
    ans
0    0
1    1
2    1
3    0
4    1

部分文字列の判定

  • str型のオブジェクトS, T に対して S in TT の部分文字列に S を含むか判定

AtCoder ABC230 B問題解説より

S = input()
T = "oxx" * 10 ** 5
print("Yes" if S in T else "No")

文字列に数を埋め込む

  • .format() を使う
//名前は田中である. 88歳だ.

print("名前は{}である. {}歳だ.".format("Tanaka", 88))
print("名前は{1}である. {0}歳だ.".format(88, "Tanaka"))
print("名前は{name}である. {age}歳だ.".format(name="Tanaka", age=88))

  • {インデックス番号:初期指定} で書式を変えられる
  • インデックス番号は省略できる

集合 set() に関するあれこれ

  • sorted() は使えるけど sort() は使えない
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?