Printing a list of lists without parentheses

A somewhat similar question was asked here, but the answers did not help.

I have a list of lists, in particular something like ..

[[tables, 1, 2], [ladders, 2, 5], [chairs, 2]] 

It is designed for a simple indexer.

I want to output it like this:

 tables 1, 2 ladders 2, 5 chairs 2 

I can not get this conclusion.

However, I can get:

 tables 1 2 ladders 2 5 chairs 2 

But it is not quite close enough.

Is there an easy way to do what I ask? This does not mean that it is a difficult part of the program.

+7
source share
5 answers

This will do the following:

 for item in l: print item[0], ', '.join(map(str, item[1:])) 

where l is your list.

To enter, enter

 tables 1, 2 ladders 2, 5 chairs 2 
+9
source

If you do not mind that the output is on separate lines:

 foo = [["tables", 1, 2], ["ladders", 2, 5], ["chairs", 2]] for table in foo: print "%s %s" %(table[0],", ".join(map(str,table[1:]))) 

To get all this in one line, this is a little more complicated:

 import sys foo = [["tables", 1, 2], ["ladders", 2, 5], ["chairs", 2]] for table in foo: sys.stdout.write("%s %s " %(table[0],", ".join(map(str,table[1:])))) print 
+3
source

In Python 3.4.x

This will do the following:

 for item in l: print(str(item[0:])[1:-1]) 

where l is your list.

To enter, enter the following data:

 tables 1, 2 ladders 2, 5 chairs 2 

Another (cleaner) way would be something like this:

 for item in l: value_set = str(item[0:]) print (value_set[1:-1]) 

Sets the same result:

 tables 1, 2 ladders 2, 5 chairs 2 

Hope this helps anyone who might run into this problem.

+1
source

Try the following:

 L = [['tables', 1, 2], ['ladders', 2, 5], ['chairs', 2]] for el in L: print('{0} {1}'.format(el[0],', '.join(str(i) for i in el[1:]))) 

Output:

 tables 1, 2 ladders 2, 5 chairs 2 

{0} {1} is the output line in which

{0} is equal to el[0] , which is equal to 'tables' , 'ladders' , ...

{1} equals ', '.join(str(i) for i in el[1:])

and ', '.join(str(i) for i in el[1:]) combines each element in the list of the following: [1,2] , [2,5] , ... with ', ' as a separator.

str(i) for i in el[1:] used to convert each integer to a string before joining.

0
source

This uses a pure understanding of lists without map() , re.sub() or the for loop:

 print '\n'.join((item[0] + ' ' + ', '.join(str(i) for i in item[1:])) for item in l) 
0
source

All Articles