gpt4 book ai didi

python - 如何在一行中打印列表元素?

转载 作者:行者123 更新时间:2023-12-03 18:09:37 25 4
gpt4 key购买 nike

我需要打印整数的排序列表,但它应该在一行中,没有列表方括号,最后没有任何 '\n'...

import random
n = int(input(""))
l=[]
for i in range(n):
x = int(input())
l.append(x)
not_sorted = True
while not_sorted:
x = random.randint(0,n-1)
y = random.randint(0,n-1)
while x==y:
y = random.randint(0,n-1)
if x>y:
if l[x]<l[y]:
(l[x],l[y])=(l[y],l[x])
if x<y:
if l[x]>l[y]:
(l[x],l[y])=(l[y],l[x])
for i in range(0,n-1):
if l[i]>l[i+1]:
break
else:
not_sorted = False
for i in range(n):
print(l[i])

输出应该是这样的::: 1 2 3 4 5
而不是这样:::: [1,2,3,4,5]

最佳答案

您可以将列表解压到 print使用 *这将自动被一个空格分割

print(*l)

如果需要逗号,请使用 sep=争论
print(*l, sep=', ')

关于python - 如何在一行中打印列表元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52097264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com