Printing an int list in a single line python3

前端 未结 10 1394
臣服心动
臣服心动 2020-12-03 01:16

I\'m new to python and I\'m trying to scan multiple numbers separated by spaces (let\'s assume \'1 2 3\' as an example) in a single line and add it to a list of int. I did i

10条回答
  •  悲哀的现实
    2020-12-03 01:33

    these will both work in Python 2.7 and Python 3.x:

    >>> l = [1, 2, 3]
    >>> print(' '.join(str(x) for x in l))
    1 2 3
    >>> print(' '.join(map(str, l)))
    1 2 3
    

    btw, array is a reserved word in Python.

提交回复
热议问题