Python Forum
Best way to accommodate unknown number of variables?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best way to accommodate unknown number of variables?
#1
Hi all,

I've run up against this issue a number of times so I thought I'd seek advice on the best way to handle it.

Suppose I have a large data file and I don't know how often a pattern will repeat. Each time it occurs, though, I need to capture details and later evaluate to select which one I'll need.

What is the best way to name variables in this case?

One approach would be to define as many or more sets than I'll ever need. For example:
dte0 = dte1 = dte2 = dte3 = ... = dte50 = 0
row_num1 = row_num2 = row_num3 = ... = row_num50 = 0
That seems like a pain.

I'd rather define and initialize the variables with a loop like:
variable_set = ['dte'+ str(i) for i in range(50)]
print(variable_set)
Now I've got the variable names in a list... could I then initialize them as a loop?

This doesn't work:
for i in len(variable_set):
    int(variable_set[i]) = 0
    
print(variable_set)
Even if that did work, I think I might have just changed the list to all zeros, which loses the variable names for later use in the program. I don't know how to keep the strings as variable names for later use...

Any general thoughts for something like this?
Reply


Messages In This Thread
Best way to accommodate unknown number of variables? - by Mark17 - May-20-2022, 05:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create X Number of Variables and Assign Data RockBlok 8 976 Nov-14-2023, 08:46 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020