Python按空格分割列表

时间:2015-03-26 12:12:36

标签: python

我有一个带有数字的文本文件,方式如下“12345679010111213” 我已经构建了一个脚本来读取fille,使用名为“numbersoflist”的变量将值附加到列表中list1.append(numbersoflist)

但是当我调用list1.split('')时,它仍会打印出文本文件中出现的值,没有空格。我的目标是让它们看起来像“1 2 3 4 5 6 ......”

1 个答案:

答案 0 :(得分:3)

>>> s = '12345679010111213'
>>> list(s)
['1', '2', '3', '4', '5', '6', '7', '9', '0', '1', '0', '1', '1', '1', '2', '1', '3']
>>> ' '.join(list(s))
'1 2 3 4 5 6 7 9 0 1 0 1 1 1 2 1 3'
>>> ' '.join(s) # works since str is also an iterable
'1 2 3 4 5 6 7 9 0 1 0 1 1 1 2 1 3'