简体   繁体   English

每个 n 元素划分一个 Python 列表

[英]Divide a Python list every n-element

Recently, I've found a code for developing a set of lists from a list, that code was written by user Mai , answering question but i have not understood it yet.最近,我发现了一个从列表中开发一组列表的代码,该代码是由用户Mai编写的,回答问题但我还没有理解它。 Could somebody help me to understand it?有人可以帮我理解吗? And... is there a way to rewrite that code that be easier?而且...有没有办法更容易地重写该代码? The code is:代码是:

def even_divide(lst, num_piece=4):
    return [
        [lst[i] for i in range(len(lst)) if (i % num_piece) == r]
        for r in range(num_piece)
    ]

Thanks!谢谢!

It's pretty simple actually.其实很简单。 Just follow through the values of the two loops:只需遵循两个循环的值:

Starting with the outer loop, r would be 0 , then 1 , then 2 , etc. Let's look at the case for which r == 1 .从外部循环开始, r将是0 ,然后是1 ,然后是2 ,等等。让我们看看r == 1的情况。 When running through the different values of i , (which would be 0, 1, 2, ... len(lst) , the value of i % 4 , meaning the remainder of dividing i by 4 , would be 0, 1, 2, 3, 0, 1, 2, 3, ... . So the i % 4 would be equal to r , for every 4 values of i !当运行i的不同值时(即0, 1, 2, ... len(lst)i % 4的值,即i除以4的余数将为0, 1, 2, 3, 0, 1, 2, 3, ...因此,对于 i 的每 4 个值, i i % 4等于r

For our chosen r == 1 , that would mean we're choosing lst[1], lst[5], lst[9], ... , etc.对于我们选择的r == 1 ,这意味着我们选择lst[1], lst[5], lst[9], ...等。

And for r == 2 ?对于r == 2 You guessed it!你猜对了! You'd be picking up lst[2], lst[6], lst[10],... .你会拿起lst[2], lst[6], lst[10],...

So over all you'd get 4 lists, with non-overlapping elements of the original list, by just "jumping" 4 elements every time, but starting at different values.所以总的来说,你会得到 4 个列表,其中原始列表的元素不重叠,每次只需“跳跃”4 个元素,但从不同的值开始。

Which naturally leads to the more simple solution:这自然会导致更简单的解决方案:

def even_divide(lst, num_piece=4):
    return [lst[r::num_piece] for r in range(num_piece)]

Could somebody help me to understand it?有人可以帮我理解吗?

Sure.当然。 It's a list comprehension.这是一个列表理解。 A list comprehension takes a list and does something to or with every element in that list: Let's say I want to multiply every element in my list by 2:列表推导接受一个列表并对列表中的每个元素执行某些操作:假设我想将列表中的每个元素乘以 2:

new_list = [element*2 for element in my_list]

What makes it a list comprehension is the bracket syntax.使它成为列表理解的是括号语法。 For those new to it, that's usually the part that takes a moment to get used to.对于那些不熟悉它的人来说,这通常是需要一点时间来适应的部分。 With that said, I assume that is what is giving you difficulty in understanding the code in your question, as you have a list comprehension in a list comprehension.话虽如此,我认为这就是让您难以理解问题中的代码的原因,因为您在列表理解中具有列表理解。 It might be difficult to understand now, but list comprehensions are a wonderful thing in python.现在可能很难理解,但列表推导在 python 中是一件很棒的事情。

But, as this post mentions, there's a lot of discussions around list comprehension, lambda's, map, reduce, and filter.但是,正如这篇文章所提到的,有很多关于列表理解、lambda、map、reduce 和 filter 的讨论。 Ultimately, its up to you to decide what's best for your project.最终,由您决定什么最适合您的项目。 I'm not a fan of anything else but list comprehensions, so I use those religiously.除了列表推导之外,我不喜欢其他任何东西,所以我虔诚地使用它们。

Based on the question you've linked, the list comprehension takes a 1d list of length x and turns it into a 2d list of (length x, width y).根据您链接的问题,列表理解采用长度 x 的一维列表并将其转换为(长度 x,宽度 y)的二维列表。 It's like numpy.reshape .这就像numpy.reshape

And... is there a way to rewrite that code [to] be easier?而且...有没有办法重写该代码[以]更容易?

I would not recommend it.我不会推荐它。 List comprehensions are considered very pythonic and you will see them everywhere.列表推导被认为是非常 Python 的,你会在任何地方看到它们。 Best to use them and get used to them.最好使用它们并习惯它们。

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

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