473,487 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

iterators

Hi.

Python support forward iterators, but what about bidirectional and
random access ones?
Regards Manlio Perillo
Jul 18 '05 #1
3 1690
Manlio Perillo wrote:
Python support forward iterators, but what about bidirectional and
random access ones?


'random access iterators' are called 'Sequence' types in Python
(http://www.python.org/doc/2.3.3/lib/typesseq.html). You can create your own
sequence types by implementing some special methods
(http://www.python.org/doc/2.3.3/ref/...ce-types.html).

Daniel

Jul 18 '05 #2
gsm
Not too sure but I suppose you can design class iterators and you decide how
they behave.

I normally just add a __iter__ method and use yield

class IteratorObject:

def __init__(self, data):

self.data = data
self.__items = len(data)

def __iter__(self):

index = 0

while index < self.__items:

result = blah blah i.e do what ever you want here
#eg return a random item from list
yield result
index += 1

not sure if this helps
"Manlio Perillo" <NO******************@libero.it> wrote in message
news:o9********************************@4ax.com...
Hi.

Python support forward iterators, but what about bidirectional and
random access ones?
Regards Manlio Perillo

Jul 18 '05 #3

"Manlio Perillo" <NO******************@libero.it> wrote in message
news:o9********************************@4ax.com...
Hi.

Python support forward iterators,
Actually, it supports general linear iteration, which is to say,
one-at-a-time processing of every item in any collection, however
structured, in whatever order. Structured collections usually have special
orders like forward and backwards for sequences, alphabetic for string
collections, pre-order and post-order for trees, and also in-order for
binary trees. Support has three aspects: a definition of 'iterator-ness'
(having __iter__ and next methods -- the 'iterator protocol') that users
can implement, implementation of the protocol for all appropriate builtin
collection types as well as provision of two special interator types that
serve as return values for iter() and generator functions, and use of the
protocol by for statements.

< but what about bidirectional

List .push and .pop can be thought of as a type of bidirectional access.
More generally, one can augment .next with a .prev method, and people have.
This is specifically appropriate for a doubly-linked list, actual or
virtual. If a collection can be randomly accessed (see below), then
bidirectional access is easily accomplished with a cursor that is
incremented and decremented, but there is usually no reason to limit index
changes to +-1. For a multidimensional array, one could define a
corresponding multidirectional cursor.
random access ones?


See __getitem__ methods and index (object[]) syntax, which apply to both
sequences and associations. A random access cursor is a name bound to an
approprite index object or a collection object containing such.

Terry J. Reedy

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2189
by: Steven Bethard | last post by:
So, as I understand it, in Python 3000, zip will basically be replaced with izip, meaning that instead of returning a list, it will return an iterator. This is great for situations like: zip(*)...
18
2270
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much...
1
1874
by: Marcin Kaliciñski | last post by:
template<class RanAccIt> void some_algorithm(RanAccIt begin, RanAccIt end) { // this algorithm involves calling std::lexicographical_compare // on range [begin, end), and on reverse of this range...
3
2897
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
8
1981
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a...
24
3910
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
14
2304
by: Jiri Kripac | last post by:
Languages such as Simula 67 contain a general concept of coroutines that allow the execution of a method to be suspended without rolling back the stack and then later resumed at the same place as...
2
2326
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
90
3360
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
18
2089
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
0
7106
marktang
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,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7181
jinu1996
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...
1
6846
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...
0
7349
tracyyun
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...
0
5442
agi2029
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,...
1
4874
isladogs
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...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.