简体   繁体   English

Python:在一条语句中打印列表中的所有项目

[英]Python: Print all items in list in one statement

My current code loops through the list and prints items i and i+1 next to each other. 我当前的代码遍历该list ,并彼此相邻打印项目ii+1 However, this results in code like: 但是,这导致如下代码:

for i in list:
    print(str(i) + ': ' + str(i+1))

This code works to print each item, but for project-related reasons they all need to be in one print statement, which would, if done manually, appear as: 这段代码可以打印每个项目,但是由于与项目相关的原因,它们都需要放在一个print语句中,如果手动完成,它将显示为:

print(str(i) + ': ' + str(i+1) + '\n' + str(i+2) + ': ' + str(i+3) + '\n'...)

Which is absurd, and also does not use iteration to loop through the list, meaning it would have to be added to for each item in the list. 这是荒谬的,并且不使用迭代来遍历列表,这意味着必须将其添加到列表中的每个项目。

Therefore, my question is, how could this be done with recursion in this manner, where items i and i+1 are printed, then items i+2 and i+3 on the next line in the same statement, and so on? 因此,我的问题是,如何以这种方式递归来做到这一点:先打印项目ii+1 ,然后再打印同一语句下一行的项目i+2i+3 ,依此类推?

To elaborate on 'project-related reasons': The project involves the Discord module, and it needs to export the information as one message sent in the channel. 要详细说明“与项目相关的原因”:该项目涉及Discord模块,它需要将信息导出为在通道中发送的一条消息。 This means I cannot iterate through each and print it, as that would send a new message for each item in the list. 这意味着我无法遍历每个项目并打印出来,因为那样会为列表中的每个项目发送一条新消息。

First of all, try to not use list as a variable name! 首先,尝试不要将list用作变量名! Secondly, you can write a generator function that runs through every second item in the list arr (starting either at 0 or 1) and join this to an empty string: 其次,您可以编写一个生成器函数,该函数贯穿列表arr (从0或1开始)中的第二个项目,并将其join到一个空字符串:

arr=[1,2,3,4,5,6,7,8]
print( ''.join("{0}: {1}\n".format(i,k)  for i,k in zip(arr[0::2], arr[1::2])) )

This yields "items i and i+1 […] then items i+2 and i+3 on the next line", as requested : 根据要求,将生成“项目i和i + 1 […],然后是下一行的项目i + 2和i + 3”:

1: 2
3: 4
5: 6
7: 8

Which is however different from your initial code, so perhaps you'd rather meant "items i and i+1 […], then items i+1 and i+2 on the next line : 但是,这与您的初始代码不同,因此,您可能宁愿意思是“项目i和i + 1 […],然后在下一行输入项目i + 1和i + 2:

print( ''.join("{0}: {1}\n".format(i,k)  for i,k in zip(arr[0:], arr[1:])) )
print(':'.join((str(i) for i in list)))

或这个

print(''.join(( str(i)+(':'if not i%2 else '\n') for n,i in enumerate(list) )))

Don't you want 你不想要

list_value = ['a','b','c','d','e']
print('\n'.join(('{}: {}'.format(list_value[i],list_value[i+1]) for i in range(len(list_value)-1))))

Which gives: 这使:

a: b
b: c
c: d
d: e

Rather than the @user1438644 's answer of 而不是@ user1438644的回答

print(':'.join((str(i) for i in list_value)))

which gives: 这使:

a:b:c:d:e

Something like this would store your output in one string 这样的事情会将您的输出存储在一个字符串中

output = ''

odd, even = mylist[::2], mylist[1::2]

for i in range(len(even)):
    output += '{}: {}\n'.format(odd[i], even[i])
print(output)

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

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