Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Dynamic variable names

Reply
Thread Tools

Re: Dynamic variable names

 
 
Steve Holden
Guest
Posts: n/a
 
      02-07-2010
R (Chandra) Chandrasekhar wrote:
> Dear Folks,
>
> I have read that one should use a dictionary in python to accommodate
> dynamic variable names. Yet, I am puzzled how to achieve that in my
> case. Here is what I want to do:
>
> 1. The user inputs a number of six-digit hex numbers as a
> comma-separated list. These numbers represent colours, but the number of
> colours is not known beforehand.
>
> 2. Using these colours in pairs, I am generating image files whose names
> must be unique. I could use the respective hex numbers for this, but
> would like to explore generating filenames like
>
> colour_1-colour_2.jpg
>
> Because I do not know how many colours there would be in advance, I need
> to generate the colour_n names on the fly.
>
> So, my two questions are:
>
> 1. How do I do this properly in python?
>
> 2. If there is a better scheme than what I have outlined, can someone
> please point me to a Web link?
>

Here's one way, though not necessarily the best:

>>> import itertools
>>> ctr = itertools.count(1)
>>> for i in range(5):

.... print "colour_%03d-colour%03d.jpg" % (ctr.next(), ctr.next())
....
colour_001-colour002.jpg
colour_003-colour004.jpg
colour_005-colour006.jpg
colour_007-colour008.jpg
colour_009-colour010.jpg
>>>


I zero-filled the names so they sort in numerical order. If this isn't a
requirement then simply change the format string to

"colour_%d-colour%d.jpg"

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic variable names without EVAL - NEWBIE jason@cyberpine.com ASP .Net 2 07-15-2010 02:39 PM
dynamic variable names or using a variable as another's name Peter Buckley Ruby 5 02-27-2009 06:05 PM
checking for mis-spelled variable names / function names News123 Python 2 11-26-2008 12:37 AM
confusion between global names and instantiated object variable names wanwan Python 3 10-14-2005 09:46 PM
Dynamic Variable Names - Syntax? Ken Halley ASP General 5 11-07-2003 03:24 AM



Advertisments