I
I
igorekishev2015-04-06 02:27:58
Python
igorekishev, 2015-04-06 02:27:58

Formation of lists nested in a list from other lists according to certain rules?

Good afternoon,
there are three lists

allIndex_A = [1, 2, 8, 12, 17, 21, 23, 29]
allIndex_B = [3, 5, 7, 9, 11, 13, 15, 18, 20, 22, 24, 26, 28, 30]
allIndex_C = [4, 6, 10, 14, 16, 19, 25, 27]

you need to make a list of them with nested lists, like this:
newList = [[1,3,4], [2,3,4], [8,9,10], [12,13,14], ...]

The first element of the nested list is the first element of the list allIndex_A.
The second element is the first element in the allIndex_B list whose value is greater than the value of the first element in the nested list.
The third element is also the first element found in the allIndex_C list that is greater than the first element in the nested list.
The first element of the second nested list is the second element from the list allIndex_A.
The second and third elements of the second nested list are found under the same conditions as in the first nested list. That is, it must be the first found element from the lists allIndex_B and allIndex_C, the value of which is greater than the value of the first element.
An example of the resulting newList above.
I started learning programming recently, I don't know many things yet. I would be grateful if someone explains how to do what I need, or at least gives a link where you can read on the topic of the question.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
B
bobrovskyserg, 2015-04-06
@igorekishev

allIndex_A = [1, 2, 8, 12, 17, 21, 23, 29]
allIndex_B = [3, 5, 7, 9, 11, 13, 15, 18, 20, 22, 24, 26, 28, 30]
allIndex_C = [4, 6, 10, 14, 16, 19, 25, 27]

ita, itb, itc = iter(allIndex_A), iter(allIndex_B), iter(allIndex_C)
b = c = allIndex_A[0]
newList = []
try:
    while True:
        a = next(ita)
        while b <= a:
            b = next(itb)
        while c <= a:
            c = next(itc)
        newList.append([a, b, c])
except StopIteration:
    print(newList)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question