简体   繁体   English

如何将这一行for循环更改为正常for循环?

[英]How would I change this one line for loop to normal for loop?

This is a general question that I was not to able to understand. 这是一个我无法理解的一般性问题。

If I have this: 如果我有这个:

somelist = [[a for a, b in zip(X, y) if b == c] for c in np.unique(y)]

How can I write this as normal multiline for loop? 如何将其写为正常的多行循环? I never seem to get it right. 我似乎从来没有做对。

EDIT: So far I've tried this: 编辑:到目前为止,我已经尝试过:

somelist = []
for c in np.unique(y):
    for x, t in zip(X, y):
        if t == c:
            separated.append(x)

But I wasn't sure if this was right because I wasn't getting an expected result in some other part of my code. 但我不确定这是否正确,因为我没有在我的代码的其他部分得到预期的结果。

Let me know if this works: evaluate the outer list comprehension first for the outer loop. 让我知道这是否有效:首先为外循环评估外部列表理解。 then evaluate the inner list comprehension. 然后评估内部列表理解。

somelist=[]
for c in np.unique(y):
    ans=[]
    for a,b in zip(X,y):
        if b==c:
            ans.append(a)
    somelist.append(ans)

To flat a nested comprehension out, follow these steps: 要展开嵌套的理解,请按照下列步骤操作:

  1. First create an empty container: somelist = [] 首先创建一个空容器: somelist = []
  2. If the comprehension has an if clause, put it right after the for 如果理解有if子句,请在for
  3. Then, flat the nested comprehensions out, starting with the innermost 然后,从最里面开始,将嵌套的理解展平

The inner comprehension is: 内心理解是:

row = []
for a, b in zip(X, y):
    if b == c:
        row.append(a)

Then, somelist is nothing more than [row for c in np.unique(y)] , where row depends on several factors. 然后,somelist只不过是[row for c in np.unique(y)] row ,其中row取决于几个因素。 This one is equivalent to: 这相当于:

somelist = []
for c in np.unique(y):
    somelist.append(row)

So the complete version is: 所以完整的版本是:

somelist = []
for c in np.unique(y):
    row = []
    for a, b in zip(X, y):
        if b == c:
        row.append(a)
    c.append(row)

This how it looks like using "normal" for-loop (a.ka. without using list comprehension): 这看起来像使用“普通”for循环(a.ka.而不使用列表理解):

somelist = [] 
for c in np.unique(y)
   l = []
   for a, b in zip(X, y):
        if b == c:
           l.append(a)
   somelist.append(l)

Your were very close. 你非常接近。 The problem with your approach is that you forgot an important point: The result of the list comprehension will be a list of lists . 你的方法的问题是你忘记了一个重点: 列表理解的结果将是一个列表列表 Thus, the values computed in the inner loop, need to be held in a temporary list that will be append to the "main" list somelist to create a list of lists: 因此,在内循环中计算的值需要保存在临时列表中,该列表将附加到“主”列表somelist以创建列表列表:

somelist = []
for c in np.unique(y):
    # create a temporary list that will holds the values computed in the
    # inner loop.
    sublist = []
    for x, t in zip(X, y):
        if t == c:
            sublist.append(x)
    # after the list has been computed, add the temporary list to the main
    # list `somelist`. That way, a list of lists is created.
    somelist.append(sublist)

The general rule of thumb when converting a list comprehension to a vanilla for loop is that for each level of nesting, you'll need another nested for loop and another temporary list to hold the values computed in the nested loop. 将列表推导转换为vanilla for循环时的一般经验法则是,对于每个嵌套级别,您将需要另一个嵌套for循环和另一个临时列表来保存嵌套循环中计算的值。

As a caveat, once you start getting past 2-3 leves of nesting in your comprehension, you should seriously consider coveting it to a normal for loop. 作为一个警告,一旦你开始在你的理解中开始超过2-3个嵌套,你应该认真考虑将它co for到正常的循环。 Whatever efficacy you're gaining, it offset my the unreliability of the nested list comprehension. 无论你获得什么功效,它都会抵消我对嵌套列表理解的不可靠性。 Remember, "97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%" . 请记住, “97%的时间:过早的优化是所有邪恶的根源。但我们不应该在那个关键的3%中放弃我们的机会”

After offering the obvious caveat that, for performance and Pythonic reasons, you should not expand your list comprehension into a multi-line loop, you would write it from the outside in: 在提供明显的警告之后,出于性能和Pythonic的原因,您不应该将列表理解扩展为多行循环,您可以从外部编写它:

somelist = []
for c in np.unique(y):
    inner_list = []
    for a, b in zip(X, y):
        if b == c:
            inner_list.append(a)
    somelist.append(inner_list)

And now you see the beauty of list comprehensions. 现在你看到了列表理解的美感。

somelist = []
for c in np.unique(y):
    somelist.append([a for a, b in zip(X, y) if b == c])

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

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