裏 RjpWiki

Julia ときどき R, Python によるコンピュータプログラム,コンピュータ・サイエンス,統計学

リスト内包表記は,リストしか使えない Python のひがみ

2021年02月13日 | ブログラミング

別に Python がリストしか使えない訳ではない

以下の例も,numpy ndarray などを使うとか,しかるべき関数を使えばリスト内包表記など使わなくてもできる話である。

しかしなぜか,「リスト内包表記はすごいんだぜ!」的な上から目線の記事が多いので,そうでもないよねというのを書いてみる。

# full_name = "Yamada Taro"
# characters = [char for char in full_name]

fullname = "Yamada Taro"
characters = split(fullname, "")

# Matrix = [[7, 1, 5],
#           [22, 99, 0],
#           [33, 2, 57]]
# row_min = [min(row) for row in Matrix]

M = [7 1 5; 22 90 0; 33 2 57]

rowmin = minimum(M, dims=2)

# People = ["Taro", "Jiro", 'Wan', "Elepahnt", "Tomas", "ken", "sae"]
# target = [name for name in People if len(name) < 4 and name.islower()]

people = ["Taro", "Jiro", "Wan", "Elepahnt", "Tomas", "ken", "sae"]
select(s) = (length(s) < 4) & all("a" .<= split(s, "") .<= "z")
target = people[select.(people)]

# Genius = ["taro", "jiro", "yamada", "ito"]
# target = [name.capitalize() for name in Genius]

genius = ["taro", "jiro", "yamada", "ito"]
target = titlecase.(genius)

# People = ["Taro", "Hiroyuki", "ken", "youu"]
# answer = [name if name.startswith('y') else 'Not Genius' for name in People]

people = ["Taro", "Hiroyuki", "ken", "youu"]
starts(s) = (s[1] == 'y') + 1
answer = ["not Genius", "youu"][starts.(people)]

# Genius = ["taro", "jiro", "yamada"]
# L = []
# for name in Genius:
#     for char in name:
#         L.append(char)

genius = ["taro", "jiro", "yamada"]
split(join(genius), "")

# People = ["Kenny", "Tomas", "tom", "young"]
# L = map(lambda a: a.lower(), People)

people = ["Kenny", "Tomas", "tom", "young"]
select(s) = all("a" .<= split(s, "") .<= "z")
L = lowercase.(people)

# L1 = filter(lambda a: len(a) < 4, People)
# L2 = [a for a in People if len(a) < 4]

people = ["Kenny", "Tomas", "tom", "young"]
L2 = people[length.(people) .< 4]

リスト内包表記とジェネレータのメモリー使用量

# large_list = [x for x in range(1_000_000)] # 8697440
# large_list_g = (x for x in range(1_000_000)) # 96

Base.summarysize(collect(1:1000000))
Base.summarysize(1:1000000)

# list_2d = [
#      [0, 0, 0],
#      [1, 1, 1],
#      [2, 2, 2],
# ]
# flat = [num ** 2 if num % 2 == 0 else num for row in list_2d for num in row]

# list_2d = [
#      [0, 0, 0],
#      [1, 1, 1],
#      [2, 2, 2],
# ]
# flat = []
# for row in list_2d:
#     for num in row:
#         flat.append(num ** 2 if num % 2 == 0 else num)

list2d = [0 0 0; 1 1 1; 2 2 2]
flat = vec(list2d' .^ 2)

いかがでしたでしょうか。リスト内包表記は記述法としてはわかりにくい(そもそも同じ処理を書いても,長くなりがち。長いプログラムはそれだけでよくない)。

対して,ベクトル処理できる関数で対処できる Julia は,関数名を見ただけでもわかる。R も Julia と同じ感じ(だからリスト内包表記などない)。Julia では,内包表記もできる(が,使う局面は少ない)

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

Julia と R で整数演算の違い

2021年02月13日 | ブログラミング

using RCall

R"x <- -5:8"      # -5 -4 -3 -2 -1  0  1  2  3  4  5  6  7  8
x = -5:8          # -5:8
x = collect(-5:8) # -5 -4 -3 -2 -1  0  1  2  3  4  5  6  7  8

R"x  + 1"  # -4 -3 -2 -1  0  1  2  3  4  5  6  7  8  9
  x .+ 1   # -4 -3 -2 -1  0  1  2  3  4  5  6  7  8  9

R"2*x + 3" # -7 -5 -3 -1  1  3  5  7  9 11 13 15 17 19
  2x .+ 3  # -7 -5 -3 -1  1  3  5  7  9 11 13 15 17 19

R"x %% 2"    #  1  0   1  0   1  0  1  0  1  0  1  0  1  0
  mod.(x, 2) #  1  0   1  0   1  0  1  0  1  0  1  0  1  0
  x .% 2     # -1  0  -1  0  -1  0  1  0  1  0  1  0  1  0
  rem.(x, 2) # -1  0  -1  0  -1  0  1  0  1  0  1  0  1  0

R"x %/% 5"              # -1  -1  -1  -1  -1  0  0  0  0  0  1  1  1  1
  x .÷  5               # -1   0   0   0   0  0  0  0  0  0  1  1  1  1
  div.(x, 5)            # -1   0   0   0   0  0  0  0  0  0  1  1  1  1
  fld.(x, 5)            # -1  -1  -1  -1  -1  0  0  0  0  0  1  1  1  1
  div.(x, 5, RoundDown) # -1  -1  -1  -1  -1  0  0  0  0  0  1  1  1  1
  div.(x, 5)            # -1   0   0   0   0  0  0  0  0  0  1  1  1  1
  div.(x, 5, RoundUp)   # -1   0   0   0   0  0  1  1  1  1  1  2  2  2
  round.(Int, x ./ 5)   # -1  -1  -1   0   0  0  0  0  1  1  1  1  1  2

R"x %% Inf" #  Inf  Inf  Inf  Inf  Inf   0   1   2   3   4   5   6   7   8
  x .% Inf  # -5.0 -4.0 -3.0 -2.0 -1.0 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0

R"x  + Inf" # Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf
  x .+ Inf  # Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf

R"x  * Inf" # -Inf -Inf -Inf -Inf -Inf NaN Inf Inf Inf Inf Inf Inf Inf Inf
  x .* Inf  # -Inf -Inf -Inf -Inf -Inf NaN Inf Inf Inf Inf Inf Inf Inf Inf

R"x  / Inf" #    0    0    0    0    0   0   0   0   0   0   0   0   0   0
  x ./ Inf  # -0.0 -0.0 -0.0 -0.0 -0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

PVアクセスランキング にほんブログ村

PVアクセスランキング にほんブログ村