简体   繁体   English

将列表理解转换为循环

[英]convert list comprehension into loop

I'm trying to convert list comprehension into for loop.我正在尝试将列表理解转换为 for 循环。 lst = [ x**2 for x in [x**2 for x in range(11)] ]

I tried with below code我试过下面的代码

lst = []
for x in range(1):
  for x in range(11):
    lst.append(x**4)
print lst

But this does not match with x**2 and x**2 .但这与x**2x**2不匹配。 In my code, there is no x**2 twice在我的代码中,没有x**2两次

lst = []
for x in range(1):
   for x in range(11):
       lst.append(x**4)
print lst

can someone help with for loop where x**2 appears twice?有人可以帮助x**2出现两次的 for 循环吗?

In the list comprehension, the inner comprehension's x is distinct from the variable with the same name in the outer scope.在列表推导中,内部推导的x与外部作用域中的同名变量不同。 When you unroll this, the variables are in the same scope, so they can't use the same name.当您展开它时,变量在同一范围内,因此它们不能使用相同的名称。

lst = []
for y in range(1):
  for x in range(11):
    lst.append(x**4)
print(lst)

But of course, you don't need two loops - a loop over a static list with a single element is entirely pointless.但是,当然,您不需要两个循环 - 对具有单个元素的静态列表进行循环是完全没有意义的。

If you insist on having x**2 twice, that can be done too, of course:如果你坚持要x**2两次,那当然也可以:

lst = []
for x in range(11):
    x = x ** 2
    lst.append(x**2)

But both unrolling the comprehension and breaking up x**2 looks to me like unnecessary changes.但是展开理解和分解x**2在我看来都是不必要的更改。 This is simple enough that it should be understandable to any competent programmer;这很简单,任何有能力的程序员都应该可以理解; maybe add a comment if something seems particularly unobvious.如果某些事情看起来特别不明显,可以添加评论。

By the way, if you are only just learning the basics, you should probably ignore Python 2, and spend your time on the currently recommended and supported version of the language, which is Python 3.顺便说一句,如果您只是在学习基础知识,那么您可能应该忽略 Python 2,而将时间花在该语言的当前推荐和支持的版本上,即 Python 3。

Here you go,干得好,

lst = []
for x in range(11):
    x = x**2
    lst.append(x**2)

This is same as,这与,

for x in range(11):
    lst.append(x**4)

Output:输出:

[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]

If you are very particular that you need to use nested for loop and x**2 twice then you can try the below code,如果您非常特别需要使用嵌套for循环和x**2两次,那么您可以尝试以下代码,

lst = []
for x in range(11):
    lst.append(x**2) # appends squared of x to the lst 
    for x in [lst[-1]]: # loops (one iteration) using the last inserted item of the list `lst`
        lst[-1] = x**2 # performs squared operation on the x value again and update the value in lst
print lst

Output:输出:

[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]

See all in action here .这里查看所有操作。

I will try my best to explain what went wrong我会尽力解释哪里出了问题

lst = [ x**2 for x in [x**2 for x in range(11)] ]
print(lst)
# outputs [0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]

In order to achieve the same results without using list comprehension, what you can do is to split up into 2 lists.为了在不使用列表理解的情况下获得相同的结果,您可以做的是将其拆分为 2 个列表。 The nested list and the output list.嵌套列表和输出列表。

lst2= []
nestedlist = []
for x in range(11):
  nestedlist.append(x**2)
for x in nestedlist:
  x = x**2
  lst2.append(x)

print(lst2)
# outputs [0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]

Notice how in the list comprehension the inner list is iterated first before the the outer list.注意在列表推导式中内部列表是如何在外部列表之前首先迭代的。 Same logic applies to this "split up" version.同样的逻辑适用于这个“拆分”版本。

The reason why doing something like the following: is WRONG这样做的原因如下:是错误的

lst2= []
nestedlist = []
for x in range(11):
  nestedlist.append(x**2)
  for x in nestedlist:
    lst2.append(x**2)

print(lst2)
#outputs [0, 0, 1, 0, 1, 16, 0, 1, 16, 81, 0, 1, 16, 81, 256, 0, 1, 16, 81, 256, 625, 0, 1, 16, 81, 256, 625, 1296, 0, 1, 16, 81, 256, 625, 1296, 2401, 0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]

Is because when you are doing the following,是因为当你在做以下事情时,

  for x in nestedlist:
    lst2.append(x**2)

What you are actually doing is applying **2 the LENGTH of the list.您实际上正在做的是应用**2列表的长度 Hence outputting for example [0,1] to [0,1,0,1], not the elements in the list.因此输出例如 [0,1] 到 [0,1,0,1],而不是列表中的元素。

I guess list comprehension is smart in that sense.我想列表理解在这个意义上是聪明的。

lst =[]
tmp = []
for x in range(11):
    tmp.append(x**2)

for x in tmp:
    lst.append(x**w) 

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

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