Sunday 15 February 2015

Python How to print list of list -


I want to print the list of lists in Python 3.x with the code below but it is giving an error.

  lol = [[1,2], [3,4], [5,6], ['five', ' Six]]] (Elem)) # This is the error I'm doing- & gt; TypeError: Sequence Item 0: Expected string example, int   

I expect this output:

  1: 2 3: 4 5: 6 five:   

I can get the same output using PLL code (this is just for reference):

 for  (my $ i = 0; $ i & lt; scalar (@ {$ lol}); $ i ++) {print included (":", @ {$ lol-> [$ i]}). "\ N"; }   

How do I do Python 3.x?

can not be included in any string of int From str you can try all your data

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

or with generator

  for elem in lol: print (":". (I) join for i in elem) < / Code>  

or you can use format instead of casting for a string (it's a complex formatting For use in lol:

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

No comments:

Post a Comment