0
0

pythonの内包表記のメモ

Last updated at Posted at 2024-01-19

Pythonの内包表記のメモを書きます。英語名で「List Comprehension」と呼びます。

s = ['......', '.#.#..', '......']

上記を、二次配列で表したいときのコードを書きます。

r = [[0 if v == '.' else '#' for v in row] for row in s]

上記は下記の内包表記です。

r = []
for row in s:
    r2 = []
    for v in row:
        if v == '.':
            r2.append(0)
        else:
            r2.append('#')
    r.append(r2)

メモとして残します。

0
0
3

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