[Tutor] changing list index start

Knacktus knacktus at googlemail.com
Sat Sep 11 16:05:27 CEST 2010


Am 11.09.2010 15:46, schrieb Joel Goldstick:
>
>
> On Sat, Sep 11, 2010 at 9:25 AM, Rance Hall <ranceh at gmail.com
> <mailto:ranceh at gmail.com>> wrote:
>
>     On Fri, Sep 10, 2010 at 6:14 PM, Lie Ryan <lie.1296 at gmail.com
>     <mailto:lie.1296 at gmail.com>> wrote:
>      > On 09/11/10 07:36, Rance Hall wrote:
>
>     <snip>
>
>      > In most cases in Python, you would almost never need to reference the
>      > list's index directly since python makes it easy to use iterators;
>      > however in your particular case, which is a valid exception,
>     enumerate()
>      > takes an optional second argument `start` which defines the
>     number that
>      > enumerate start to count with, i.e. you can do:
>      >
>      > for i, option in enumerate(mainmenuoptions, 1):
>      >    print('%s. %s' % (i, option))
>      >
>      >> php provided a way to change this, but I can find no documentation
>      >> that says python can do this as well.
>      >
>
>
>     Thanks everyone for responding,  Because this menu structure is
>     repeated many times in my code, the ideal solution would have been to
>     "set index start = 1" in the beginning of the script.
>
>     something like sysctl variables in Linux perhaps but in this case only
>     valid for this program.
>
>     Its clear from the responses that this solution is not available in
>     python, I wish it were, it would make my life much easier for this
>     project.
>
>     I like the approach that Lie suggested, as it seems more "natural
>     python" to me as opposed to a workaround.
>
>     However this is also only half a solution since it applies to the
>     printed menus only and not to the response afterward.
>
>
> It might be more trouble than its worth, but you could use a dictionary:
> Instead of this:
>      mainmenuoptions = ['Clients','Jobs','Billing','Quotes','To Do
> Items','Employee','Exit']
>
> do this:
>      mainmenuoptions = {'Clients': 1,'Jobs': 2,'Billing': 3,'Quotes':
> 4,'To Do
> Items': 5,'Employee': 6,'Exit': 7}
>
> Use the phrases as key, and the value as your numeric index.  Or you could
> reverse {1: 'Clients', ...
>
> if that suites your already written code.
>
> If you use this pattern in many places, could you not refactor so that
> you call
> the process with the menu data structures as parameters, and fix the code
> in a single place?
>
>

+1 for the dictionary. A variation of Joels suggestion might be:

shortcuts_to_name_and_functions = {1: ['Clients', clientsmenu], 2: 
['Jobs', jobsmenu]}

for shortcut, name_and_func in shortcuts_to_name_and_functions.items():
     print "%s. %s" % (shortcut, name_and_func[0])

# later make your call
shortcuts_to_name_and_functions[mainchoice][1]()





>
>     It seems that Luke is right looks like we have to do math with the
>     indexes.
>
>     Lie also referred to my particular case as a valid exception, are
>     there enough other such valid exceptions that requesting a feature
>     enhancement would gain some traction?
>
>     If this is but one of a few special cases, I doubt it would be worth
>     the time or trouble to formally make the request.
>
>     Maybe I should ask if there is a better way to do what I want to do
>     here. Is there?
>     _______________________________________________
>     Tutor maillist  - Tutor at python.org <mailto:Tutor at python.org>
>     To unsubscribe or change subscription options:
>     http://mail.python.org/mailman/listinfo/tutor
>
>
>
>
> --
> Joel Goldstick
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list