K
kj
Switching from Perl here, and having a hard time letting go...
Suppose I have an "array" foo, and that I'm interested in the 4th, 8th,
second, and last element in that array. In Perl I could write:
my @wanted = @foo[3, 7, 1, -1];
I was a bit surprised when I got this in Python:
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers
Granted, Perl's syntax is often obscure and hard-to-read, but in
this particular case I find it quite transparent and unproblematic,
and the fictional "pythonized" form above even more so.
The best I've been able to come up with in Python are the somewhat
Perl-like-in-its-obscurity:
or the clearer but unaccountably sesquipedalian
Are these the most idiomatically pythonic forms? Or am I missing
something better?
TIA!
kynn
Suppose I have an "array" foo, and that I'm interested in the 4th, 8th,
second, and last element in that array. In Perl I could write:
my @wanted = @foo[3, 7, 1, -1];
I was a bit surprised when I got this in Python:
Traceback (most recent call last):wanted = foo[3, 7, 1, -1]
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers
Granted, Perl's syntax is often obscure and hard-to-read, but in
this particular case I find it quite transparent and unproblematic,
and the fictional "pythonized" form above even more so.
The best I've been able to come up with in Python are the somewhat
Perl-like-in-its-obscurity:
or the clearer but unaccountably sesquipedalian
wanted = [foo for i in 3, 7, 1, -1]
wanted = [foo[3], foo[7], foo[7], foo[-1]]
Are these the most idiomatically pythonic forms? Or am I missing
something better?
TIA!
kynn