0
2

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 5 years have passed since last update.

集合内包表記

0
Posted at
1
s = set()
for i in range(10):
    s.add(i)
print(s)
1の実行結果
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

これを内包表記にすると

内包表記1
s = {i for i in range(10)}
print(s)
内包表記1の実行結果
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

リスト内包表記の時と同様に、
3で割って余り0の数字のみ集合に入れるには

内包表記2
s = {i for i in range(10) if i % 3 == 0}
print(s)
内包表記2の実行結果
{0, 3, 6, 9}
0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?