Python> zip (* list4)> Matrix conversion> List and tuple argument expansion / dictionary argument expansion / local variable dictionary locals ()

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 http://qiita.com/7of9/items/d7bc7038a697adb214ee#comment-1a85846225c5085f851a

test_python_170323.py


list4 = [
	[ 1, 2, 3],
	[ 4, 5, 6],
	[ 7, 8, 9]
]

for cols in zip(*list4):
	print(cols)
$ python test_python_170323.py 
(1, 4, 7)
(2, 5, 8)
(3, 6, 9)

It seems that the rows and columns have been converted.

What are you doing?

http://www.madopro.net/entry/2016/12/21/134846

Of note is the zip (* pair) part. The zip argument is preceded by an asterisk. The asterisk in front of the function call argument is Official Document Also expands and interprets.

The above zip (* list4) will be the same as below.

for cols in zip((1,2,3),(4,5,6),(7,8,9)):
	print(cols)
>>> help(zip)
zip(...)
    zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
    
    Return a list of tuples, where each tuple contains the i-th element
    from each of the argument sequences.  The returned list is truncated
    in length to the length of the shortest argument sequence.

Looking at, in the expanded results (1,2,3), (4,5,6), (7,8,9), i-th element (eg (i == 0) At times, (set of 1,4,7) is a tuple.

The result is a function like matrix transformation.

Matters taught

In [Comment] by @shiracamus (http://qiita.com/7of9/items/e23bdd6e8d4d7997104a/#comment-3cd011185d0632437f24), I learned the following matters.

--Expansion of arguments for lists and tuples (*) --Dictionary argument expansion (**) --Local Variable Dictionary (locals ())

Recommended Posts

Python> zip (* list4)> Matrix conversion> List and tuple argument expansion / dictionary argument expansion / local variable dictionary locals ()
Expansion by argument of python dictionary
[Python Iroha] Difference between List and Tuple
Basic operation list of Python3 list, tuple, dictionary, set
Convenient argument passing technique in Python (tuple, dictionary type)
python variable expansion, format
Python3 List / dictionary memo
List method argument information for classes and modules in Python
Python: Create a dictionary from a list of keys and values