How can I generate variables with 100 indices? [duplicate] - python

This question already has answers here:
How to programmatically set a global (module) variable?
(3 answers)
Closed 8 years ago.
I'm very beginner at python and gurobipy library,
and I want to generate bunch of individual variables with indices, like x1, x2, ... ,x100...
Is there any faster and easier way to generate them?
I tried "for" structure in various form but shell keeps returning syntax error message... :(
HELP PLEASE!

If you just want a group of values that can be indexed, use a list (indexes are 0 start):
mylist = ["some item", 23, "other item", 99]
twentythree = mylist[1]
You can add items to the list with .append(<item>):
mylist.append("new item")
You can make a list of 100 items with range(100), but the resulting list will contain values 0 to 99.

Can't say that I like doing it this way - but whenever you have to do such tedious work manually...
for i in range(1,101):
globals()["x{}".format(i)] = "test"
print x53 # prints test

Related

Question caused by lambda expression in python? [duplicate]

This question already has answers here:
How does a lambda function refer to its parameters in python?
(4 answers)
Closed 1 year ago.
I wrote a Python code like:
fun_list = []
for i in range(10):
fun_list.append(lambda : f(i))
for j in range(10):
fun_list[j]()
I want it to output numbers from 0 to 9, but actually it outputs 9 for ten times!
I think the question is that the variable is be transported into function f only it was be called. Once it was be called it will globally find variable named 'i'.
How to modify the code so that it can output numbers from 0 to 9?
Your Code doesn't make sensefun_list[j]() how are you calling a List Element?
If You Want to append the numbers mentioned above into your array then Correct Code is:-
fun_list = []
for i in range(10): ### the range has to be from 0 to 10 that is [0,10)
fun_list.append(i) ### You dont need lambda function to append i
What This does is also if of No need because you first of all havent initialized what is f?... Is it a function?. You here are not appending the value but instead appending the called function statement. You dont need that. Just do it simply like above....:-
fun_list.append(lambda : f(i))

How to change the specific values within a dictionary of lists in python [duplicate]

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 1 year ago.
This is the worst blocker that I have ever had attempting to learn Python. I spent about 10 hours on this one and could really use help.
I want to have dictionaries hold lists in python and then access and change a specific value in those lists. When I do this the way that seems logical to me and access a key then list value, it actually replaces other key's list values.
If I just assign 100 to the key XList it prints 100 for both XList and YList. Why does assigning to one key effect the other???
And if I uncomment the assignment to YList it prints 254 for both. Why can't I assign itemized values to keys and lists like this? How can I assign specific values to lists within dictionaries and access the main lists via keys?? Why does what seems like changing just one keys list value change both key's lists???
testDictKeys= ['XList', 'YList', 'ZList']
testDict = dict.fromkeys(['XList', 'YList', 'ZList'])
noneFillerList = [None] * 30
#Now fill all the columns and row of the dictionary with zeros
for x in range(0,2):
testDict[testDictKeys[x]] = noneFillerList
for x in range(0, 29):
testDict['XList'][x]=100
#testDict['YList'][x]=254
print (testDict['XList'][0])
print (testDict['YList'][0])
Any help is appreciated.
The problem is that you only create one list and assign every dictionary to contain it. To fix the problem, be sure to create a new list to add to each dictionary.

How do I make different variables in for loops In Python [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 3 years ago.
I am making a program that has a for loop and every time the loop is ran I want it to make a new variable like
for item in range(0, size)
(Make new variable bit1, bit2, bit3, bit4, etc with value of 0)
Is this possible?
Create a List of variables and append to it like this:
bits = []
for item in range(0, size)
bits.append(0)
# now you have bits[0], bits[1], bits[2], etc, all set to 0
We can do this by appending to the vars() dictionary in the program.
for index, item in enumerate(range(size)):
vars()[f'bit{index+1}'] = 0
Try calling the names now:
>>> bit1
0
And while this works, I'd recommend using a list or a dict instead.

Iterating and changing elements in a list in Python [duplicate]

This question already has answers here:
Can't modify list elements in a loop [duplicate]
(5 answers)
Closed 4 years ago.
I'm supposed to find the largest number in a list, and then change all the elements of the list to that number. But when I print the output after the iteration, it still hasn't changed. What am I doing wrong please ?
Ar =[5,4,11]
ArMax = max(Ar)
for i in Ar:
i = ArMax
print(Ar)
The list doesn't change because you've done nothing to change the list. Your list is Ar -- where have you assigned a new value to anything in that list? Nowhere. All you did was to create a local variable to take on the values in Ar, and then change the value of that variable. You never touched the original list. Instead, you have to change the list elements themselves, not their copies. Keeping close to your present code:
for i in range(len(Ar)):
Ar[i] = ArMax
Another way would be to create a new list with those items in it, and simply replace the original list all at once:
Ar = [ArMax] * len(Ar)
This looks to see how long the current list is with len(Ar). Then it takes a one-element list with the max value and replicates it that many times. That new list becomes the new value of Ar.
i = ArMax does not actually assign values to the list because the values of i are copies of the elements in the list. If you want to fix this, try:
for i in xrange(len(Ar)):
Ar[i] = ArMax

how to define a list with predefined length in Python [duplicate]

This question already has answers here:
Create a list with initial capacity in Python
(11 answers)
Closed 9 years ago.
I want to do the following :
l = list()
l[2] = 'two'
As expected, that does not work. It returns an out of range exception.
Is there any way to, let say, define a list with a length ?
Try this one
values = [None]*1000
In place of 1000 use your desired number.

Categories