[英]Print list elements on new line
I wish to print the elements of an array, without commas, and on separate lines. 我希望打印数组的元素,不带逗号,并在不同的行上。 I am writing a function for insertion sort.
我正在编写插入排序函数。 Though it works correctly, I am finding it tricky to print them properly.
虽然它工作正常,但我发现正确打印它们很棘手。 The code I've written is:
我写的代码是:
#!/bin/python
def insertionSort(ar):
newNo = ar[-1]
for i in range(0, m-1):
if(newNo < ar[m-i-1]):
ar[m-i] = ar[m-i-1]
for e in ar:
print e,
print '\n'
ar[m-i-1] = newNo
for f in ar: print f,
m = input()
ar = [int(i) for i in raw_input().strip().split()]
insertionSort(ar)
The output I get is: 我得到的输出是:
2 4 6 8 8
2 4 6 6 8
2 4 4 6 8
2 3 4 6 8
I should get the following output for the code to pass the test case: 我应该得到以下输出代码来传递测试用例:
2 4 6 8 8
2 4 6 6 8
2 4 4 6 8
2 3 4 6 8
ie without the extra space between lines. 即没有线之间的额外空间。 Click here for the detailed problem statement.
单击此处查看详细的问题陈述。
print
statement appends a new line automatically, so if you use print
语句会自动附加一个新行,因此如果您使用
print '\n'
You will get two new lines. 你会得到两条新线。 So you can use an empty string as in
print ''
or the better way would be to use print
by itself : 所以你可以像在
print ''
那样使用空字符串,或者更好的方法是单独使用print
:
print
This could be a fix - 这可能是一个修复 -
def insertionSort(ar):
newNo = ar[-1]
for i in range(0, m-1):
if(newNo < ar[m-i-1]):
ar[m-i] = ar[m-i-1]
for e in ar:
print e,
print
ar[m-i-1] = newNo
for f in ar: print f,
m = input()
ar = [int(i) for i in raw_input().strip().split()]
insertionSort(ar)
Edit - Ok ! 编辑 - 好的! ^ Someone already mentioned that.
^有人已经提到过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.