user12601274
user12601274

Reputation:

Python | How to group value to different lists from a main list given a certain range of number for each index

OGlist = [A, B, C, D, E, 1, 2, 3, 4, 5, F, G, H, I, J, 6, 7, 8, 9, 10]
_list = []
_list2 = []

So, I have a list OGlist... I want the first 5 elements of the OGlist into _list , the second 5 elements of OGlist into _list2, the third 5 elements into _list, the fourth 5 elements into _list2 and so on

How do I achieve this?

I have tried this:

for x in range(1, len(OGlist) + 1):
    _list.append(OGlist[x-1])
    if x%5 == 0:
        y = x
        while True:
            _list2.append(OGlist[x])
            x += 1
            if x == y + 5:
                break

What condition and logic should I use to get the desired output?

Desired output:

_list = [A, B, C, D, E, F, G, H, I, J]
_list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Upvotes: 4

Views: 136

Answers (5)

user158881
user158881

Reputation: 133

here is my code

import numpy as np

size_array = 500 # example
big_list = [x for x in range(size_array)]
half_list1 = []
half_list2 = []


idx = [x for x in range(0,int(size_array/5), 2)]
idx = np.repeat(idx, 5)*5 + np.tile([1, 2, 3, 4, 5], int(size_array/10))

half_list1 = [big_list[i] for i in idx]
half_list2 = [big_list[i+5] for i in idx]

Upvotes: 1

pregenRobot
pregenRobot

Reputation: 307

If you are looking for one liners, this should do it.

OGlist = [x, y, foo, bar, etc]

list_of_lists = [OGlist[i:i+5] for i in range(len(OGlist)-5) if i%5==0]

##GETTING LISTS AT EVEN INDEXES
_list = [l_ for i,l_ in enumerate(list_of_lists) if i%2 == 0]

##GETTING LISTS AT ODD INDEXES
_list2 = [l_ for i,l_ in enumerate(list_of_lists) if i%2 == 1]

##FLATTENING
from itertools import chain

_list = list(chain.from_iterable(_list))
_list2 = list(chain.from_iterable(_list2))

It iterates over every 5 elements (using OGlist[i:i+5]) in the list (enforced by i%5==0) until it reaches a point where there are less than 5 elements (enforced by len(OGlist) -5).

Testing:

╰─$ python                                                                                                                                                                  25ms
Python 3.8.3 (default, Jul  2 2020, 11:26:31)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> OGlist = list(range(100))
>>> OGlist
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> list_of_lists = [OGlist[i:i+5] for i in range(len(OGlist)-5) if i%5==0]
>>> list_of_lists
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24], [25, 26, 27, 28, 29], [30, 31, 32, 33, 34], [35, 36, 37, 38, 39], [40, 41, 42, 43, 44], [45, 46, 47, 48, 49], [50, 51, 52, 53, 54], [55, 56, 57, 58, 59], [60, 61, 62, 63, 64], [65, 66, 67, 68, 69], [70, 71, 72, 73, 74], [75, 76, 77, 78, 79], [80, 81, 82, 83, 84], [85, 86, 87, 88, 89], [90, 91, 92, 93, 94]]
>>> _list = [val for val in [l_ for i,l_ in enumerate(list_of_lists) if i%2 == 0]]
>>> _list2 = [val for val in [l_ for i,l_ in enumerate(list_of_lists) if i%2 == 1]]
>>> _list
[[0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [20, 21, 22, 23, 24], [30, 31, 32, 33, 34], [40, 41, 42, 43, 44], [50, 51, 52, 53, 54], [60, 61, 62, 63, 64], [70, 71, 72, 73, 74], [80, 81, 82, 83, 84], [90, 91, 92, 93, 94]]
>>> _list2
[[5, 6, 7, 8, 9], [15, 16, 17, 18, 19], [25, 26, 27, 28, 29], [35, 36, 37, 38, 39], [45, 46, 47, 48, 49], [55, 56, 57, 58, 59], [65, 66, 67, 68, 69], [75, 76, 77, 78, 79], [85, 86, 87, 88, 89]]
>>> from itertools import chain
>>> _list = list(chain.from_iterable(_list))
>>> _list2 = list(chain.from_iterable(_list2))
>>> _list
[0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44, 50, 51, 52, 53, 54, 60, 61, 62, 63, 64, 70, 71, 72, 73, 74, 80, 81, 82, 83, 84, 90, 91, 92, 93, 94]
>>> _list2
[5, 6, 7, 8, 9, 15, 16, 17, 18, 19, 25, 26, 27, 28, 29, 35, 36, 37, 38, 39, 45, 46, 47, 48, 49, 55, 56, 57, 58, 59, 65, 66, 67, 68, 69, 75, 76, 77, 78, 79, 85, 86, 87, 88, 89]
>>>

Upvotes: 2

Federico Baù
Federico Baù

Reputation: 7715

I would like to add a more Dynamic solution for this problem!

If you don't know the size if your list/Array for instance we add some elements (and aren't multiple of 5 as well:

OGlist = ['A', 'B', 'C', 'D', 'E', 1, 2, 3, 4, 5, 'F', 'G', 'H', 'I', 'J', 6, 7, 8, 9, 10, 'AB', 'BB', 'CC', 'DD']

Look what happens:

groups = list(zip(*(iter(OGlist),)*5))
list_ =  functools.reduce(operator.iconcat, groups[1::2], [])
list_2 = functools.reduce(operator.iconcat, groups[::2], [])
print(list_, list_2)

OUTPUT

# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']

All of the other elements are gone!

SIMPLE SOLUTION

NOTE MY BE IMPROVE A LOT, IS JUST AS AN EXAMPLE

from itertools import count


OGlist = ['A', 'B', 'C', 'D', 'E', 1, 2, 3, 4, 5, 'F', 'G', 'H', 'I', 'J', 6, 7, 8, 9, 10, 'AB', 'BB', 'CC', 'DD']


def get_partition(lst, start, stop):
    return lst[start:stop]

def alternator(lst):
    counting = count(0, 5)  # MAKES AN ITERATOR
    next(counting)
    limit = len(lst)
    _list = []
    _list2 = []
    prev = 0
    while limit >= 0:
        slicer_first = next(counting)  # GET First 5 Elements
        slicer_second = next(counting) # GET Second 5 Elements

        # ===== < GET ELEMENTS > ===== #
        first_block = get_partition(lst, prev, slicer_first)
        second_block = get_partition(lst, slicer_first, slicer_second)

        # ===== < ADD ELEMENTS TO LISTS > ===== #
        _list += first_block
        _list2 += second_block
        prev = slicer_second

        limit -= 5
    return _list, _list2

for r in alternator(OGlist):
    print("LIST =>", r)
# LIST => ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'AB', 'BB', 'CC', 'DD']
# LIST => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  1. In this example I used itertools.count function

  2. Call Next to counting so we get 5, 10, etc..

  3. From the function get_partition we get the slice of elements.

Upvotes: 0

gregory
gregory

Reputation: 12955

import functools
import operator

OGlist = [A, B, C, D, E, 1, 2, 3, 4, 5, F, G, H, I, J, 6, 7, 8, 9, 10]

groups = list(zip(*(iter(OGlist),)*5))
list_ =  functools.reduce(operator.iconcat, groups[1::2], [])
list_2 = functools.reduce(operator.iconcat, groups[::2], [])

Upvotes: 1

deceze
deceze

Reputation: 522322

I'd approach this like so:

from itertools import chain

_list = list(chain.from_iterable(OGlist[i:i+5] for i in range(0, len(OGlist), 10)))
_list2 = list(chain.from_iterable(OGlist[i:i+5] for i in range(5, len(OGlist), 10)))

OGlist[i:i+5] for i in range(0, len(OGlist), 10) returns a sequence like [[0, 1, 2, 3, 4], [10, 11, 12, 13, 14], ...], chain.from_iterable concatenates that list of lists into one list.

Upvotes: 4

Related Questions