Picking out items from a python list which have sp

2019-01-22 12:29发布

I'm sure there's a nice way to do this in Python, but I'm pretty new to the language, so forgive me if this is an easy one!

I have a list, and I'd like to pick out certain values from that list. The values I want to pick out are the ones whose indexes in the list are specified in another list.

For example:

indexes = [2, 4, 5]
main_list = [0, 1, 9, 3, 2, 6, 1, 9, 8]

the output would be:

[9, 2, 6]

(i.e., the elements with indexes 2, 4 and 5 from main_list).

I have a feeling this should be doable using something like list comprehensions, but I can't figure it out (in particular, I can't figure out how to access the index of an item when using a list comprehension).

5条回答
对你真心纯属浪费
2楼-- · 2019-01-22 12:36
t = []
for i in indexes:
    t.append(main_list[i])
return t
查看更多
姐就是有狂的资本
3楼-- · 2019-01-22 12:36
map(lambda x:main_list[x],indexes)
查看更多
倾城 Initia
4楼-- · 2019-01-22 12:53

I think Yuval A's solution is a pretty clear and simple. But if you actually want a one line list comprehension:

[e for i, e in enumerate(main_list) if i in indexes]
查看更多
叼着烟拽天下
5楼-- · 2019-01-22 12:57
[main_list[x] for x in indexes]

This will return a list of the objects, using a list comprehension.

查看更多
看我几分像从前
6楼-- · 2019-01-22 12:59

As an alternative to a list comprehension, you can use map with list.__getitem__. For large lists you should see better performance:

import random

n = 10**7
L = list(range(n))
idx = random.sample(range(n), int(n/10))

x = [L[x] for x in idx]
y = list(map(L.__getitem__, idx))

assert all(i==j for i, j in zip(x, y))

%timeit [L[x] for x in idx]            # 474 ms per loop
%timeit list(map(L.__getitem__, idx))  # 417 ms per loop

For a lazy iterator, you can just use map(L.__getitem__, idx). Note in Python 2.7, map returns a list, so there is no need to pass to list.

查看更多
登录 后发表回答