How can I use the 'for' loop for one variable in a function that depends on two variables?

I just want to use the for loop for t variable in my function:

 l = [] def func(s): for i in range(1, 100): t = i p = t * 2 + s * 2 return p l.append(func(10)) print l 

I want the value of t to start from 1 to 99 and print a list of all the values, but I always get l = [218] .

+6
source share
3 answers

I assume you installed NumPy (at least your original question suggested), so I will tell you how you can get your result using numpy-arrays very efficient way (without any jumps and explicit iterations):

 > import numpy as np > s = 10 > l = np.arange(1, 100) * 2 + s * 2 # Arrange produces an array. Syntax is like "range". > print(l) array([ 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218]) 

I used the fact that mathematical operations on NumPy arrays affect all elements. Therefore, it is so easy to write an operation: np.arange(1, 100) * 2 , because it multiplies each element by 2 and numpy.arange is an opportunity to create an array containing all the numbers between the given start and stop with an optional step (like python range ).

In NumPy, this would not be a good choice for append single values ​​(because it recreates the entire array, rather than just adding values). In most cases, it is best to create an array with a finite size and shape and act directly on it. Of course, you can concatenate or append various NumPy arrays, but as already mentioned, it always creates a completely new array and therefore is not very efficient (if used regularly).


So, now a few notes on your initial attempt and why it didn’t work: your function created a lot of numbers, but it overrides them in each loop and returns only the last one:

 def func(s): for i in range(1, 100): t = i p = t * 2 + s * 2 # Next iteration of the loop just overwrites p # Returns only the last p return p 

You can make it a generator (with yield instead of return ), but it will probably overflow here, I will show it :-)

 l = [] def func(s): for i in range(1, 100): p = i * 2 + s * 2 yield p l.append(list(func(10))) # Need to convert the generator to a list here. # In this case a simple "l = list(func(10))" would be easier if you don't need to append. 
+41
source

You calculate the value several times (until you add it to the list), and then add the final value to the list.

Instead, we want to add to the list as we go.

 def func(s): l = [] for i in range(1, 100): l.append(i*2+s*2) return l 

You can also do this on a single line using list comprehension if you are going for a short time.

 func = lambda s: [(t*2)+s*2 for t in range(1, 100)] 

Output:

 [22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218] 

Pay attention to 218 at the end. As I said, you were returning the last value, so your program printed 218.

+4
source

Since there are many solutions that are. With different libraries or generators. You only had an indentation problem in your code, and it was ready to fly. Here is a small modification to your code.

 #In Python3 def func(s): for i in range(1,100): p = i * 2 + s * 2 l.append(p) return l l =[] print(func(10)) 
0
source

All Articles