Python How to print list of list

后端 未结 3 2097
离开以前
离开以前 2021-01-23 07:14

I want to print list of list in python 3.x with below code, but it is giving an error.

lol=[[1,2],[3,4],[5,6],[\'five\',\'six\']]
for elem in lol:
      print (\         


        
3条回答
  •  渐次进展
    2021-01-23 07:38

    Use a list comprehension and str.join:

    Convert the integers to string(using str()) before joining them

    >>> lis = [[1,2],[3,4],[5,6],['five','six']]
    >>> print ("\n".join([ ":".join(map(str,x))   for x in lis]))
    1:2
    3:4
    5:6
    five:six
    

    or:

    >>> print ("\n".join([ ":".join([str(y) for y in x])   for x in lis]))
    1:2
    3:4
    5:6
    five:six
    

提交回复
热议问题