在没有空格的python中打印列表 [英] Printing lists in python without spaces

查看:64
本文介绍了在没有空格的python中打印列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个程序,将基数为 10 的数字更改为基数 7,所以我这样做了:

num = int(raw_input(""))模 = 整数(0)列表 = []而数量>0:mod = num%7数量 = 数量/7list.append(mod)list.reverse()对于范围内的 i (0,len(list)):打印列表[i],

但是如果数字是 210,它会打印 4 2 0 我如何摆脱空格

解决方案

您可以将 join 与 list comprehension 一起使用:

<预><代码>>>>l=范围(5)>>>打印 l[0, 1, 2, 3, 4]>>>''.join(str(i) for i in l)'01234'

另外,不要使用 list 作为变量名,因为它是一个内置函数.

I am doing a program that changes a number in base 10 to base 7, so i did this :

num = int(raw_input(""))
mod = int(0)
list = []
while num> 0:
    mod = num%7
    num = num/7
    list.append(mod)
list.reverse()
for i in range (0,len(list)):
    print list[i],

But if the number is 210 it prints 4 2 0 how do i get rid of the spaces

解决方案

You can use join with list comprehension:

>>> l=range(5)
>>> print l
[0, 1, 2, 3, 4]
>>> ''.join(str(i) for i in l)
'01234'

Also, don't use list as a variable name since it is a built-in function.

这篇关于在没有空格的python中打印列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆