Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

please help shrink this each_with_index() implementation

0 views
Skip to first unread message

Phlip

unread,
Jan 5, 2010, 2:58:36 PM1/5/10
to
Hypo Nt:

def each_with_index(seq):
index = 0
result = []

for item in seq:
result.append([item, index])
index += 1

return result

My Pythonic sequencing skills are obviously feeble. Can anything think
of a way to write that in fewer lines?

--
Phlip
http://c2.com/cgi/wiki?MoreliaViridis

akean

unread,
Jan 5, 2010, 3:17:11 PM1/5/10
to

y = [[seq[i],i] for i in range(len(seq))] gives the same result.
The index is accessible without assigning it to another variable.
--
Anita

Marco Nawijn

unread,
Jan 5, 2010, 3:20:58 PM1/5/10
to

You could use the build-in function enumerate inside a list
comprehension.

>>> seq = range(5)
>>> [ (i,s) for i,s in enumerate(seq) ]
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]

This will reduce the function to a one-liner.

Regards,

Marco

Carsten Haese

unread,
Jan 5, 2010, 3:22:51 PM1/5/10
to pytho...@python.org
Phlip wrote:
> Hypo Nt:
>
> def each_with_index(seq):
> index = 0
> result = []
>
> for item in seq:
> result.append([item, index])
> index += 1
>
> return result
>
> My Pythonic sequencing skills are obviously feeble. Can anything think
> of a way to write that in fewer lines?

Couldn't you just use the built-in enumerate() to replace the whole thing?

--
Carsten Haese
http://informixdb.sourceforge.net

Phlip

unread,
Jan 5, 2010, 3:36:07 PM1/5/10
to
> > My Pythonic sequencing skills are obviously feeble. Can anything think
> > of a way to write that in fewer lines?

Thanks, all!

> Couldn't you just use the built-in enumerate() to replace the whole thing?

Because that would involve, like, reading an entire Python book just
to locate that method?

GMAB I'm too busy writing high-end Django via TDD & BDD! C-:

--
Phlip
http://zeekland.zeroplayer.com/Pigleg_Too/1

Antoine Pitrou

unread,
Jan 5, 2010, 4:10:00 PM1/5/10
to pytho...@python.org

>> Couldn't you just use the built-in enumerate() to replace the whole
>> thing?
>
> Because that would involve, like, reading an entire Python book just to
> locate that method?

Actually, no. It just involves reading one of the most important pages in
the documentation, the page which describes the built-in functions:

http://docs.python.org/library/functions.html

Don't forget that the Python documentation is rich and structured.
And good luck.


Phlip

unread,
Jan 5, 2010, 5:40:49 PM1/5/10
to
On Jan 5, 1:10 pm, Antoine Pitrou <solip...@pitrou.net> wrote:

> http://docs.python.org/library/functions.html
>
> Don't forget that the Python documentation is rich and structured.
> And good luck.

Does it say how to convert a string containing either an integer
representation, or something alphabetic, into an integer, or a zero,
in like 1 method call? (No except: ?)

Nothing personal, but I'm finding the super-hard stuff very facile &
tractable, and the easy stuff absurdly hard around here...

Steven D'Aprano

unread,
Jan 5, 2010, 6:26:26 PM1/5/10
to
On Tue, 05 Jan 2010 14:40:49 -0800, Phlip wrote:

> On Jan 5, 1:10 pm, Antoine Pitrou <solip...@pitrou.net> wrote:
>
>> http://docs.python.org/library/functions.html
>>
>> Don't forget that the Python documentation is rich and structured. And
>> good luck.
>
> Does it say how to convert a string containing either an integer
> representation, or something alphabetic, into an integer, or a zero, in
> like 1 method call? (No except: ?)

If you mean something like this:

>>> int('153')
153

then yes it does. But if you mean something like this:


>>> some_mysterious_function('one hundred and fifty-three')
153

then no, it doesn't.

> Nothing personal, but I'm finding the super-hard stuff very facile &
> tractable, and the easy stuff absurdly hard around here...


Then perhaps you should work through the tutorial to learn the basics.

--
Steven

Phlip

unread,
Jan 5, 2010, 6:30:09 PM1/5/10
to
> > Does it say how to convert a string containing either an integer
> > representation, or something alphabetic, into an integer, or a zero, in
> > like 1 method call? (No except: ?)
>
> If you mean something like this:
>
> >>> int('153')
>
> 153

The point: int('') or int('something') both throw an error. In
general, this is hand-holding, but in specific I don't think the "rich
and structured" documentation will cover how to beat a 0 out of it in
less than 3 lines. So I will persist in my idiotic questions here!

> Then perhaps you should work through the tutorial to learn the basics.

They will tell me how to use except: (which is a good example why a
program should not use exceptions for its normal control flow if at
all possible).

Please, please, please save your newbie admonitions for those who
qualify!

Antoine Pitrou

unread,
Jan 5, 2010, 9:28:08 PM1/5/10
to pytho...@python.org

> The point: int('') or int('something') both throw an error. In general,
> this is hand-holding, but in specific I don't think the "rich and
> structured" documentation will cover how to beat a 0 out of it in less
> than 3 lines.

Because it's a bad idea to do so and Python doesn't encourage such sloppy
behaviour. If you like it, though, you might prefer PHP.

By the way: error reporting is definitely *not* hand-holding. It is
simply not good to let errors pass silently by default. Again, if you
think the contrary, PHP is your friend ;-)

MRAB

unread,
Jan 5, 2010, 9:59:50 PM1/5/10
to pytho...@python.org
Antoine Pitrou wrote:
>> The point: int('') or int('something') both throw an error. In general,
>> this is hand-holding, but in specific I don't think the "rich and
>> structured" documentation will cover how to beat a 0 out of it in less
>> than 3 lines.
>
> Because it's a bad idea to do so and Python doesn't encourage such sloppy
> behaviour. If you like it, though, you might prefer PHP.
>
> By the way: error reporting is definitely *not* hand-holding. It is
> simply not good to let errors pass silently by default. Again, if you
> think the contrary, PHP is your friend ;-)
>
Someone wrote about a port of a Perl script to Python. The script would
parse the output from a program it would call, but the Python script
sometimes raised a ValueError when it tried to convert a certain field
to a number. It turned out that sometimes the field would contain
"ERROR" instead of a number. The Perl script had been silently
'converting' any such "ERROR" to 0!

alex23

unread,
Jan 5, 2010, 10:46:01 PM1/5/10
to
Phlip <phlip2...@gmail.com> wrote:
> They will tell me how to use except: (which is a good example why a
> program should not use exceptions for its normal control flow if at
> all possible).

Really? Magic functions that coerce and eat errors are a better coding
technique than exceptions and explicit handling?

What kool-aid have you been drinking?

Steven D'Aprano

unread,
Jan 5, 2010, 11:52:12 PM1/5/10
to
On Tue, 05 Jan 2010 15:30:09 -0800, Phlip wrote:

>> > Does it say how to convert a string containing either an integer
>> > representation, or something alphabetic, into an integer, or a zero,
>> > in like 1 method call? (No except: ?)
>>
>> If you mean something like this:
>>
>> >>> int('153')
>>
>> 153
>
> The point: int('') or int('something') both throw an error.

Well, that's a bug. Obviously they should return 42.

Or is that 23? I forget.

But seriously... of course they do. What did you expect them to do?

Any Python function will raise an exception if you pass invalid data to
it. So the solutions are, don't pass invalid data, or wrap the function
in a try block. If you don't want to use try, then make sure that your
data is good.

> In general,
> this is hand-holding, but in specific I don't think the "rich and
> structured" documentation will cover how to beat a 0 out of it in less
> than 3 lines. So I will persist in my idiotic questions here!
>
>> Then perhaps you should work through the tutorial to learn the basics.
>
> They will tell me how to use except: (which is a good example why a
> program should not use exceptions for its normal control flow if at all
> possible).

Huh? So because YOU don't know how to use except, programs shouldn't use
exceptions for flow control? I reject that, and I will continue using
exceptions for flow control when I think it is appropriate.


> Please, please, please save your newbie admonitions for those who
> qualify!

Oh please, get off your high horse. You're asking newbie questions, no
matter how many years of using Python you may or may not have, your
experience is obviously low. Stick around and you might learn something,
but if you bite every time somebody tries to teach you, you'll soon run
out of people willing to help you.

--
Steven

alex23

unread,
Jan 6, 2010, 1:48:59 AM1/6/10
to
Steven D'Aprano <ste...@REMOVE.THIS.cybersource.com.au> wrote:
> Stick around and you might learn something,
> but if you bite every time somebody tries to teach you, you'll soon run
> out of people willing to help you.

The ongoing crowdsourced development by this group of "Victor
Subervi's" project would seem to indicate that this isn't the case. Or
that if it is, that it will be a good handful of months at the very
soonest before it becomes true.

Nobody

unread,
Jan 6, 2010, 12:59:40 PM1/6/10
to
On Tue, 05 Jan 2010 12:20:58 -0800, Marco Nawijn wrote:

> You could use the build-in function enumerate inside a list
> comprehension.
>
>>>> seq = range(5)
>>>> [ (i,s) for i,s in enumerate(seq) ]
> [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]

Just use list(), i.e. "list(enumerate(seq))".

Carl Banks

unread,
Jan 6, 2010, 1:08:21 PM1/6/10
to
On Jan 5, 2:40 pm, Phlip <phlip2...@gmail.com> wrote:
> On Jan 5, 1:10 pm, Antoine Pitrou <solip...@pitrou.net> wrote:
>
> >http://docs.python.org/library/functions.html
>
> > Don't forget that the Python documentation is rich and structured.
> > And good luck.
>
> Does it say how to convert a string containing either an integer
> representation, or something alphabetic, into an integer, or a zero,
> in like 1 method call?


No, as he said, it only describes the most important functions.


Carl Banks

Nobody

unread,
Jan 6, 2010, 1:10:36 PM1/6/10
to

Maybe he's doing it for a living?

Contract programming seems to work on the basis that the ultimate
requirement is for the client to hand over the money. If, at that point,
the program is still full of bugs, you get to charge extra for "upgrades".

Writing robust software from the outset puts you at a competitive
disadvantage to those who understand how the system works.

Phlip

unread,
Jan 6, 2010, 3:12:08 PM1/6/10
to
Nobody wrote:
> On Tue, 05 Jan 2010 19:46:01 -0800, alex23 wrote:
>
>>> They will tell me how to use except: (which is a good example why a
>>> program should not use exceptions for its normal control flow if at
>>> all possible).
>> Really? Magic functions that coerce and eat errors are a better coding
>> technique than exceptions and explicit handling?
>>
>> What kool-aid have you been drinking?
>
> Maybe he's doing it for a living?
>
> Contract programming seems to work on the basis that the ultimate
> requirement is for the client to hand over the money. If, at that point,
> the program is still full of bugs, you get to charge extra for "upgrades".

Uh, no, right now I'm working with the long-term goal of creating useful modules
that sustain us equitably.

> Writing robust software from the outset puts you at a competitive
> disadvantage to those who understand how the system works.

And I, not my language, should pick and chose how to be rigorous. The language
should not make the decision for me.

Steven D'Aprano

unread,
Jan 6, 2010, 4:20:40 PM1/6/10
to
On Wed, 06 Jan 2010 12:12:08 -0800, Phlip wrote:

> And I, not my language, should pick and chose how to be rigorous. The
> language should not make the decision for me.

All languages make that decision for you by making some thing possible
and other things not. The language designer, not the programmer, decides
what syntactic features to allow and disallow, what programming styles to
include, and what functionality to provide as primitives.


--
Steven

Antoine Pitrou

unread,
Jan 6, 2010, 6:45:31 PM1/6/10
to pytho...@python.org
Le Wed, 06 Jan 2010 12:12:08 -0800, Phlip a écrit :
>
> And I, not my language, should pick and chose how to be rigorous. The
> language should not make the decision for me.

And that's why there is the "try: ... except: ..." construct.
Your rant is getting tiring.


alex23

unread,
Jan 6, 2010, 7:33:58 PM1/6/10
to
Phlip <phlip2...@gmail.com> wrote:
> And I, not my language, should pick and chose how to be rigorous. The language
> should not make the decision for me.

Since you seem unwilling to put the minimal effort into producing the
support code you'd need to work with Python the way you want, perhaps
Perl might be more to your liking?

Carl Banks

unread,
Jan 6, 2010, 7:52:03 PM1/6/10
to
On Jan 6, 12:12 pm, Phlip <phlip2...@gmail.com> wrote:

> Nobody wrote:
> > Writing robust software from the outset puts you at a competitive
> > disadvantage to those who understand how the system works.
>
> And I, not my language, should pick and chose how to be rigorous. The language
> should not make the decision for me.

Oh well, your language does. Deal with it, or pick another language.


Carl Banks

P.S. Actually, Python has ways to request no expection when it's
actually useful, e.g. the get method of dicts, but not when it's
ridiculous and stupid like int() returning zero on error.

Roel Schroeven

unread,
Jan 7, 2010, 8:37:07 AM1/7/10
to
Phlip schreef:

> Nobody wrote:
>> Writing robust software from the outset puts you at a competitive
>> disadvantage to those who understand how the system works.
>
> And I, not my language, should pick and chose how to be rigorous. The language
> should not make the decision for me.

You can always easily make your program less rigorous than the language,
but the reverse is generally very difficult. So a rigorous language
gives you the choice, why a non-rigorous language does not.

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven

0 new messages