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 zip(), set() の挙動を調べた

Last updated at Posted at 2023-02-10

最近LeetCodeにハマっておりまして、その中で得た知見を備忘的に残していこうと思います。

zip()の挙動

main.py
strs = ["flower","flow","flight"]

for a in zip(strs):
  print(a)

# 出力 
# ('flower',)
# ('flow',)
# ('flight',)

zip(*strs)とした場合

main.py
strs = ["flower","flow","flight"]

for a in zip(*strs):
  print(a)

# 出力 
# ('f', 'f', 'f')
# ('l', 'l', 'l')
# ('o', 'o', 'i')
# ('w', 'w', 'g')

このアスタリスク何?となった場合「こちら」が参考になった。
ちなみにこのアスタリスクは引数展開

set()の挙動

main.py
sets1 = [1, 2, 3]
sets2 = [1, 2, 3, 1, 2, 3]
sets3 = [1, 2, 3, 1, 2, 3, 1, 2, 3]

s1 = set(sets1)
s2 = set(sets2)
s3 = set(sets3)

print(s1)
print(s2)
print(s3)

# 出力 
# {1, 2, 3}
# {1, 2, 3}
# {1, 2, 3}

set()は要素をまとめて、重複した要素は取り除いてくれるらしい。
参考にしたのは「こちら」

0
0
2

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?