简体   繁体   中英

convert list comprehension into loop

I'm trying to convert list comprehension into for loop. 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 . In my code, there is no x**2 twice

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?

In the list comprehension, the inner comprehension's x is distinct from the variable with the same name in the outer scope. 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:

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

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,

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. 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. Hence outputting for example [0,1] to [0,1,0,1], not the elements in the list.

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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