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 versions you could iterate a dictionary via the
> keys() method. More recently you can do it directly - although the effect is
> the same. There is no guarantee of order only that you process every item
> once.


And of course if you want to get the items sorted you can iterate over
sorted(mydict.keys()).

-Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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 recently you can do it directly - although 
the effect is the same. There is no guarantee of order only that 
you process every item once.


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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 +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,3]
> # item is list item             (implicit, it could be written items(l) or 
> l.items())
> for n in l: print n,
> # item is (index,item) pair
> for (i,n) in enumerate(l): print (i,n),
> print
>
> d = {'a':1, 'b':2, 'c':3}
> # item is dict key              (implicit, it could be written d.keys())
> for k in d: print k,
> # item is dict value
> for v in d.values(): print v,
> # item is (key,value) pair
> for (k,v) in d.items(): print (k,v),
> print
>
> (In the last case "items()" is maybe a bit confusing.)
>
> Python lets you construct your own iterators on custom collections to iterate 
> in a different way:
> class NestedList(list):
>        def __iter__(self):
>                ll = list.__iter__(self)
>                for l in ll:
>                        for item in l:
>                                yield item
> ll = NestedList([[1,2,3], [9,8]])
> for n in ll: print n,
>
> Denis
> 
>
> vit esse estrany ☣
>
> spir.wikidot.com
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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,3]
# item is list item (implicit, it could be written items(l) or 
l.items())
for n in l: print n,
# item is (index,item) pair
for (i,n) in enumerate(l): print (i,n),
print

d = {'a':1, 'b':2, 'c':3}
# item is dict key  (implicit, it could be written d.keys())
for k in d: print k,
# item is dict value
for v in d.values(): print v,
# item is (key,value) pair
for (k,v) in d.items(): print (k,v),
print

(In the last case "items()" is maybe a bit confusing.)

Python lets you construct your own iterators on custom collections to iterate 
in a different way:
class NestedList(list):
def __iter__(self):
ll = list.__iter__(self)
for l in ll:
for item in l:
yield item
ll = NestedList([[1,2,3], [9,8]])
for n in ll: print n,

Denis


vit esse estrany ☣

spir.wikidot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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 write it like:

for tup in enumerate(sequence):
 index = tup[0]
 value = tup[1]
 # process stufff here
OR:
for tup in enumerate(sequence):
 index,value  = tup
 # process stufff here

Which becomes
for index, value in enumerate(sequence):
 # process stufff here

HTH,

Alan G.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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:
>>             adjustedSpaceDict[form] = spaceDict['nth'+form] - \
>>                                       spaceDict['1st'+form]
>>         return (numDict, adjustedSpaceDict)


Oh yeah, I think I pulled up an old version of the source file by
accident, I've fixed that bug already.
The correct code is:

# stuff...
             if spaceDict['1st'+key] == 0:
                 spaceDict['1st'+key] = i
             else:
                 spaceDict['nth'+key] = i
         for form in forms:
             adjustedSpaceDict[form] = spaceDict['nth'+form] - \
                                       spaceDict['1st'+form]
         return (numDict, adjustedSpaceDict)

I don't think I'll use defaultdict here, though thanks for pointing it
out to me, that's the first time I've heard of it; I'm trying to keep
the number of outside modules to a minimum as this is an assessed
piece of work.

Thanks, Bob.


On 19 April 2010 00:34, bob gailer  wrote:
> 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', 'EMPTY', 'EMPTY', 'EMPTY',
>> 'S7', 'EMPTY', 'EMPTY', 'RLS', 'FOR', 'EMPTY', 'EMPTY', 'EMPTY', 'S9',
>> 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY',
>> 'EMPTY', 'EMPTY', 'S8', 'IMA', 'S11', 'P8', 'EMPTY', 'IMA', 'EMPTY',
>> 'EMPTY', 'S11', 'S11', 'EMPTY', 'EMPTY', 'EMPTY', 'P7', 'S9', 'P11',
>> 'P11', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY',
>> 'EMPTY', 'EMPTY', 'P9', 'EMPTY', 'EMPTY', 'P8', 'FOR', 'S10', 'S11',
>> 'S7', 'P7', 'EMPTY', 'EMPTY', 'IMA', 'EMPTY', 'S9', 'P10', 'P11',
>> 'CAT', 'S8', 'P9', 'RLS']
>>
>> def analyseTimetable():
>>         "Find number of and spaces between each form."
>>
>
> Consider using defaultdict in the collections module. The first time a key
> is referenced it is automatically added with a specified value.
>
>
> import collections
>         numDict = collections.defaultdict(0)
>         spaceDict = collections.defaultdict(0)
>
>>         adjustedSpaceDict = {}
>>
>
> If you use enumerate then you can replace timetable[i] with key
>>
>>         for i, key in enumerate(timetable):
>>             numDict[key] += 1
>>
>
> 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:
>>             adjustedSpaceDict[form] = spaceDict['nth'+form] - \
>>                                       spaceDict['1st'+form]
>>         return (numDict, adjustedSpaceDict)
>>
>> # End example
>>
>> This function works fine, but I think that using range(len(timetable))
>> is clumsy. On the other hand, I need the indexes to track the number
>> of spaces between instances of each form. Without using another loop
>> (so not using list.index), is there a way of getting the index of the
>> list entries?
>
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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[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['1st'+item] == 0:
>               spaceDict['nth'+item] = i
>
>>
>> This function works fine, but I think that using range(len(timetable))
>> is clumsy. On the other hand, I need the indexes to track the number
>
> enumerate() is your friend.
>
>
> HTH,
>
> Alan G
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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', 'EMPTY', 'EMPTY', 'EMPTY',
'S7', 'EMPTY', 'EMPTY', 'RLS', 'FOR', 'EMPTY', 'EMPTY', 'EMPTY', 'S9',
'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY',
'EMPTY', 'EMPTY', 'S8', 'IMA', 'S11', 'P8', 'EMPTY', 'IMA', 'EMPTY',
'EMPTY', 'S11', 'S11', 'EMPTY', 'EMPTY', 'EMPTY', 'P7', 'S9', 'P11',
'P11', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY',
'EMPTY', 'EMPTY', 'P9', 'EMPTY', 'EMPTY', 'P8', 'FOR', 'S10', 'S11',
'S7', 'P7', 'EMPTY', 'EMPTY', 'IMA', 'EMPTY', 'S9', 'P10', 'P11',
'CAT', 'S8', 'P9', 'RLS']

def analyseTimetable():
 "Find number of and spaces between each form."
   


Consider using defaultdict in the collections module. The first time a 
key is referenced it is automatically added with a specified value.





import collections
 numDict = collections.defaultdict(0)
 spaceDict = collections.defaultdict(0)


 adjustedSpaceDict = {}
   


If you use enumerate then you can replace timetable[i] with key

 for i, key in enumerate(timetable):
 numDict[key] += 1
   


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:
 adjustedSpaceDict[form] = spaceDict['nth'+form] - \
   spaceDict['1st'+form]
 return (numDict, adjustedSpaceDict)

# End example

This function works fine, but I think that using range(len(timetable))
is clumsy. On the other hand, I need the indexes to track the number
of spaces between instances of each form. Without using another loop
(so not using list.index), is there a way of getting the index of the
list entries?



--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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['1st'+item] == 0:
   spaceDict['nth'+item] = i


This function works fine, but I think that using range(len(timetable))
is clumsy. On the other hand, I need the indexes to track the number


enumerate() is your friend.


HTH,

Alan G

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor