简体   繁体   English

从Python列表中打印项目

[英]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. 我正在尝试用Python编写一个函数,在其中给了一个列表l = [[2,3],[5,6],[4,8]] ,其中包含其他列表。 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 ! 由于它是整数,因此要与字符串连接,请使用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

zip - to put it in a simple way, it groups all elements of same index in sublist to a single list. zip-简单地说,它将子列表中相同索引的所有元素分组到一个列表中。

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. 此函数返回一个元组列表,其中第i个元组包含每个参数序列或可迭代对象中的第i个元素。 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. 当有多个长度相同的参数时,zip()类似于map(),其初始参数为None。 With a single sequence argument, it returns a list of 1-tuples. 使用单个序列参数,它将返回一个1元组的列表。 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). 这使得使用zip(* [iter(s)] * n)将数据系列聚类为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. 由于修改了默认的“ \\ n”作为打印的结束字符,因此需要在循环之间添加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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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