Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Python > tuple > 二重tupleを一重tupleに変換する

More than 3 years have passed since last update.
動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 とその-devel
mpich.x86_64 3.1-5.el6とその-devel
gcc version 4.4.7 (とgfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.1を使用。
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv

code

test_python_170323d.py
atuple = ((3, 1, 4), (1, 5, 9), (2, 6, 5))
print(atuple)

# 1 
res = [ flatten for inner in atuple for flatten in inner ]
res = tuple(res)
print(res)

# 2
res = ()
for rows in atuple:
    res = res + rows

print(res)
結果
$ python test_python_170323d.py 
((3, 1, 4), (1, 5, 9), (2, 6, 5))
(3, 1, 4, 1, 5, 9, 2, 6, 5)
(3, 1, 4, 1, 5, 9, 2, 6, 5)

方法1はコードを読んでからの理解時間(Readable codeでのTime-till-understanding)が長い。

当面、方法2の実装を使いそう。

7of9
セブンオブナインです。Unimatrix 01の第三付属物 9の7という識別番号です。Star trek Voyagerの好きなキャラクターです。まとめ記事は後日タイトルから内容がわからなくなるため、title検索で見つかるよう個々の記事にしてます。いわゆるBorg集合体の有名なセリフから「お前たち(の知識)を吸収する。抵抗は無意味だ」。Thanks in advance.
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away