简体   繁体   English

分割单个列表元素

[英]Split single list elements

I have a list of data that I am trying to manipulate. 我有一个要处理的数据列表。

initial_list = [333322222111111, 555000033311123, 666333312466332]

I want to put each element into a new list and then split them further so my new list would be: 我想将每个元素放到一个新列表中,然后将它们进一步拆分,这样我的新列表将是:

new_list = [[333,22222111111], [555, 000033311123], [666,333312466332]]

I have done the following: 我已经完成以下工作:

new_list = [[] for i in range(0, len(initial_list))]

This gives me: new_list = [[], [], []] 这给了我: new_list = [[], [], []]

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

This has given me 这给了我

[[333322222111111], [555000033311123], [666333312466332]]

I'm now stuck how to split each nested list... The .split method only works with strings.. The first 3 values within each list need to be cut off.. Ideally I'd even want to split the other part into further even chunks 我现在陷入了如何拆分每个嵌套列表的问题。.split方法仅适用于字符串..每个列表中的前3个值都需要切除..理想情况下,我什至希望将其他部分拆分为甚至更大的块

Any advice on what direction to go would be great. 任何关于该走的方向的建议都是很棒的。

Assume all your value are of the same length. 假设您所有的价值都一样。 The following code will output what you want. 以下代码将输出您想要的内容。

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

Some resources about the answer: List Comprehensions 有关答案的一些资源: 列表推导

Try like this, Just use the slicing of string, 尝试这样,只需使用字符串切片,

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

[[333, 322222111111], [555, 33311123], [666, 333312466332]]
initial_list = [333322222111111, 555000033311123, 666333312466332]
new_list =[]
for i in initial_list:
    i=str(i)
    new_list.append([int(i[0:3]),int(i[3:])])
print new_list

Output: 输出:

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

As per your doubt(comment section) of getting output in below new format: 根据您的疑问(评论部分)以以下新格式获取输出:

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

Here is the code: 这是代码:

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

Try like this 这样尝试

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

output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM