Reputation: 1160
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 = [[], [], []]
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
Any advice on what direction to go would be great.
Upvotes: 0
Views: 117
Reputation: 1030
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
Upvotes: 0
Reputation: 1941
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]]
Upvotes: 0
Reputation: 16081
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]]
Upvotes: 0
Reputation: 5533
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
Upvotes: 3