Operating environment
Xeon E5-2620 v4 (8 cores) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 and its-devel
mpich.x86_64 3.1-5.el6 and its-devel
gcc version 4.4.7 (And gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Use 1.
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
Python 3.6.0 on virtualenv
Related: Python> tuple> Convert double tuple to single tuple
--Method 1: Comprehension --Method 2: Use itertools
Reference: http://stackoverflow.com/questions/2961983/convert-multi-dimensional-list-to-a-1d-list-in-python Reference: http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python
test_python_170323e.py
import itertools
alist = [[3, 1, 4], [1, 5, 9], [2, 6, 5]]
print(alist)
# 1
res = [ flatten for inner in alist for flatten in inner ]
print(res)
# 2
res = itertools.chain(*alist)
res = list(res)
print(res)
result
$ python test_python_170323e.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]
I personally think that itertools is more readable now (2017/03/23).
Recommended Posts