1

I have for example two lists:

List1 = [1, "/", 2, "/", 3]    
List2 = [11, "/", 12, 13, 14, "/", 15, 16, 17, 18]

As result I need follow combinations (delimeter is "/"):

1 11

2 12

2 13

2 14

3 15

3 16

3 17

3 18

How can I do it in python?

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
zoran
  • 13
  • 3

5 Answers5

1

Step 1 would be to figure out a way of turning [1, "/", 2, "/", 3] and [11, "/", 12, 13, 14, "/", 15, 16, 17] into [[1], [2], [3]] and [[11], [12, 13, 14], [15, 16, 17]].

Step 2 would be how to figure out a way of turning [100, 101] and [200, 201, 202] into [(100, 200), (100, 201), (100, 202), (101, 200), (101, 201), (101, 202)]

Once you have that, the printing should be trivial.

Vatine
  • 20,782
  • 4
  • 54
  • 70
1

First split the lists, then zip them together by groups and then make a pair of each matching group.

import itertools

list1_sub = [list(y) for x, y in itertools.groupby(List1, lambda z: z == "/") if not x]
list2_sub = [list(y) for x, y in itertools.groupby(List2, lambda z: z == "/") if not x]
zip_lists = zip(list1_sub, list2_sub)
data = itertools.chain(*[itertools.product(x[0], x[1]) for x in zip_lists])
Tom Ron
  • 5,906
  • 3
  • 22
  • 38
0

Not sure how useful a code dump is going to be to help you understand how to do this but I was interested in trying it:

>>> a = [1, '/', 2, '/', 3]
>>> b = [11, '/', 12, 13, 14, '/', 15, 16, 17, 18]
>>> def split(a, delim='/'):
...     l = [[]]
...     for i in a:
...             if i == '/':
...                     l.append([]) #new list
...             else:
...                     l[-1].append(i) #append to latest list
...     return l #list of split lists
...
>>> import itertools
>>> [i for x in zip(split(a), split(b)) for i in itertools.product(*x)]
[(1, 11), (2, 12), (2, 13), (2, 14), (3, 15), (3, 16), (3, 17), (3, 18)]
Holloway
  • 6,412
  • 1
  • 26
  • 33
0

Copying mostly from the accepted answer in this question you could try using a generator and zip like this:

def group(seq, sep):
    g = []
    for element in seq:
        if element == sep:
            yield g
            g = []
            continue
        g.append(element)
    yield g

List1 = [1, "/", 2, "/", 3]    
List2 = [11, "/", 12, 13, 14, "/", 15, 16, 17, 18]

List1_grouped = list(group(List1, "/"))
List2_grouped = list(group(List2, "/"))
# List1_grouped = [[1], [2], [3]]
# List2_grouped = [[11], [12, 13, 14], [15, 16, 17, 18]]

for index, items in zip(List1_grouped, List2_grouped):
    for item in items:
        print(index[0], item)
# 1 11
# 2 12
# 2 13
# 2 14
# 3 15
# 3 16
# 3 17
# 3 18
Community
  • 1
  • 1
Cottonbud
  • 106
  • 7
0

You can do it as well this way:

>>>List1 = [1, "/", 2, "/", 3]    
>>>List2 = [11, "/", 12, 13, 14, "/", 15, 16, 17, 18]
>>> indx=0
>>> for x in List1:
        if x == '/':
            continue
        for j,y in enumerate(List2[indx:]):
             if y == '/':
                 indx = indx+j+1
                 break
             print x,y
Iron Fist
  • 10,739
  • 2
  • 18
  • 34