Python How to print list of list

后端 未结 3 2068
离开以前
离开以前 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:41

    One can't join int's only strings. You can Explicitly cast to str all your data try something like this

    for elem in lol:
        print (":".join(map(str, elem)))
    

    or with generator

    for elem in lol:
        print (":".join(str(i) for i in elem))
    

    or You can use format instead of casting to string (this allows You to use complex formatting)

    for elem in lol:
        print (":".join("'{}'".format(i) for i in elem))
    

提交回复
热议问题