繁体   English   中英

从列表输出中删除空格

[英]Remove white space from list output

我写的代码是将斐波那契数列生成到用户选择的点,例如'10'将生成:

[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

问题是空白处,我想知道是否有可能像这样打印它:

[1,1,2,3,5,8,13,21,34,55]

没有空格。

这是我正在使用的代码:

a=int(input("write the length of numbers you would like to see the fibonacci series for(by entering 0 or 1 the output will be [1,1]):  "))

if a<0:
     print("invalid entry please type a positive number")
else:        
    i=2
    fibs=[1,1]
    b=fibs[-2]
    c=fibs[-1]
    d=b+c
    while i<a :
        i=i+1
        b=fibs[-2]
        c=fibs[-1]
        d=b+c
        fibs.append(d)
print(fibs)

当您打印这样的容器时,其空格的使用已在其__repr__方法中确定。 您必须自己格式化输出:

print('[{}].format('",".join(map(str, fibs))))  # Instead of print(fibs).

这段代码:

print('[{}]'.format(','.join([str(x) for x in fibs])))

创建一个由转换为字符串的数字组成的新列表,将其用逗号连接并在大括号之间打印。

请注意, 这不是执行所需操作的最快,最简单的方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM