python - 插入取自 for 循环的值

标签 python arrays for-loop python-3.x

在这段代码中,我分别打印了这 n 个值。但我希望在每次循环后将这些值打印在一个数组中。我该怎么做?

n=1000000
for x in range(1,3000001):
    if n%2==0:
        x=n/2
    else:
        x=3*n+ 1
    n=x
    print(n)
    if n==1:
        break

谢谢。

最佳答案

print() 中使用 end=' ':

print(n, end=' ')

演示:

for i, x in enumerate(range(10), 1):
    print (x, end=' ')
    if x==5:
        break
...         
0 1 2 3 4 5
>>> print(i)  #print the count
6

关于python - 插入取自 for 循环的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18298774/

相关文章:

python - 为什么 Apache 服务器在 python 中使用 Snowflake Ingest 模块抛出语法错误

arrays - 如何从 Spark Scala 中的多个数组创建 DataFrame?

linux - 使用命令的输出来喂另一个

c++ - 寻找对的更有效方法

javascript - 如何编辑对象并将新对象传递给数组? ReactJS, Material UI

for-loop - LaTeX 中的 for 循环和表格

Python:当矩阵高度 > 256 时,使用 numpy 将迭代赋值转换为原子赋值不起作用

python - inplace=True 不适用于子集数据

python - 将字节列表转换为字节字符串

python - 过滤字符串列表以不包含来自另一个列表的任何字符串作为子字符串