A couple of quick questions

cbarker at jps.net cbarker at jps.net
Mon May 24 15:16:24 EDT 1999


Hi,

I've been using Python for a few months now, and I have a couple of
things I havn't figured out how to do, that would be handy

1) loop a known number of times, without creating a list of indexes:

for i in range(N):
	# do something

works just fine, unless N is very large. if it's large, it wastes
memory, if it's very large it can be impossible!

I know I can use a while:
while 1:
	i = i + 1
	if i > N: break
	# do something

But that is not very elegant!

2) Is there a "case" construct?

I can use if...elif...elif...elif...else, but it's once, again, not
very elegant.

3) lists of indexes:

let's say I have a list of the indexes of another list that I want. Is
there a way to return the corresponding elements without a loop?

example:

list = [1,2,3,4,5,6,7,8,9,10,11]
indexes = [3,5,9]
new_list = []
for i in indexes:
	new_list.append(list[i])

This just isn't elegant (or fast!). What I want to be able to do is
something like:

new_list = list[indexes]

Is there a way to do this?

NOTE: I'm pretty sure PERL can do this, and I know MATLAB can, and it
can be very very handy!

Please sent a note to me directly if you can, as well as the newsgroup,
because I'm stuck with dejanews at work and it's kind of a pain!

-chris

cbarker at jps.net


--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---




More information about the Python-list mailing list