Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Grabbing previous iteration in a dict

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


dannywebster at googlemail

May 9, 2008, 2:10 AM

Post #1 of 8 (127 views)
Permalink
Grabbing previous iteration in a dict

Hello all,

I have a dictionary of which i'm itervalues'ing through, and i'll be
performing some logic on a particular iteration when a condition is
met with trusty .startswith('foo'). I need to grab the previous
iteration if this condition is met. I can do something with an extra
var to hold every iteration while iterating, but this is hacky and not
elegant.

Is there a mechanism whereby I can just index the dict value
subscripts like in an array? I could just rewind by 1 if so.

Cheerz

dan.
--
http://mail.python.org/mailman/listinfo/python-list


"http://phr.cx" at NOSPAM

May 9, 2008, 2:48 AM

Post #2 of 8 (115 views)
Permalink
Re: Grabbing previous iteration in a dict [In reply to]

dannywebster[at]googlemail.com writes:
> I have a dictionary of which i'm itervalues'ing through, and i'll be
> performing some logic on a particular iteration when a condition is
> met with trusty .startswith('foo'). I need to grab the previous
> iteration if this condition is met. I can do something with an extra
> var to hold every iteration while iterating, but this is hacky and not
> elegant.

You cannot rely on the elements of a dictionary being in any
particular order (dicts are internally hash tables), so the above
is almost certainly ont what you want.
--
http://mail.python.org/mailman/listinfo/python-list


dannywebster at googlemail

May 9, 2008, 7:21 AM

Post #3 of 8 (117 views)
Permalink
Re: Grabbing previous iteration in a dict [In reply to]

On May 9, 10:48 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> dannywebs...@googlemail.com writes:
> > I have a dictionary of which i'm itervalues'ing through, and i'll be
> > performing some logic on a particular iteration when a condition is
> > met with trusty .startswith('foo'). I need to grab the previous
> > iteration if this condition is met. I can do something with an extra
> > var to hold every iteration while iterating, but this is hacky and not
> > elegant.
>
> You cannot rely on the elements of a dictionary being in any
> particular order (dicts are internally hash tables), so the above
> is almost certainly ont what you want.


Hi - thanks for your reply. How about if I made the dict into a list
(of
which I have done). How would I then reference the previous item?
Can they
be indexed?
--
http://mail.python.org/mailman/listinfo/python-list


gherron at islandtraining

May 9, 2008, 7:45 AM

Post #4 of 8 (115 views)
Permalink
Re: Grabbing previous iteration in a dict [In reply to]

dannywebster[at]googlemail.com wrote:
> On May 9, 10:48 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
>
>> dannywebs...@googlemail.com writes:
>>
>>> I have a dictionary of which i'm itervalues'ing through, and i'll be
>>> performing some logic on a particular iteration when a condition is
>>> met with trusty .startswith('foo'). I need to grab the previous
>>> iteration if this condition is met. I can do something with an extra
>>> var to hold every iteration while iterating, but this is hacky and not
>>> elegant.
>>>
>> You cannot rely on the elements of a dictionary being in any
>> particular order (dicts are internally hash tables), so the above
>> is almost certainly ont what you want.
>>
>
>
> Hi - thanks for your reply. How about if I made the dict into a list
> (of
> which I have done). How would I then reference the previous item?
> Can they
> be indexed?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Yes:

listOfItems = DICT.items()
for i in range(len(listOfItems)):
k,v = listOfItems[i] # Current key,value pair
if whatever:
kPrev,vPrev = listOfItems[i-1] # Previous key,value pair

Still, since there is no proscribed order in which the items are placed
into the list, I wonder how this can be useful. However, this *does*
do what you asked.

Gary Herron

--
http://mail.python.org/mailman/listinfo/python-list


hyugaricdeau at gmail

May 9, 2008, 7:55 AM

Post #5 of 8 (116 views)
Permalink
Re: Grabbing previous iteration in a dict [In reply to]

On May 9, 5:10 am, dannywebs...@googlemail.com wrote:
> I have a dictionary of which i'm itervalues'ing through, and i'll be
> performing some logic on a particular iteration when a condition is
> met with trusty .startswith('foo'). I need to grab the previous
> iteration if this condition is met. I can do something with an extra
> var to hold every iteration while iterating, but this is hacky and not
> elegant.

Why is that so terrible?
previous = None
for key in mydict:
if key.starswith('foo') and previous is not None:
# ...do stuff...
previous = key

Doesn't seem too ugly to me.

> Is there a mechanism whereby I can just index the dict value
> subscripts like in an array? I could just rewind by 1 if so.

You can't rely on the keys in a dictionary being in any specific
order. But if you want a list of the keys you can just call
mydict.keys() which will give a copy of the dictionary's keys in a
list. Then you can iterate that and use it however you would use any
other list. Note that it's a copy though. It might help if you
better explained exactly what you need to do.
--
http://mail.python.org/mailman/listinfo/python-list


yves at zioup

May 9, 2008, 7:57 AM

Post #6 of 8 (119 views)
Permalink
Re: Grabbing previous iteration in a dict [In reply to]

dannywebster[at]googlemail.com wrote:
>> You cannot rely on the elements of a dictionary being in any
>> particular order (dicts are internally hash tables), so the above
>> is almost certainly ont what you want.
>
>
> Hi - thanks for your reply. How about if I made the dict into a list
> (of
> which I have done). How would I then reference the previous item?
> Can they
> be indexed?

Yes, I ran in a situation similar to yours, I read the content of a file,
and had to both keep the order of the lines in the original file, and create
a dictionary from the lines. So I created a class that contained both a
dictionary and a tuple containing the keys of the dictionary in the original
order. Something like this:

class mydict(object):

def __init__(self, filename):
x = [ e.strip().split() for e in file(filename) ]
self.ordered_lines = tuple([ e[0] for e in x ])
self.dictionary = dict( zip(self.ordered_lines, [ e[1:] for e in x]) )

a = mydict('/some/file')
a.ordered_lines
a.dictionary



Yves.
http://www.SollerS.ca
--
http://mail.python.org/mailman/listinfo/python-list


paul.hankin at gmail

May 9, 2008, 8:09 AM

Post #7 of 8 (113 views)
Permalink
Re: Grabbing previous iteration in a dict [In reply to]

On May 9, 10:10 am, dannywebs...@googlemail.com wrote:
> Hello all,
>
> I have a dictionary of which i'm itervalues'ing through, and i'll be
> performing some logic on a particular iteration when a condition is
> met with trusty .startswith('foo').  I need to grab the previous
> iteration if this condition is met.  I can do something with an extra
> var to hold every iteration while iterating, but this is hacky and not
> elegant.

Often when you're iterating, 'hacky' code can be lifted out into a
separate generator, keeping the details of the hacks nicely away from
your code. That's true here...

def iterprevious(seq):
"""Generate pairs of (previous element, current element)
from seq."""
last = None
for x in iter(seq):
yield last, x
last = x

Then, when you want use it...

for previous, (key, value) in iterprevious(d.iteritems()):
... In the loop, previous will either be None if we're on the
first element, otherwise (previous_key, previous_value).

--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list


dannywebster at googlemail

May 9, 2008, 8:44 AM

Post #8 of 8 (116 views)
Permalink
Re: Grabbing previous iteration in a dict [In reply to]

On May 9, 4:09 pm, Paul Hankin <paul.han...@gmail.com> wrote:
> On May 9, 10:10 am, dannywebs...@googlemail.com wrote:
>
> > Hello all,
>
> > I have a dictionary of which i'm itervalues'ing through, and i'll be
> > performing some logic on a particular iteration when a condition is
> > met with trusty .startswith('foo'). I need to grab the previous
> > iteration if this condition is met. I can do something with an extra
> > var to hold every iteration while iterating, but this is hacky and not
> > elegant.
>
> Often when you're iterating, 'hacky' code can be lifted out into a
> separate generator, keeping the details of the hacks nicely away from
> your code. That's true here...
>
> def iterprevious(seq):
> """Generate pairs of (previous element, current element)
> from seq."""
> last = None
> for x in iter(seq):
> yield last, x
> last = x
>
> Then, when you want use it...
>
> for previous, (key, value) in iterprevious(d.iteritems()):
> ... In the loop, previous will either be None if we're on the
> first element, otherwise (previous_key, previous_value).
>
> --
> Paul Hankin


Hi all - some good stuff here. Thanks for the info, I have a few
ideas
to play with.

thanks again

dan.

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.