Python |给定每个索引的特定数字范围,如何将值分组到主列表中的不同列表

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 = []

所以,我有一个列表 OGlist...我想将 OGlist 的前 5 个元素放入 _list,将 OGlist 的第二个 5 个元素放入 _list2,将第三个 5 个元素放入 _list,将第四个 5 个元素放入 _list2,然后等等

如何实现?

我试过这个:

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

我应该使用什么条件和逻辑来获得所需的输出?

期望的输出:

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

如果您正在寻找一种衬垫,这应该可以。

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))

它迭代列表中的每 5 个元素(使用 OGlist[i:i+5])(由 i%5==0 强制执行)直到它到达少于 5 个元素的点(由强制执行) len(OGlist) -5).

测试:

╰─$ 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]
>>>

我会这样处理:

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 像 [[0, 1, 2, 3, 4], [10, 11, 12, 13, 14], ...]chain.from_iterable 这样的序列将列表的列表连接成一个列表。

这是我的代码

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]
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], [])

我想为这个问题添加一个更动态的解决方案!

如果您不知道尺寸,例如您的 list/Array 我们会添加一些元素(并且也不是 5 的倍数:

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']

看看会发生什么:

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)

输出

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

所有其他元素都消失了!

简单的解决方案

请注意,我的改进很大,仅作为示例

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. 在这个例子中我使用了itertools.count函数

  2. 调用 Nextcounting 所以我们得到 5、10 等等。

  3. 从函数get_partition我们得到元素切片。