简体   繁体   English

如何拆分每个第 N 个元素的 Python 列表

[英]How to Split Python list every Nth element

What I am trying to do is pretty simple, but I couldn't find how to do it.我想要做的很简单,但我找不到如何去做。

  • Starting with 1st element, put every 4th element into a new list.从第一个元素开始,将每第四个元素放入一个新列表中。
  • Repeat with the 2nd, 3rd, and 4th elements.重复第 2、3 和 4 个元素。

From:从:

list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b']

To:到:

list1 = ['1', '5', '9']
list2 = ['2', '6', 'a']
list3 = ['3', '7', 'b']
list4 = ['4', '9']

In other words, I need to know how to:换句话说,我需要知道如何:

  • Get the Nth element from a list (in a loop)从列表中获取第 N 个元素(在循环中)
  • Store it in new arrays将其存储在新数组中
  • Repeat重复

The specific solution is to use slicing with a stride:具体的解决方案是使用跨步切片:

source = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b']
list1 = source[::4]
list2 = source[1::4]
list3 = source[2::4]
list4 = source[3::4]

source[::4] takes every 4th element, starting at index 0; source[::4]从索引 0 开始,每 4 个元素取一个; the other slices only alter the starting index.其他切片只改变起始索引。

The generic solution is to use a loop to do the slicing, and store the result in an outer list;通用的解决方案是使用循环进行切片,并将结果存储在外部列表中; a list comprehension can do that nicely:列表理解可以很好地做到这一点:

def slice_per(source, step):
    return [source[i::step] for i in range(step)]

Demo:演示:

>>> source = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b']
>>> source[::4]
['1', '5', '9']
>>> source[1::4]
['2', '6', 'a']
>>> def slice_per(source, step):
...     return [source[i::step] for i in range(step)]
... 
>>> slice_per(source, 4)
[['1', '5', '9'], ['2', '6', 'a'], ['3', '7', 'b'], ['4', '8']]
>>> slice_per(source, 3)
[['1', '4', '7', 'a'], ['2', '5', '8', 'b'], ['3', '6', '9']]

The numbered names are a bad idea, and you shouldn't name your own variable list (it shadows the built-in), but in general you can do something like:编号名称是一个坏主意,你不应该命名你自己的变量list (它隐藏了内置),但通常你可以这样做:

>>> startlist = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b']
>>> n = 4
>>> endlist = [[] for _ in range(n)]
>>> for index, item in enumerate(startlist):
    endlist[index % n].append(item)


>>> endlist
[['1', '5', '9'], ['2', '6', 'a'], ['3', '7', 'b'], ['4', '8']]
lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'] s = [] n = 3 for x in range(n): s.append(lst[x::n]) print(s)

Alternative approach:替代方法:

list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b']
lists = []
list_temp = []

for _, __ in enumerate(list, 1):
  list_temp.append(__)
  if _ % 3 == 0 or _ == len(list):
    lists.append(list_temp)
    list_temp = []

This will append the chunks in a temporary list and append the temporary list to our final list every time the index divided by the desired list length has no remainders.这将在临时列表中附加块,并在每次索引除以所需列表长度没有余数时将临时列表附加到我们的最终列表中。

It's more a mathematical approach than a pythonic one :)它更像是一种数学方法而不是 Pythonic 方法 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM