python - 如何在一行中打印列表元素?
问题描述
我需要打印排序后的整数列表,但它应该在一行中,没有列表方括号,最后没有任何 '\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=', ')
推荐阅读
- r - 负相关泰勒图(左象限)
- jmeter - 在 Jmeter 中,标头管理器不添加用于测试 Spring Boot Rest API 的 jwt 授权令牌
- reactjs - 使用 React 和 redux 构建浏览器扩展
- javascript - 在 if 语句中选择
- r - 如何按由多行组成的序列复制数据?
- android - 测试房间数据库,可迭代包含“项目”但!项目 0 是“项目”
- javascript - 你什么时候在反应中使用类组件,因为 useState 现在在功能组件中可用
- rspec - 配置 rspec 以排除文件夹,除非它被明确指定给 rspec 运行
- react-native - React Native 嵌套平面列表(垂直平面列表中的水平平面列表)
- ms-word - 如何在 Word 中强调文本,而不改变文档,就像在 Grammarly 中一样