Python> Extract (unpack) the value of list> Add *> You taught me the difference between Python 2 and Python 3 regarding print (* mylist) / print ().

Operating environment


GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
sc_N = array.array('i')
sc_N.fromfile(rfp,1)
sc_sizes = array.array('i')
sc_sizes.fromfile(rfp, sc_N)

The above will result in an error.

TypeError: an integer is required

Because sc_N is a list, not a value (integer type).

It would be unpacking to take a value from a list of one element.

Reference http://stackoverflow.com/questions/3480184/unpack-a-list-in-python

I implemented it. (Addition: The following ideone is a Python 3 environment) http://ideone.com/1VUh3O

mylist = [ 3 ]
print (mylist)
 
print (*mylist)

Run


[3]
3

I got it.

Python2 and Python3

@Knoguchi and @shiracamus told me about the difference between Python2 and Python3 in the print () example.

Considering the future, I will consider implementing it in Python3.

Recommended Posts

Python> Extract (unpack) the value of list> Add *> You taught me the difference between Python 2 and Python 3 regarding print (* mylist) / print ().
Difference between list () and [] in Python
Difference between append and + = in Python list
[Python Iroha] Difference between List and Tuple
[Introduction to Python] What is the difference between a list and a tuple?
The answer of "1/2" is different between python2 and 3
About the difference between "==" and "is" in python
The timing when the value of the default argument is evaluated is different between Ruby and Python.
Difference between Ruby and Python in terms of variables
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
List concatenation method in python, difference between list.extend () and “+” operator
Extract the value of dict or list as a string
Extract the value closest to a value from a Python list element
Get and set the value of the dropdown menu using Python and Selenium
I investigated the behavior of the difference between hard links and symbolic links
[Python] Three methods to compare the list of one-dimensional array and the list of two-dimensional array and extract only the matching values [json]
Difference between Ruby and Python split
The story of Python and the story of NaN
Difference between == and is in python
Difference between python2 series and python3 series dict.keys ()
[Python] Difference between function and method
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
About the basics list of Python basics
[Python] Difference between sorted and sorted (Colaboratory)
[Python] Case where the execution time differs between the built-in list and deque
I measured the speed of list comprehension, for and while with python2.7.