[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Increment Variable Name

Dave Dave

1/23/2008 10:46:00 PM

This is probably really trivial but I'm stumped.... :-(

Does anyone know how to increment a variable name?

For example:

I know the length of a list and I want to pass each element of a list
to a unique variable, thus I want to increment variable names. If the
list length = 4, i want to have the following variables: var1, var2,
var3, var4.


Thanks
9 Answers

Ben Finney

1/23/2008 11:07:00 PM

0

David Brochu <brochu121@gmail.com> writes:

> I know the length of a list and I want to pass each element of a
> list to a unique variable, thus I want to increment variable names.
> If the list length = 4, i want to have the following variables:
> var1, var2, var3, var4.

This has a very bad code smell. What problem are you trying to solve
by doing this? I strongly suspect there's a better solution.

--
\ "If you do not trust the source do not use this program." |
`\ â??Microsoft Vista security dialogue |
_o__) |
Ben Finney

Pablo Ziliani

1/23/2008 11:20:00 PM

0

Ben Finney wrote:
> This has a very bad code smell (...)
>
> \ `\ _o__) Ben Finney

That is forcefulness.
(sorry, couldn't resist)

Diez B. Roggisch

1/23/2008 11:28:00 PM

0

David Brochu schrieb:
> This is probably really trivial but I'm stumped.... :-(
>
> Does anyone know how to increment a variable name?
>
> For example:
>
> I know the length of a list and I want to pass each element of a list to
> a unique variable, thus I want to increment variable names. If the list
> length = 4, i want to have the following variables: var1, var2, var3, var4.
>

Use a dictionary

value_dict = {}

for i, value in values:
value_dict["var%i" % i] = value


Diez

Grant Edwards

1/23/2008 11:54:00 PM

0

On 2008-01-23, Diez B. Roggisch <deets@nospam.web.de> wrote:
> David Brochu schrieb:
>> This is probably really trivial but I'm stumped.... :-(
>>
>> Does anyone know how to increment a variable name?
>>
>> For example:
>>
>> I know the length of a list and I want to pass each element of a list to
>> a unique variable, thus I want to increment variable names. If the list
>> length = 4, i want to have the following variables: var1, var2, var3, var4.
>>
>
> Use a dictionary
>
> value_dict = {}
>
> for i, value in values:
> value_dict["var%i" % i] = value

That assumes that the OPs "list" is actually a list of tumples:

[(1,firstvalue),(2,secondvalue), (3, thirdvalue), ...]

I'd adjust my thinking (regarding 0/1 based counting) and just
use a list or a tuple:

var = list(values)
or
var = tuple(values)

In either case, you now have

var[0], var[1], var[2], var[3], ...



If you insist on numbers starting at 1, then a dict would work:

var = {}
for i,value in itertools.enumerate(itertools.count(1), values):
var[i] = value

now you have

var[1], var[2], var[3], var[4], ...



--
Grant Edwards grante Yow! I'm continually AMAZED
at at th'breathtaking effects
visi.com of WIND EROSION!!

Ben Finney

1/24/2008 12:15:00 AM

0

Pablo Ziliani <pablo@decode.com.ar> writes:

> Ben Finney wrote:
> > This has a very bad code smell (...)
> >
> > \ `\ _o__) Ben Finney
>
> That is forcefulness.
> (sorry, couldn't resist)

I suspect that's a comment on my online nickname, "bignose", and
talking about code smells.

Nevertheless, it's probably a good opportunity to point out that "code
smell" <URL:http://martinfowler.com/bliki/CodeSmel... is a term
with a specific, useful meaning in programming.

--
\ "[Freedom of speech] isn't something somebody else gives you. |
`\ That's something you give to yourself." -- _Hocus Pocus_, Kurt |
_o__) Vonnegut |
Ben Finney

Diez B. Roggisch

1/24/2008 9:36:00 AM

0

Grant Edwards schrieb:
> On 2008-01-23, Diez B. Roggisch <deets@nospam.web.de> wrote:
>> David Brochu schrieb:
>>> This is probably really trivial but I'm stumped.... :-(
>>>
>>> Does anyone know how to increment a variable name?
>>>
>>> For example:
>>>
>>> I know the length of a list and I want to pass each element of a list to
>>> a unique variable, thus I want to increment variable names. If the list
>>> length = 4, i want to have the following variables: var1, var2, var3, var4.
>>>
>> Use a dictionary
>>
>> value_dict = {}
>>
>> for i, value in values:
>> value_dict["var%i" % i] = value
>
> That assumes that the OPs "list" is actually a list of tumples:

Tumples? :)

I forgot the enumerate...

Diez

Jan Wicijowski

1/24/2008 12:03:00 PM

0

On Jan 23, 11:45 pm, David Brochu <brochu...@gmail.com> wrote:
> This is probably really trivial but I'm stumped.... :-(
>
> Does anyone know how to increment a variable name?
>
> For example:
>
> I know the length of a list and I want to pass each element of a list  
> to a unique variable, thus I want to increment variable names. If the  
> list length = 4, i want to have the following variables: var1, var2,  
> var3, var4.
>
> Thanks

Do you want to do this?:

>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', 'pywin': <module 'pywin' from 'C:\Python25\Lib\site-
packages\pythonwin\pywin\__init__.pyc'>, '__doc__': None}
>>> locals()['var'+str(1)] = "spam"
>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', 'var1': 'spam', 'pywin': <module 'pywin' from 'C:
\Python25\Lib\site-packages\pythonwin\pywin\__init__.pyc'>, '__doc__':
None}
>>> var1
'spam'

Paul Hankin

1/24/2008 12:36:00 PM

0

On Jan 24, 12:02 pm, janislaw <wicijow...@gmail.com> wrote:
> On Jan 23, 11:45 pm, David Brochu <brochu...@gmail.com> wrote:
>
> > This is probably really trivial but I'm stumped.... :-(
>
> > Does anyone know how to increment a variable name?
>
> > For example:
>
> > I know the length of a list and I want to pass each element of a list
> > to a unique variable, thus I want to increment variable names. If the
> > list length = 4, i want to have the following variables: var1, var2,
> > var3, var4.
>
> > Thanks
>
> Do you want to do this?:
> locals()['var'+str(1)] = "spam"

As I recently learnt in this newsgroup, that's not guaranteed to work.
From http://docs.python.org/lib/built-in-...

Warning: The contents of this dictionary should not be modified;
changes may not affect the values of local variables used by the
interpreter.

--
Paul Hankin

Terry Reedy

1/24/2008 10:34:00 PM

0


"Paul Hankin" <paul.hankin@gmail.com> wrote in message
news:fdff5a3c-376c-41da-bc7c-| > Do you want to do this?:
| > locals()['var'+str(1)] = "spam"
|
| As I recently learnt in this newsgroup, that's not guaranteed to work.
| >From http://docs.python.org/lib/built-in-...
|
| Warning: The contents of this dictionary should not be modified;
| changes may not affect the values of local variables used by the
| interpreter.

It currently works at module level, where locals() is globals(). But then,
one might as well write globals()['var'+str(1)] = "spam". But in Python,
one is almost certainly better to use a collection object than this idiom
(indefinite multiple numbered variables) imported from languages which do
not have them.