LoginSignup
3
4

More than 5 years have passed since last update.

R の chartr() を Python でやるには

Posted at

R には chartr() という便利な関数がある。
これは、二つの文字列を指定すると、それらの対応する一文字ずつをそれぞれ置換するという関数である。
実際に例を見てみる。

R
txt <- "I like orange"
chartr("abcde", "ABCDE", txt)
結果
[1] "I likE orAngE."

これを、Python でやるには、次のようにする。

Python
import string

txt = "I like orange."
trans = string.maketrans("abcde", "ABCDE")
result = txt.translate(trans)
print(result)
結果
I likE orAngE.

Enjoy!

参考

3
4
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
3
4