2013-07-11 38 views
0

我创建打印出列表中的程序:单独的一个部分名单分为四名部分名单与Python

['2 19 2839475239874 hda'] 

它应该是这样的:

['2','19','2839475239874','hda'] 

因此而不必一个包含四个部分的列表,我有一大部分。我可以做些什么来分开我的名单,以便它有四个部分?

谢谢!我一直在这里工作了一段时间,而且我还没有找到任何实际可行的答案。

+1

你有没有考虑过,也许这个问题是不是“我的名单应该看像这样并且不“,也许它是,”为什么不是在我生成它时四个部分的列表?“ – RoadieRich

+0

可能有办法按照您的预期创建列表,而不是将其拆分。你能展示你是如何创建列表的? – dansalmo

回答

2

使用str.split分裂在空格字符串:

>>> lis = ['2 19 2839475239874 hda'] 
>>> lis[0].split() 
['2', '19', '2839475239874', 'hda'] 

帮助上str.split

>>> print (str.split.__doc__) 
S.split(sep=None, maxsplit=-1) -> list of strings 

Return a list of the words in S, using sep as the 
delimiter string. If maxsplit is given, at most maxsplit 
splits are done. If sep is not specified or is None, any 
whitespace string is a separator and empty strings are 
removed from the result.