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. 2 1049
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
On May 9, 4:09 pm, Paul Hankin <paul.han...@gmail.comwrote:
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Robin Cull |
last post by:
Imagine I have a dict looking something like this:
myDict = {"key 1": , "key
2": , "key 3": ,
"key 4": }
That is, a set of keys which have a variable length list of associated
values after...
|
by: Raymond Hettinger |
last post by:
Here is a discussion draft of a potential PEP.
The ideas grew out of the discussion on pep-284.
Comments are invited. Dart throwing is optional.
Raymond Hettinger
...
|
by: Raymond Hettinger |
last post by:
Please comment on the new PEP for reverse iteration methods.
Basically, the idea looks like this:
for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0
<do something with i>
The...
|
by: Raymond Hettinger |
last post by:
Based on your extensive feedback, PEP 322 has been completely revised.
The response was strongly positive, but almost everyone preferred
having a function instead of multiple object methods. The...
|
by: flacco |
last post by:
is there a way to iterate over the *values* in a list/dict/whatever,
regardless of whether it's a list, dict, or whatever? ie, the iteration
code will not know beforehand what kind of container...
| |
by: tkpmep |
last post by:
I have list of lists of the following form
L=, , , ]
I want to aggregate these lists, i.e. to reduce L to
L=, ] #500 = 100+400, 200=300-100
Here's how I have done it:
L.sort()
for i in...
|
by: Bengt Richter |
last post by:
Has anyone found a way besides not deriving from dict?
Shouldn't there be a way?
TIA
(need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-)
I guess I can just document...
|
by: robert |
last post by:
In very rare cases a program crashes (hard to reproduce) :
* several threads work on an object tree with dict's etc. in it. Items
are added, deleted, iteration over .keys() ... ). The threads are...
|
by: MTD |
last post by:
Hello all,
I've been messing about for fun creating a trial division factorizing
function and I'm naturally interested in optimising it as much as
possible.
I've been told that iteration in...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |