Linked Questions

566 votes
19 answers
282k views

I know that some other languages, such as PHP, support a concept of "variable variable names" - that is, the contents of a string can be used as part of a variable name. I heard that this is ...
user avatar
10 votes
2 answers
76k views

I'm just learning to program and am learning Python as my first language. As an exercise I'm trying to write an address book program. New contact are created by the user using the command prompt. ...
ShaunSoda's user avatar
  • 101
0 votes
4 answers
4k views

I'm looking for a way, in python 3.2, to create a large number of variables and assign them values. Something like X = 10 Y = 10 A = 0 B = 0 while X >= 0: while Y >= 0: cell[C]X[A] =...
Luingar's user avatar
  • 39
2 votes
5 answers
13k views

I am trying to automate populating a town by randomly generating households. I generate the name of the town, generate the number of households, the last name of each household and number of occupants ...
CromerMW's user avatar
0 votes
2 answers
18k views

I am trying to create variable for each iteration of the for loop: for i in range(10): x(i) = 'abc' so that I would get x1, x2, x3, x4, .... x10, all equal to 'abc' Anyone know how to do it? Thanks!...
Quy Vu Xuan's user avatar
1 vote
6 answers
9k views

My question is how to create variables "on the fly". I'm trying to generate an object with a random set of attributes. from random import randint, choice class Person(object): def ...
jtsmith1287's user avatar
  • 1,159
3 votes
5 answers
6k views

I'm trying to import datasets which have the following filenames (phone1, phone2, etc) df1 = pd.read_csv(r'C:\Users\...\phone1.csv') df2 = pd.read_csv(r'C:\Users\...\phone2.csv') df3 = pd.read_csv(r'C:...
siiddd's user avatar
  • 53
1 vote
2 answers
19k views

I'm trying to put together a programming project at my university, but I'm stuck. The "math" part I've got covered, but I need help with lists and loops in Python. I'm working with graphs, let me ...
kjubus's user avatar
  • 453
0 votes
1 answer
13k views

I have a function that I made to analyze experimental data (all individual .txt files) This function outputs a dictionary ({}) of Pandas Dataframes Is there a efficient way to iterate over this ...
Adi's user avatar
  • 141
2 votes
2 answers
6k views

I was just wondering if there was a way I could create variables from string like in the code below, I can create a file which includes the variable subject in the filename. filehandle = open('...
Sebastian Southwell's user avatar
-1 votes
2 answers
8k views

I am currently trying to write a simple program to calculate probabilities of possible combinations with dice of different sizes (e.g. : 6, 12, 4, 20). The problem i have is that it is supposed to be ...
Gwendal Delisle Arnold's user avatar
0 votes
3 answers
7k views

I have a file that stores variable names and values like below: name,David age,16 score,91.2 ... I want to write a python script that reads the file and automatically creates variables in an object ...
zleung's user avatar
  • 31
3 votes
2 answers
3k views

I'm a little new to Python, and was hoping that someone might be able to help me with a problem I've got. I have the need to instantiate multiple objects using the variables in a list. Let's say I ...
Simplified's user avatar
0 votes
5 answers
2k views

Is it possible to create a variable name based on the value of a string? I have a script that will read a file for blocks of information and store them in a dictionary. Each block's dictionary will ...
Rauffle's user avatar
  • 1,035
0 votes
1 answer
3k views

I would like to write this: x = 'something_{}'.format(1) exec('{} = {}'.format(x,np.zeros((2,2)))) Problem: I get SyntaxError: invalid syntax and I don't know how to solve it. Someone has an idea?
JrCaspian's user avatar
  • 327
1 vote
2 answers
1k views

I have a list files that I want to iterate over and import as dataframes: data_files = ['data1.csv', 'data2.csv', 'data3.csv'] I'm trying to iterate over the list and create eponymously name ...
Talysin's user avatar
  • 363
1 vote
1 answer
2k views

I am trying to assign every letter in the given word 'cmpny' to an undefined variable(here, i am using counters as variable) var_counter = 0 user_input = 'cmpny' for usr in user_input: ...
a1ft a1ft's user avatar
1 vote
1 answer
2k views

I want to run a for loop with index i so that I can define (inside each loop) an ndarray with name A_i. More concretely, I want it to look something like for i in range(numer): A_i = M where M is ...
TheQuantumMan's user avatar
-2 votes
2 answers
948 views

Here the number of variables (which correspond to numfactors) are assigned manually to each letter of the alphabet. So the first variable is A and it gets assigned the value of an array slice (paths[...
Matt's user avatar
  • 2,872
-3 votes
3 answers
206 views

I have written some classes I would like to test by creating multiple instances of my classes. I would like those instances to have names following the pattern p1, p2, etc: p1 = Point(1,1) p2 = Point(...
Rick's user avatar
  • 45.6k
0 votes
0 answers
1k views

I have a simple problem in python. I am trying to create a varible which uses the value of another variable in its name. A pertinent part of the code is: for m in range (1,4): if m ==1: ...
MWallace's user avatar
3 votes
1 answer
2k views

I have a class for which I want to create instances through a function, but I also want to be able to name the instances with the value of a Tkinter.Entry widget. The simplified version of that I am ...
JuanFiscina's user avatar
-1 votes
1 answer
1k views

Based on the code below, I am trying to create a for loop that has a variable in it but the variable name changes based on counter number. What should I do? I want the final product of this for loop ...
Adel Badiee's user avatar
-1 votes
2 answers
568 views

I want to change a variable using another variable without doing the boring old way of: if choice == 'tuna': tuna_num -= 1 Instead I wanted to do it in a just as quick way as the method seen below: ...
loltrox's user avatar
  • 111
2 votes
2 answers
107 views

Before you say to use a dictionary, I want to change the value of the actual variable. This is what happens with the standard dict strategy: (Note that I'm not necessarily using a list, the value ...
Inbl's user avatar
  • 660
-2 votes
4 answers
303 views

I have a simple question: I have this code: # open an HTML file on my own (Windows) computer url = "$user data here" webbrowser.open(url,new=new) I need to have it so that when it is run the user ...
Justin's user avatar
  • 301
1 vote
4 answers
125 views

I have variables text1, text2, text3. I want to print them. for i in range(1, 4): text = 'text' + str(i) print(text) This code prints strings text1 text2 text3 , not the content of them.
user avatar
0 votes
1 answer
395 views

I would like to convert each item of a list of strings into an empty list object with the string as list name. Input: list_items = ["string1","string2"] Output: 2 empty lists ...
olive's user avatar
  • 199
1 vote
0 answers
357 views

I'm making a piece of code that asks users to input their name and three scores into a program. This will continue happening within a while loop until it is stopped (meaning there could be 2 users ...
Daisy Bradbury's user avatar
0 votes
1 answer
354 views

Let's say I have multiple lists called level_1, level_2, level_3.. Now I want to loop through all of them, but want to use list 1 on idx 1, list 2 on idx 2, .. My solution would be something like this:...
Tomblarom's user avatar
  • 1,469
1 vote
3 answers
65 views

I'm currently making a game for CompSci class, and I want to shorten our random monster fights. Is there a way to make it so when I call a def I can change the name depending on a random variable? ...
user avatar
-4 votes
2 answers
223 views

I have a function in which variables of 'node1', 'node2', 'node3' are referenced. I would like to make it so that the second time the function is called, 'node1' become 'node1_a' and on the third ...
Sam's user avatar
  • 1,062
0 votes
2 answers
166 views

I am having some problems retrieving data from a dictionary that is created in a for loop. In my code I have two dictionaries created and updated periodically, one from a machine database and one from ...
Andrew's user avatar
  • 11
0 votes
1 answer
203 views

I would like to set the variable name in a for loop, like: for i in range(5): namei = i # this is a variable name It will give me: name0 = 0 name1 = 1 name2 = 2 name3 = 3 name4 = 4 does anyone ...
Kan's user avatar
  • 169
-4 votes
1 answer
108 views

I've got an idea for a program that I'm not sure how to implement. How can I make the program ask the user something -- input() -- as many times as user responds until x command is written and (and ...
gonza's user avatar
  • 3
0 votes
4 answers
138 views

I'm having an absolute mind-block. This is my data: db_one = 1 db_two = 2 db_three = 3 db_four = 4 list_to_iterate = ['one','two','three','four'] for each_item in list_to_iterate: new_string = '...
Slowat_Kela's user avatar
  • 1,531
0 votes
0 answers
133 views

this is my first post so I am sorry if I get any formatting wrong. I have searched this before in stackoverflow and it did come up with a few responses but I didn't really understand as I couldn't ...
mre12345's user avatar
  • 1,137
-3 votes
6 answers
130 views

I am using Python 2.5. In the code below want to print either err1, err2 or err3 depending on the value of x. Is it possible to create the variables err1, err2 or err3 by joining "err" plus the value ...
bob_the_bob's user avatar
0 votes
0 answers
119 views

I want to create 6 pandas dataframes from 6 text files. I want to name them according to the names of the text files. The text files are named as 'DAPC_min.txt' and so on. I want the dataframe names ...
anotherone's user avatar
0 votes
1 answer
70 views

Python newbie here: I have a class ExampleState: ... My purpose is to create a class object unique to every user. I get the username from my client flutter app in django. So, instead of state = ...
Earthling's user avatar
  • 489
0 votes
2 answers
91 views

I have a list of IDs I'm trying to manipulate in order to get long strings of the following format: "ID1,ID2,ID3,....,IDn". that is, a long string where the IDs are separated by commas, which are ...
Optimesh's user avatar
  • 2,877
0 votes
2 answers
62 views

I'm coding a text-based adventure game for my python project in school, and I'm currently creating a fight function. It involves a lot of dictionaries, hence why I need to refer to a variable from a ...
paochy's user avatar
  • 11
-2 votes
1 answer
63 views

Sorry if the title is confusing but I couldn't think of a better way to word it. I'm making a game with a friendship system and this is essentially what I'm trying to do: playerChar = "Adam";...
Questionnaire's user avatar
0 votes
1 answer
91 views

Here is the code to start with. Ignoring all the CSV jazz if you want, the important part is the loop. import csv Teacher_Array = [] class Teacher: def __init__ (self, login, password, name, ...
user3688385's user avatar
0 votes
1 answer
41 views

if one had a list of strings, how could they create variables (empty list objects) with those names? pre_vars= ['list_1','list_2','list_3'] print list_1,list_2,list_3 >>> [],[],[] I saw ...
O.rka's user avatar
  • 31k
0 votes
0 answers
74 views

This is the original code: class stock(object): def __init__(self,ticker): self.ticker = ticker tickers = ['KO','JNJ','PFE','NKE','PG','WMT','MMM','IBM'] stocks = [] leng = len(tickers) for i in ...
ian_chan's user avatar
  • 355
0 votes
1 answer
58 views

I've used locals() function and created some variables. Now I need to fetch all the variables created into a list, how should I do? example: column_list=[x for x in range(100)] for i,j in enumerate(...
S.Gu's user avatar
  • 659
-1 votes
5 answers
65 views

line0 = "hello" line1 = "stack" line2 = "overflow" linenum = 1 for x in range(3): linenum = "line%d"%(x) print(linenum) I would like to print hello ...
MrKeviscool's user avatar
0 votes
2 answers
38 views

I'm creating a loop which looks a it like this players = int(input('How many players? ')) for i in range (0,players): name = input('What is your name? ') Then I want the next variable to be '...
user avatar
-1 votes
2 answers
131 views

I'm trying to create a lot of variables inside a loop. for i in range(10): globals()[f"variables{i}"] = i perfect, so now I have variables0 = 0, variables1 = 1, variables2 = 2, etc. But ...
Gaston Malvinas's user avatar

15 30 50 per page