Extracting an undefined number of items from a list

V

vsoler

Hi everyone,

say that 'db' is a list of values
say 'i' is a list of indexes
I'd like to get a list where each item is i-th element of db.

For example:

db=[10,20,30,40,50,60,70,80,90] #undefined length
i=[3,5,7] #undefined length
then result=[30,50,70] # resulting list

how can I do this?
 
P

Patrick Maupin

Hi everyone,

say that 'db' is a list of values
say 'i' is a list of indexes
I'd like to get a list where each item is i-th element of db.

For example:

db=[10,20,30,40,50,60,70,80,90]      #undefined length
i=[3,5,7]                                         #undefined length
then result=[30,50,70]                     # resulting list

how can I do this?

With a list comprehension:
db=[10,20,30,40,50,60,70,80,90]
i=[3,5,7]
[db[x-1] for x in i]
[30, 50, 70]

Regards,
Pat
 
V

vsoler

Hi everyone,
say that 'db' is a list of values
say 'i' is a list of indexes
I'd like to get a list where each item is i-th element of db.
For example:
db=[10,20,30,40,50,60,70,80,90]      #undefined length
i=[3,5,7]                                         #undefined length
then result=[30,50,70]                     # resulting list
how can I do this?

With a list comprehension:
db=[10,20,30,40,50,60,70,80,90]
i=[3,5,7]
[db[x-1] for x in i]

[30, 50, 70]

Regards,
Pat

Thank you Patrick,

Your quick answer only tells me how much I still have to learn.
 
C

Chris Rebert

Hi everyone,

say that 'db' is a list of values
say 'i' is a list of indexes
I'd like to get a list where each item is i-th element of db.

For example:

db=[10,20,30,40,50,60,70,80,90]      #undefined length
i=[3,5,7]                                         #undefined length
then result=[30,50,70]                     # resulting list

how can I do this?

result = [db[k-1] for k in i] # your indices seem to be 1-based

See "List comprehensions":
http://docs.python.org/tutorial/datastructures.html#list-comprehensions

Cheers,
Chris
 
S

Steven D'Aprano

Hi everyone,

say that 'db' is a list of values
say 'i' is a list of indexes
I'd like to get a list where each item is i-th element of db.

For example:

db=[10,20,30,40,50,60,70,80,90] #undefined length
i=[3,5,7] #undefined length then
result=[30,50,70] # resulting list

how can I do this?


Others have given a simple solution using list comprehensions. Here's
another few ways.


# The beginner's way
result = []
for index in i: # "i" is a bad name for a list of indexes
x = db[index - 1] # shift from one-based counts to zero-based
result.append(x)


# The advanced functional way
from operator import itemgetter
indexes = map(lambda n: n-1, i) # adjust for one-based counts
result = itemgetter(*indexes)(db)



# The obfuscated, fragile way
map( itemgetter(0), sorted(
zip(db, range(1, len(db)+1)), key=lambda t: t[1] if t[1] in i else -1
)[-len(i):] )
 
P

Patrick Maupin

# The obfuscated, fragile way
map( itemgetter(0), sorted(
     zip(db, range(1, len(db)+1)), key=lambda t: t[1] if t[1] in i else -1
     )[-len(i):] )

I have to hand it to you that this might, in fact, be "the"
obfuscated, fragile way, but I gotta tell you that I'm certain there
exist other ways that are considerably less obfuscated and
considerably less fragile that I, personally would still consider to
be obfuscated and fragile...

The only thing you get points off for is the lack of a multiply or
divide by 10. The original data set is just crying out for such a
thing, and, while it might not do all that much for the obfuscation,
it would certainly help with the fragility. ;-)

Regards,
Pat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top