No More Retake

3DCG屋さん向けTips&Referenceサイト

メニュー

『内包表記』って便利 by Python

Pythonには”リスト内包表記”なるものがあるそうな。
初めて目にした時は、「なんじゃこのケッタイな書き方は!」と思ったものです。

リスト内包表記をザックリ説明すると、
『for分とかif分とか使って複数行必要だった処理が一行ですむよ。しかも処理が早いよ。でもちょっと読みづらいよ』
っていう書き方でしょうか。

ものの本によると、”リスト内包表記とは、あるリストを元にして別のリストを作るための記法のことです”、だそうです。

ちょっとMayaのpyemlで普通の書き方と、それを内包表記で書いた例を作ってみました。
選択したオブジェクトのうち、Meshのみをリストで返すというものです。

import pymel.core as pm
sel = pm.selected()
MeshList = []
#--------------------------------------
for i in sel:
    if i.nodeType() == "mesh":
        MeshList.append(i)
#--------------------------------------
print MeshList

これをリスト内包表記を使って書くと

import pymel.core as pm
sel = pm.selected()
#リスト内包表記
#----------------------------------------------------
MeshList = [x for x in sel if x.nodeType() == "mesh"]
#----------------------------------------------------
print MeshList

コメントで囲まれた部分を内包表記で書き換えました。
for分とif分を組み合わせたような書き方のが、リスト内包表記です。
比べるとすっきりした印象があります。
ここではfor分とif分の二つを内包表記で書き換えましたが、可読性が許す限りいくつでも内包できます。

以上、最近覚えたpythonのリスト内包表記でした。

参考:
http://docs.python.jp/2/tutorial/datastructures.html

Related Posts Plugin for WordPress, Blogger...

関連記事

  1. 2015.02.23

    Nuke:Groupノード
  2. 2014.03.24

    toxik:[Retime]

コメントをお待ちしております

このサイトについて

3DCG Tipsサイト。Houdiniの記事が多めです。

RSS

follow us in feedly

Archives

  • 2024 (1)
  • 2023 (2)
  • 2022 (9)
  • 2021 (14)
  • 2020 (46)
  • 2019 (17)
  • 2018 (33)
  • 2017 (29)
  • 2016 (36)
  • 2015 (64)
  • 2014 (54)
  • 2013 (60)
  • 2012 (55)

スポンサードリンク

言語切り替え

  • English (United States)
  • 日本語

スポンサードリンク

言語切り替え

  • English (United States)
  • 日本語

Houdiniを使えるようになる本


Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /usr/home/nomorere/nomoreretake.net/wordpress/wp-content/themes/n1-custom/functions/social.php on line 132

Warning: file_get_contents(): Failed to enable crypto in /usr/home/nomorere/nomoreretake.net/wordpress/wp-content/themes/n1-custom/functions/social.php on line 132

Warning: file_get_contents(http://api.facebook.com/restserver.php?method=links.getStats&urls=http%3A%2F%2Fnomoreretake.net%2F2013%2F06%2F10%2Fby-python%2F): failed to open stream: operation failed in /usr/home/nomorere/nomoreretake.net/wordpress/wp-content/themes/n1-custom/functions/social.php on line 132

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /usr/home/nomorere/nomoreretake.net/wordpress/wp-content/themes/n1-custom/functions/social.php on line 132

Warning: file_get_contents(): Failed to enable crypto in /usr/home/nomorere/nomoreretake.net/wordpress/wp-content/themes/n1-custom/functions/social.php on line 132

Warning: file_get_contents(http://api.b.st-hatena.com/entry.count?url=http%3A%2F%2Fnomoreretake.net%2F2013%2F06%2F10%2Fby-python%2F): failed to open stream: operation failed in /usr/home/nomorere/nomoreretake.net/wordpress/wp-content/themes/n1-custom/functions/social.php on line 132