简体   繁体   中英

Printing items from a list in Python

I am trying to write a function in Python where I am given a list, l = [[2,3],[5,6],[4,8]] , which contains other lists. I am supposed to print the first item in every list on one line with three spaces in between and the second item in every list in another line with also three spaces in between. I have made the following code - however it seems to print everything on one line, and I am not sure what I am doing wrong.

l = [[2,3],[5,6],[4,8]]
for item in l:
    print(item[0], end = '  ')
for obj in l:
    print(obj[1], end = '  ') 

This will do:

for column in range(2):
    print("   ".join([str(row[column]) for row in l]))
>>> l = [[2,3],[5,6],[4,8]]
>>> for column in range(2):
...     print("   ".join([str(row[column]) for row in l]))
... 
2   5   4
3   6   8

U already made it just add a line break outside both loops and your good to go:

l = [[2,3],[5,6],[4,8]]
for item in l:
    print(item[0], end = '  ')

print('\n') #take note this needs to be outside both loops

for obj in l:
    print(obj[1], end = '  ')

However if you just want it to be on the next line immediately,

l = [[2,3],[5,6],[4,8]]
    for item in l:
        print(item[0], end = '  ')

print() #take note this needs to be outside both loops

for obj in l:
   print(obj[1], end = '  ')

Since it is an integer, to concatenate with strings ,use map !

>>> "   ".join(str(item[0]) for item in l)
'2   5   4'
>>> "   ".join(str(item[1]) for item in l)
'3   6   8'

Or use zip !

zip - to put it in a simple way, it groups all elements of same index in sublist to a single list.

This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there are multiple arguments which are all of the same length, zip() is similar to map() with an initial argument of None. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.

The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n).

>>> l
[[2, 3], [5, 6], [4, 8]]
>>> zip(*l)
[(2, 5, 4), (3, 6, 8)]
>>> for each in zip(*l):
...     "    ".join(map(str,each))
... 
'2    5    4'
'3    6    8'

You can try this:

l = [[2,3],[5,6],[4,8]]
new_l = ["{}   {}   {}".format(*[b[i] for b in l]) for i in range(len(l[0]))]
for i in new_l:
   print(i)

Output:

2   5   4
3   6   8

You need to add a print() in between your loops since you modified the default "\\n" as the end character for the print.

Another way to achieve this would be:

>>> print(*a[0], sep = "   ")
>>> print(*b[1], sep = "   ")

Just put a print statement in between!

l=[[2,3],[4,5],[6,7]]
for item in l:
    print(item[0], end = '  ')
print()
for obj in l:
    print(obj[1], end = '  ') 
>>> ''.join([str(l[i][0])+str("   ") if i!=len(l)-1 else str(l[i][0]) for i in range(len(l))])
'2   5   4'
 >>> ''.join([str(l[i][1])+str("   ") if i!=len(l)-1 else str(l[i][1]) for i in range(len(l))])
'3   6   8'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM