在 python 中使用 map 处理列表时获取项的索引

在使用 map ()处理列表时,我希望在 lambda 中访问该项的索引。我该怎么做?

比如说

ranked_users = ['jon','bob','jane','alice','chris']
user_details = map(lambda x: {'name':x, 'rank':?}, ranked_users)

在上面的例子中,我如何得到每个用户的排名?

114061 次浏览

Use enumerate:

In [3]: user_details = [{'name':x, 'rank':i} for i,x in enumerate(ranked_users)]


In [4]: user_details
Out[4]:
[{'name': 'jon', 'rank': 0},
{'name': 'bob', 'rank': 1},
{'name': 'jane', 'rank': 2},
{'name': 'alice', 'rank': 3},
{'name': 'chris', 'rank': 4}]

PS. My first answer was

user_details = map(lambda (i,x): {'name':x, 'rank':i}, enumerate(ranked_users))

I'd strongly recommend using a list comprehension or generator expression over map and lambda whenever possible. List comprehensions are more readable, and tend to be faster to boot.

Alternatively you could use a list comprehension rather than map() and lambda.

ranked_users = ['jon','bob','jane','alice','chris']
user_details = [{'name' : x, 'rank' : ranked_users.index(x)} for x in ranked_users]

Output:

[{'name': 'jon', 'rank': 0}, {'name': 'bob', 'rank': 1}, {'name': 'jane', 'rank': 2}, {'name': 'alice', 'rank': 3}, {'name': 'chris', 'rank': 4}]

List comprehensions are very powerful and are also faster than a combination of map and lambda.

In my opinion the question was about map function and preferred answer is partly correct due to syntax error caused by putting tuple argument to lambda lambda (i,x)

idea of enumerate is nice and proper solution would be:

map(lambda x: {'name':x[1], 'rank':x[0]}, enumerate(ranked_users))

and some timing to compare speed with comprehension:

def with_map():
ranked_users = range(10 ** 6)
list(map(lambda x: {'name': x[1], 'rank': x[0]}, enumerate(ranked_users)))


def by_comprehension():
ranked_users = range(10 ** 6)
[{'name': x, 'rank': i} for i, x in enumerate(ranked_users)]


from timeit import timeit
time_with_map = timeit(with_map, number=10)
time_with_comprehension = timeit(by_comprehension, number=10)


print('list comprehension is about %.2f x faster than map in this test case' % (time_with_map/time_with_comprehension))

test result: list comprehension is about 1.31 x faster than map in this test case

Actually here is a more elegant, verbose solution than using an enumerate tuple in the map (because of tuple indexing). Map can take more iterables as arguments so let's use it.

map(lambda user, user_id: (user_id, user), ranked_users, range(ranked_users.__len__()))