Using a for loop to iterate and create new variables [duplicate] - python

This question already has answers here:
How do I create variable variables?
(17 answers)
How can you dynamically create variables? [duplicate]
(8 answers)
Closed 6 days ago.
for x in range(1, 8):
num[x] = random.randint(0, 9)
x becomes the numbers 1-7 and i want to assign them to values num1 = random.randint(0, 9), num2... etc. the x should iterate and become part of the variable? how do i combine these to successfully make the variable?
I wanted the variables to become num1, num2, num3 and so on

Related

Lambda in list comprehension returning effectively the same lambda [duplicate]

This question already has answers here:
Local variables in nested functions
(4 answers)
Lambda in a loop [duplicate]
(4 answers)
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Closed 2 years ago.
I want to generate the Lagrange basis polynomials for a set of points where T is the set of points and n is the amount of given points. What I did is ci = [lambda t: np.prod([(t - T[j]) / (T[i] - T[j]) for j in range(n) if j != i] for i in range(n)], however, the resulting lambdas are all the same.
I checked by printing [c(val) for c in ci] in the console for several different values and it all returns the same value. How is this possible, when clearly the formula I put in should return lambdas with a different structure for each i?

Name binding in function statements [duplicate]

This question already has answers here:
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Python Argument Binders
(7 answers)
Closed 2 years ago.
The following Python program:
d = {}
for x in range(3):
d[x] = lambda: x
print(d[0](), d[1](), d[2]())
outputs:
2 2 2
x is bound by reference in the lambda expression. After the for statement, x is bound to 2, which explains the output.
I would like x to be bound by value instead, to get the following output:
0 1 2
How can I achieve this?

Python lists being edited in place? [duplicate]

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Is i = i + n truly the same as i += n? [duplicate]
(3 answers)
Closed 2 years ago.
So this is happening:
a = [1,2]
b = a
b += [3]
print(a)
[1, 2, 3]
It's extremely confusing and it's screwing up my project's test suite, which takes a list of strings and tests that it gets modified in the correct ways by each function.
Did I just find a bug in Python, or is there some convoluted reason for this misbehavior?

For loop + Booleans [duplicate]

This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
How can I use `return` to get back multiple values from a loop? Can I put them in a list?
(2 answers)
Closed 3 years ago.
Given this code:
nums = [1,2,3]
def test1(nums):
for i in nums:
if i == 1:
return True
return False
test1(nums)
The result is True even though after the for loop there is a return False. What am I missing?

Is a Python list's += operator equivalent to append() or extend()? [duplicate]

This question already has answers here:
Concatenating two lists - difference between '+=' and extend()
(11 answers)
Python append() vs. + operator on lists, why do these give different results?
(7 answers)
Closed 6 years ago.
Python lists have a += operator as well as append and extend methods.
If l is a list, is l += ... equivalent to l.append(...), l.extend(...), both, or neither?
In python += on a list is equivalent to extend method on that list.

Categories

Resources