Print list without brackets in a single row

前端 未结 12 1459
北恋
北恋 2020-11-22 17:01

I have a list in Python e.g.

names = [\"Sam\", \"Peter\", \"James\", \"Julian\", \"Ann\"]

I want to print the array in a single line withou

12条回答
  •  面向向阳花
    2020-11-22 17:50

    If the input array is Integer type then you need to first convert array into string type array and then use join method for joining with , or space whatever you want. e.g:

    >>> arr = [1, 2, 4, 3]
    >>> print(", " . join(arr))
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: sequence item 0: expected string, int found
    >>> sarr = [str(a) for a in arr]
    >>> print(", " . join(sarr))
    1, 2, 4, 3
    >>>
    

    Direct using of join which will join the integer and string will throw error as show above.

提交回复
热议问题