9cubed
ブログ | Tailwind | Vite | Python | Node.js | Linux | PowerShell | その他 | 将棋ウォーズ | 歌の練習
< 前の記事

Python - yield

次の記事 >

Python - filter

Python

Python - map

公開日:2019-03-12
更新日:2019-05-29

1. 概要

map の使い方です。
map は、リストなどの各要素に対して、関数を適用したい場合に使います。
戻り値はイテレーターとなります。元のリストなどには影響はありません。
また、map() 実行時には、まだ関数は適用されません。for などで要素を取り出す際に実行されます。

map(適用する関数, リストなど)

2.1 サンプル

map の動作確認のためのサンプルです。

def inc(value):
	return value + 1


lst = [1,2,3,4,5]
m = map(inc, lst)

for item in m:
	print(item)

print(lst)
2
3
4
5
6
[1, 2, 3, 4, 5]

2.2 ラムダ式を使ったサンプル

for item in map(lambda v: v * 2, [1,2,3,4,5]):
	print(item)
2
4
6
8
10

3. 遅延評価の確認

map() 実行時には関数が適用されないことを確認します。

def inc(value):
	print("inc()処理中")
	return value + 1


lst = [1,2,3,4,5]
m = map(inc, lst)

print("この段階では、まだ関数は実行されません。")

print("-----")

for item in m:
	print(item)

print("-----")

print(lst)
print(type(m))

この段階では、まだ関数は実行されません。
-----
inc()処理中
2
inc()処理中
3
inc()処理中
4
inc()処理中
5
inc()処理中
6
-----
[1, 2, 3, 4, 5]
<class 'map'>


< 前の記事

Python - yield

次の記事 >

Python - filter

YouTube X

新着一覧

  • SCSS のインストールVite
  • Tailwind CSS のプロジェクトの作成Tailwind
  • TypeScriptのプロジェクトの作成Vite
  • Flask のインストールと動作確認Python
  • 簡易Webサーバーの作成Python
  • pipeline で文章の生成Python
  • pipeline で文章の要約Python
  • 音声から文字起こしPython
  • Node.js のインストールNode.js
  • .ps1(PowerShellスクリプト)を実行可能にするPowerShell

アーカイブ

  • 2025/12
  • 2025/11
  • 2025/10
  • 2025/09
  • 2025/08

以前のカテゴリー一覧

  • CakePHP3
  • CentOS7
  • HTML・CSS・JavaScript
  • Haskell
  • JavaScript
  • Kotlin
  • Laravel5
  • PHP
  • Python
  • Ruby
  • RubyOnRails5
  • TypeScript
  • Vue.js
  • Webサーバ講座
  • Webプログラミング講座
  • jQuery
  • linux
  • パソコン講座
  • ブログ
  • プログラミング講座
  • メモ帳作成講座
  • 数学

Copyright © 9cubed. All Rights Reserved.

プライバシーポリシー 利用規約
▲