2
1

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.

map関数を使ってみる

Last updated at Posted at 2019-01-11

はじめに

multiprocessingを勉強していたらmap関数が出てきたので、まずは map関数を勉強してみました。

map関数

map(function, iterable, ...)
function を、結果を返しながら iterable の全ての要素に適用するイテレータを返します。追加の iterable 引数が渡されたなら、 function はその数だけの引数を取らなければならず、全てのイテラブルから並行して取られた要素に適用されます。複数のイテラブルが与えられたら、このイテレータはその中の最短のイテラブルが尽きた時点で止まります。

iterable → for文の中に入れられるリストとかタプルみたいなもの。
このiterableに対してfunctionをしてくれる。

使ってみる

[1,2,3] に対して+1する

result = map(lambda x: x+1, [1,2,3])
print(result)

返ってきた値

<map object at 0x7f26e9a17940>

イテレータという形で、私には結果が見えないので、リストに入れてあげる。
※イテレータはリストのコピーみたいなものらしいのですが、難しいのでまた今度。

result = list(map(lambda x: x+1, [1,2,3]))
print(result)
[2, 3, 4]

+1されて返ってきました\(^o^)/

参考

https://rara-world.com/python-map-filter/
lambdaについて
https://qiita.com/nagataaaas/items/531b1fc5ce42a791c7df

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?