Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 拆分单个列表元素_Python_Python 2.7 - Fatal编程技术网

Python 拆分单个列表元素

Python 拆分单个列表元素,python,python-2.7,Python,Python 2.7,我有一个我试图操纵的数据列表 initial_list = [333322222111111, 555000033311123, 666333312466332] 我想将每个元素放入一个新列表中,然后进一步拆分它们,这样我的新列表将是: new_list = [[333,22222111111], [555, 000033311123], [666,333312466332]] 我已经做了以下工作: new_list = [[] for i in range(0, len(initial_l

我有一个我试图操纵的数据列表

initial_list = [333322222111111, 555000033311123, 666333312466332]
我想将每个元素放入一个新列表中,然后进一步拆分它们,这样我的新列表将是:

new_list = [[333,22222111111], [555, 000033311123], [666,333312466332]]
我已经做了以下工作:

new_list = [[] for i in range(0, len(initial_list))]
这给了我:
new_list=[]、[]、[]、[]]

for i in range(0, len(new_list)):
    new_list[i].append(initial_list[i])
这给了我

[[333322222111111], [555000033311123], [666333312466332]]
我现在被困在如何分割每个嵌套列表。。。.split方法仅适用于字符串。。每个列表中的前3个值需要被截断。。理想情况下,我甚至希望将另一部分进一步分成更均匀的块


任何关于前进方向的建议都很好。

假设你所有的价值都是相同的长度。下面的代码将输出您想要的内容

>>> a = [333322222111111, 555000033311123, 666333312466332]
>>> [ [i/10**12, i%10**12] for i in a]
[[333, 322222111111], [555, 33311123], [666, 333312466332]]

关于答案的一些参考资料:

假设所有值的长度相同。下面的代码将输出您想要的内容

>>> a = [333322222111111, 555000033311123, 666333312466332]
>>> [ [i/10**12, i%10**12] for i in a]
[[333, 322222111111], [555, 33311123], [666, 333312466332]]

关于答案的一些参考资料:

像这样尝试,只需使用字符串切片

In [18]: print [map(int,[i[:3],i[3:]]) for i in map(str,initial_list)]

[[333, 322222111111], [555, 33311123], [666, 333312466332]]

试着这样做,只需使用线的切片

In [18]: print [map(int,[i[:3],i[3:]]) for i in map(str,initial_list)]

[[333, 322222111111], [555, 33311123], [666, 333312466332]]
输出:

[[333, 322222111111], [555, 33311123], [666, 333312466332]]
根据您对以下新格式输出的疑问(评论部分):

output: [[333, 3222, 2211, 1111], [555, 0, 3331, 1123], [666, 3333, 1246, 6332]]
代码如下:

initial_list = [333322222111111, 555000033311123, 666333312466332]
new_list =[]
for i in initial_list:
    i=str(i)
    temp_list=[]
    temp_list.append(int(i[0:3]))
    jump=4
    for j in range(3,len(i),jump):
        temp_list.append(int(i[j:j+jump]))
    new_list.append(temp_list)
print new_list
输出:

[[333, 322222111111], [555, 33311123], [666, 333312466332]]
根据您对以下新格式输出的疑问(评论部分):

output: [[333, 3222, 2211, 1111], [555, 0, 3331, 1123], [666, 3333, 1246, 6332]]
代码如下:

initial_list = [333322222111111, 555000033311123, 666333312466332]
new_list =[]
for i in initial_list:
    i=str(i)
    temp_list=[]
    temp_list.append(int(i[0:3]))
    jump=4
    for j in range(3,len(i),jump):
        temp_list.append(int(i[j:j+jump]))
    new_list.append(temp_list)
print new_list
像这样试试

 a = [333322222111111, 555000033311123, 666333312466332]
 mylst = [divmod(i,10**12) for i in a]
 print mylst 
输出:

[[333, 322222111111],  [555, 33311123],[666, 333312466332]]
像这样试试

 a = [333322222111111, 555000033311123, 666333312466332]
 mylst = [divmod(i,10**12) for i in a]
 print mylst 
输出:

[[333, 322222111111],  [555, 33311123],[666, 333312466332]]

我喜欢这种方法。。。。如果我想在每个列表中拆分索引1,然后再拆分为每4个,我该怎么做呢?i、 e[333322221111]。因此,如果给定的输入为,则初始_列表=[333322221111111555000033311123666333312466332]。你期望什么输出?我希望输出列表=[[3333222211111],[55500033311123],[66633312466332]。。。。每个列表的第一个索引是3个字符,其余的都分为4组..@arsenal88,编辑我的帖子。请查收<代码>输出:[[333322221111],[555331123],[66633312466332]。我喜欢这种方法。。。。如果我想在每个列表中拆分索引1,然后再拆分为每4个,我该怎么做呢?i、 e[333322221111]。因此,如果给定的输入为,则初始_列表=[333322221111111555000033311123666333312466332]。你期望什么输出?我希望输出列表=[[3333222211111],[55500033311123],[66633312466332]。。。。每个列表的第一个索引是3个字符,其余的都分为4组..@arsenal88,编辑我的帖子。请查收<代码>输出:[[333322221111],[555,033311123],[66633312466332]