[Tutor] List index usage: is there a more pythonesque way?

2010-04-19 Thread C M Caine
Spir sent this solely to me by accident, I think. -- Forwarded message -- From: spir ☣ Date: 2010/4/19 Subject: Re: [Tutor] List index usage: is there a more pythonesque way? To: cmca...@googlemail.com On Mon, 19 Apr 2010 12:59:40 +0100 C M Caine wrote: > That's t

Re: [Tutor] List index usage: is there a more pythonesque way?

2010-04-19 Thread Wayne Werner
On Mon, Apr 19, 2010 at 9:23 AM, Alan Gauld wrote: > > "C M Caine" wrote > >> That's the first I've read of iterating through dictionaries, I'd >> >> assumed it was impossible because they're unordered. >> > > Iteration doesn't require order, only to get each item once. > Even in very old Python

Re: [Tutor] List index usage: is there a more pythonesque way?

2010-04-19 Thread Alan Gauld
"C M Caine" wrote That's the first I've read of iterating through dictionaries, I'd assumed it was impossible because they're unordered. Iteration doesn't require order, only to get each item once. Even in very old Python versions you could iterate a dictionary via the keys() method. More

Re: [Tutor] List index usage: is there a more pythonesque way?

2010-04-19 Thread C M Caine
That's the first I've read of iterating through dictionaries, I'd assumed it was impossible because they're unordered. Your explanation for defining your own iterables is much easier to understand than the one I read before as well. Thanks again. 2010/4/19 spir ☣ : > On Mon, 19 Apr 2010 00:37:11

Re: [Tutor] List index usage: is there a more pythonesque way?

2010-04-19 Thread spir ☣
On Mon, 19 Apr 2010 00:37:11 +0100 C M Caine wrote: > That's two new things I've learnt. I didn't realise that for loops > could be used like that (with more than one... key?). Consider considering things differently: a for loop always iterates over items of a collection you indicate: l = [1,2

Re: [Tutor] List index usage: is there a more pythonesque way?

2010-04-19 Thread ALAN GAULD
> That's two new things I've learnt. I didn't realise that for loops > could be used like that (with more than one... key?). Technically its still one key but enumerate returns a tuple of index and value and we use tuple unpacking to assign the values to the loop variables. That is we could writ

Re: [Tutor] List index usage: is there a more pythonesque way?

2010-04-18 Thread C M Caine
> Something is wrong in the following if statement, as both paths execute the > same code. > >>             if spaceDict['1st'+key] == 0: >>                 spaceDict['nth'+key] = i >>             else: >>                 spaceDict['nth'+key] = i >>         for form in forms: >>             adjuste

Re: [Tutor] List index usage: is there a more pythonesque way?

2010-04-18 Thread C M Caine
That's two new things I've learnt. I didn't realise that for loops could be used like that (with more than one... key?). Thanks, I'm changing my code even now! On 19 April 2010 00:09, Alan Gauld wrote: > > "C M Caine" wrote > >>       for i in range(len(timetable)): >>           numDict[timetab

Re: [Tutor] List index usage: is there a more pythonesque way?

2010-04-18 Thread bob gailer
On 4/18/2010 6:53 PM, C M Caine wrote: # Example data for forms and timetable: forms = ["P7", "P8", "P9", "P10", "P11", "S7", "S8", "S9", "S10", "S11", "IMA", "CAT", "FOR", "RLS", "EMPTY"] timetable = ['CAT', 'P10', 'P8', 'EMPTY', 'EMPTY', 'EMPTY', 'S10', 'S8', 'IMA', 'EMPTY', 'S7', 'S10', 'P9',

Re: [Tutor] List index usage: is there a more pythonesque way?

2010-04-18 Thread Alan Gauld
"C M Caine" wrote for i in range(len(timetable)): numDict[timetable[i]] += 1 if spaceDict['1st'+timetable[i]] == 0: spaceDict['nth'+timetable[i]] = i for index, item in enumerate(timetable): numDict[item] += 1 if spaceDict[

[Tutor] List index usage: is there a more pythonesque way?

2010-04-18 Thread C M Caine
# Example data for forms and timetable: forms = ["P7", "P8", "P9", "P10", "P11", "S7", "S8", "S9", "S10", "S11", "IMA", "CAT", "FOR", "RLS", "EMPTY"] timetable = ['CAT', 'P10', 'P8', 'EMPTY', 'EMPTY', 'EMPTY', 'S10', 'S8', 'IMA', 'EMPTY', 'S7', 'S10', 'P9', 'EMPTY', 'EMPTY', 'EMPTY', 'S7', 'EMPTY'