On 12 Dec 2005 08:34:37 GMT, Antoon Pardon <[EMAIL PROTECTED]> wrote:
>Op 2005-12-10, Devan L schreef <[EMAIL PROTECTED]>: >> >> Antoon Pardon wrote: >>> On 2005-12-10, Duncan Booth <[EMAIL PROTECTED]> wrote: >> [snip] >>> >> I also think that other functions could benefit. For instance suppose >>> >> you want to iterate over every second element in a list. Sure you >>> >> can use an extended slice or use some kind of while. But why not >>> >> extend enumerate to include an optional slice parameter, so you could >>> >> do it as follows: >>> >> >>> >> for el in enumerate(lst,::2) If you are willing to use square brackets, you can spell it >>> for el in enoomerate[lst, ::2]: print el, (see below ;-) >>> > >>> > 'Why not'? Because it makes for a more complicated interface for something >>> > you can already do quite easily. >>> >>> Do you think so? This IMO should provide (0,lst[0]), (2,lst[2]), >>> (4,lst[4]) ... >>> >>> I haven't found a way to do this easily. Except for something like: >>> >>> start = 0: >>> while start < len(lst): >>> yield start, lst[start] >>> start += 2 >>> >>> But if you accept this, then there was no need for enumerate in the >>> first place. So eager to learn something new, how do you do this >>> quite easily? >> >>>>> lst = ['ham','eggs','bacon','spam','foo','bar','baz'] >>>>> list(enumerate(lst))[::2] >> [(0, 'ham'), (2, 'bacon'), (4, 'foo'), (6, 'baz')] > >It is not about what is needed, but about convenience. > >Now let me see, in order to just iterate over the even elements >of a list with the index of the element, you turned an iterator >into a list, which you use to create an other list which you >will finaly iterate over. > >If this is the proposed answer, I wonder why iterators were introduced >in the first place. I thought iterator were went to avoid the need >to construct and copy list when all you want is iterate and when >I ask how to get a specific iterator you come with a construct that >makes rather heavily use of list constructions. > Just for you ;-) >>> import itertools >>> class enoomerate(object): ... def __getitem__(self, seq): ... if isinstance(seq, tuple): ... seq, slc = seq ... else: ... slc = slice(None) ... if not isinstance(slc, slice): slc = slice(None, slc) ... return itertools.islice(enumerate(seq), slc.start or 0, slc.stop, slc.step or 1) ... >>> enoomerate = enoomerate() >>> >>> import string >>> lst = list(string.ascii_lowercase) # legit list, though could use the >>> string >>> for el in enoomerate[lst, ::2]: print el, ... (0, 'a') (2, 'c') (4, 'e') (6, 'g') (8, 'i') (10, 'k') (12, 'm') (14, 'o') (16, 'q') (18, 's') ( 20, 'u') (22, 'w') (24, 'y') >>> for el in enoomerate[lst, 3::3]: print el, ... (3, 'd') (6, 'g') (9, 'j') (12, 'm') (15, 'p') (18, 's') (21, 'v') (24, 'y') >>> for el in enoomerate[lst, 3]: print el, ... (0, 'a') (1, 'b') (2, 'c') >>> for el in enoomerate[lst, 3:6]: print el, ... (3, 'd') (4, 'e') (5, 'f') >>> for el in enoomerate[lst, 3:6:2]: print el, ... (3, 'd') (5, 'f') Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list