English 中文(简体)
How to increment variable names/Is this a bad idea [duplicate]
原标题:

In Python, if I were to have a user input the number X, and then the program enters a for loop in which the user inputs X values, is there a way/is it a bad idea to have variable names automatically increment?

ie:

user inputs  6   
value_1 = ...  
value_2 = ...  
value_3 = ...  
value_4 = ...  
value_5 = ...  
value_6 = ...

Can I make variable names increment like that so that I can have the number of variables that the user inputs? Or should I be using a completely different method such as appending all the new values onto a list?

问题回答

You should append all the values to a list, that allows you to easily iterate through the values later as well as not littering your namespace with useless variables and magic.

While using a list or dictionary is of course the right approach here, it is possible to create named variables, using exec(). You ll miss out on all the convenient operations that lists and dictionaries provide (think of len(), inserting, removing, sorting, and so on), but it s possible:

count = raw_input( Number of variables: )
for i in xrange(count):
    exec( var_  + str(i) +   =   + str(i))

exec() accepts and executes strings that contain valid Python expressions. So the above piece of code is generating and executing code on the fly.

should I be using a completely different method such as appending all the new values onto a list?

Yes, you can use a list. You can also use a mapping for this.

make variable names increment like that

No. That s a horrifyingly bad idea.


"but just out of complete curiosity, is these a way to do it in the asked manner?"

Yes, but it s clearly a horrifyingly bad idea. So, there s no point in showing it. It s a terrible, terrible thing to even attempt.

Maybe this will help you out.

filenamee =  Value_ 
counter1 = 1
counter2 = 2
list1 = []
list2 = range(30)    #random number, given len needed
for x in list2:
    counter1 = str(counter1)
    full_name = (filenamee+counter1)
    list1.append(full_name)
    counter1 = counter2
    counter2+=1

for x in list1:
    print(x)

If you re looking to make an incremental variable, this works for me. I ve got no clue whether this is clean programming or not... :)

Take care





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签