2015-09-07 77 views
0

可以說我有:如何從x範圍列表中刪除空格 - Python的

hello = list(xrange(100)) 
print hello 

結果是:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10... 

我需要從列表中刪除所有的空間,以便它可以像:

1,2,3,4,5,6,7,8,9,10... 
+0

'hello'將打印一個列表對象。爲什麼它是一個字符串對象? –

回答

0

由於join需要字符串對象,你需要明確地轉換這些項目爲字符串。例如:

hello = ','.join(list(xrange(100))) 
TypeError: sequence item 0: expected string, int found 

所以:

hello = xrange(100) 
print ''.join([str(n) for n in hello]) 

注意沒有必要爲list()

+1

不需要列表理解。將生成器表達式傳遞給'join' –