Peter
Peter

Reputation: 117

Splitting a list of lists into smaller lists

I have a list of lists:

ex = [['1001'],['0010'],['1101'],['0000']]

I want to split this list of lists into smaller lists. And I have another list which consists of indices where I want to make a split:

track = [1,3]

So I want to split this list of lists to give me following result:

sublist = [
    [[1,0],[0,0],[1,1],[0,0]],
    [[0,1],[1,0],[0,1],[0,0]]
    ]

I've tried it on just a simple list:

ex = [1,0,0,1]
start = 0
position = []
for i in track:
    position.append(ex[start:i+1])
    start = i+1

But in this case my list is already has integer whereas the original list has strings.

How can I achieve this on a list of list which has strings instead of integer? I don't quite know from where to begin?

Upvotes: 1

Views: 372

Answers (3)

Sky丶Memory
Sky丶Memory

Reputation: 41

import itertools
import sys

ex = [['1001'], ['0010'], ['1101'], ['0000']]
track = [1, 3]

# [a,b)
it = itertools.chain(map(lambda x: x - 1, track), [sys.maxsize])
last = next(it, None)
result = []
for curr in it:
    temp = []
    for s in itertools.chain.from_iterable(ex):
        temp.append(list(map(int, s[last:curr])))
    result.append(temp)
    last = curr

print(result)

Upvotes: 0

garlicFrancium
garlicFrancium

Reputation: 2259

  • sublist is the final list. subsublist is treated as an intermediate temporary list.
  • track is the outer loop
  • ex is the inner loop

ex = [['1001'],['0010'],['1101'],['0000']]
track = [1,3]
subsublist = []
sublist = []
start=0

for index in track:
    # print(index)
    if start == 0:
        end=index+1
    else:
        end=None
    for item in ex:
        subsublist.append([item[0][start:end]])
    sublist.append(subsublist)
    start=end
    subsublist = []

print(sublist)

[UPDATE]Another attempt to make the code a bit generic!

ex = [['100190'],['001099'],['110187'],['000050']]
tracks = [1,3,6]
subsublist = []
sublist = []
start=0

for track in tracks:
    indexOfTrack = tracks.index(track)
    if indexOfTrack == 0:
        end=track+1
    elif indexOfTrack > 0 and indexOfTrack < len(tracks)-1:
        end = track+1
    else:
        end=None
    for item in ex:
        subsublist.append([item[0][start:end]])
    sublist.append(subsublist)
    start=end
    subsublist = []

print(sublist)

Upvotes: 0

Patrick Artner
Patrick Artner

Reputation: 51643

You seem to want to split the numbers into digit and put the first two and last two digits of each number into a seperate list in the result - here you go:

# what you got
ex = [['1001'],['0010'],['1101'],['0000']]

# what you want
sublist = [[[1,0],[0,0],[1,1],[0,0]],
           [[0,1],[1,0],[0,1],[0,0]]]

# how to get there: create single integers from each string
# list comprehension, see below for answers about them
digits = [ list(map(int,l)) for inner in ex for l in inner] 
print(digits )

# create the results 
result = [ [],[] ]

for inner in digits:
    result[ 0].append( inner[:2] )   # list slicing, see below for answers about it
    result[-1].append( inner[2:] )

print(result)

Output (reformatted):

# split into digits
[[1, 0, 0, 1], [0, 0, 1, 0], [1, 1, 0, 1], [0, 0, 0, 0]]

# put into results
[[[1, 0], [0, 0], [1, 1], [0, 0]],
 [[0, 1], [1, 0], [0, 1], [0, 0]]]

Built in functions helps you with explanations for map() and other helpfull functions. Also interesting to read:

Upvotes: 1

Related Questions