循环内Python 2.6 / 2.x或Python 3.x中的BASH“ echo -n值”-单行不带空格

AKS

像在BASH中一样,我可以在循环中使用“ echo -n”,并在一行中打印数组(或Python中的列表)中的所有元素,而无需使用任何空格字符。有关循环,请参见下文的“ echo -n值”。

Arun Sangal@pc MINGW64 ~
$ a=(1 2 33 four)

Arun Sangal@pc MINGW64 ~
$ for v in ${a[@]}; do echo $v; done
1
2
33
four

Arun Sangal@pc MINGW64 ~
$ for v in ${a[@]}; do echo -n $v; done
1233four
Arun Sangal@pc MINGW64 ~
$ for v in ${a[@]}; do echo -n "$v "; done
1 2 33 four

在迭代每个列表值的同时,如何使用for循环在Python中执行相同的操作

PS:我不想使用“” .join(list)。另外,似乎“” .join(list)仅在我的Python列表中的值为“字符串”类型时才有效,然后“” .join(list)将起作用(如BASH echo -n),但如果列表中为Integer aka非字符串值(例如:如下面代码中的列表编号所示),““。join(numbers)或”“ .join(str(numbers))仍然不起作用(如BASH echo -n); 在这种情况下,即使我使用了print num,在循环中,它仍然引入了空格字符“”。

我正在尝试使用打印值(通过命令),但是引入了空格“”字符。

在Python 2.x中,显示以下代码:

numbers  = [7, 9, 12, 54, 99] #"".join(..) will NOT work or print val, will not work here
strings  = ["1", "2", "33", "four"] #"".join(..) will work as values are strings type.

print "This list contains: "

#This will print each value in list numbers but in individual new line per iteration of the loop.
for num in numbers:
    print num

print ""
print "".join(str(numbers))
print "".join(strings)

#PS: This won't work if the values are "non-string" format as I'm still getting a space character (see output below) for each print statement for printing num value
for num in numbers:
    print num,

输出为:

This list contains: 
7
9
12
54
99

[7, 9, 12, 54, 99]
1233four
7 9 12 54 99

我要寻找的是,当列表具有非字符串值时,如何在同一行中使用for循环和打印语句来打印列表编号的所有元素(如下所示)?

79125499
网络妖怪

如果我理解的正确,您是否正在尝试打印数字而在它们之间没有空格?如果是这样,请尝试以下操作。

from __future__ import print_function

for i in range(1, 11):
    print (i, end='')

结果

12345678910
Process finished with exit code 0

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么对于整数,2 * x * x比Python 3.x中的2 *(x * x)快?

Javascript中的Python x [1:2:3]?

Python 3.x 和 Python 2.x 在 while 循环中的区别

为什么n = [1,2,3,4,5,6,7,8],n [:6:-2]在Python中是[8]?

Python 2.x和3.x速度

Python readlines() 3.X 到 2.X

Monkeyrunner 使用的是 python 2.x 而不是 3.x

将 Python 2x 移植到 3x

在IPython Notebook中同时使用Python 2.x和Python 3.x

当 python 2x 是你的默认 python 版本但你在 Bash 上使用 python 3x 时,你如何在 pip 上安装 Django 2x

按行将2x6数组重塑为3x2x2

为什么Python 2.x中的math.factorial比3.x慢得多?

为什么Python 2.x中的math.factorial比3.x慢得多?

如何在Python中绘制从x = 0到x = 3的e ^(-t ^ 2)积分?

Python 2.x和3.x中的有效语法是否引发异常?

如何在Python 3.x中获得类似2.x的排序行为?

在Windows中访问Python2.x和3.x

处理Python 2.x和3.x中的缓冲区

如何在python2.x /3.x中缩进函数调用的时间

sum =(1 ** 2)+(2 ** 2)-(3 ** 2)+(4 ** 2)-,...,+(n ** 2)Python中的程序代码

Python-2.x:不带os.listdir()的列表目录

从 2 到 N 的给定数字应该使每个数字可整除输出为 [(2,4), (2,6),( 2,8),( 3,9),( 3,12), (3,15) .....] 在 Python 2/3 中

循环(1 + x + x ** 2 + x ** 3 + x ** 4 .... n)不起作用

Python子图2x2矩阵中的3个图(金字塔)

如何使用Java中的嵌套循环打印2x2和3x3矩阵中返回字符串值的数学乘法表

将 2x2 数组的 3x3 数组转换为 6x6 数组保持位置

PHP中的Echo 2 SQL ROW值

如何在x轴值出现多次的python中进行绘制,例如0 1 2 3 2 1 0

如何为Python 3.x安装psycopg2?