1

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?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Peter
  • 97
  • 1
  • 9

3 Answers3

1

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:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Yes, you understood it correctly. But this is just an example. Essentially I want to split it according to indices in track list which right now is [1,3] which is why it is first two or last two digits. But it won't always be like that. How can I do the split according to indices in track list? – Peter Jun 07 '19 at 13:45
  • @peter - instead of `inner[:2]` and `inner[2:]` for slicing you might want to go `inner[: track[0]+1]` and `inner[track[0]+1:]` (or `inner[track[1]-1:]`) referencing your splits - if you do not want to skip values, only one "track"-number is needed. Just make sure that you adjust for list slicing-end-value `[0:2]` (the `2`) _not_ being part of the slice. Indexing is 0 based in lists as well - so I do not quite get your 1,3. – Patrick Artner Jun 07 '19 at 13:47
  • So, 1 means that split should be made on index 1 (column wise) and so the list of lists will be sliced from 0:1 and 3 means that another split should be at index 3 (column wise) and therefore the list of lists will be spliced from 2:3 – Peter Jun 07 '19 at 13:59
0
  • 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)
garlicFrancium
  • 2,013
  • 14
  • 23
  • Thank you for you answer but it wouldn't work if I use: ex = [['1001001'],['0010001'],['1101001'],['0000001']] and track = [1,3,6] – Peter Jun 07 '19 at 14:11
  • No problem @peter . well, yes it wont work for more tracks which is if the array track increase like you said but it adding else if conditions should make it run but yes that makes it static! – garlicFrancium Jun 07 '19 at 14:35
  • Glad I could help ... even i worked on a generic code... I have updated it above.. see if that helps :) – garlicFrancium Jun 07 '19 at 15:00
0
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)

  • Thank you for your answer. But it wouldn't work if ex is for example, ex = [['1001001'], ['0010000'], ['1101111'], ['00001010']] and track = [1,3,6]. – Peter Jun 10 '19 at 10:59