Can I use index information inside the map function?

雨燕双飞 提交于 2019-11-30 04:48:11

Use the enumerate() function to add indices:

map(function, enumerate(a))

Your function will be passed a tuple, with (index, value). In Python 2, you can specify that Python unpack the tuple for you in the function signature:

map(lambda (i, el): i * el, enumerate(a))

Note the (i, el) tuple in the lambda argument specification. You can do the same in a def statement:

def mapfunction((i, el)):
    return i * el

map(mapfunction, enumerate(a))

To make way for other function signature features such as annotations, tuple unpacking in function arguments has been removed from Python 3.

Demo:

>>> a = [1, 3, 5, 6, 8]
>>> def mapfunction((i, el)):
...     return i * el
...
>>> map(lambda (i, el): i * el, enumerate(a))
[0, 3, 10, 18, 32]
>>> map(mapfunction, enumerate(a))
[0, 3, 10, 18, 32]

You can use enumerate():

a = [1, 3, 5, 6, 8]

answer = map(lambda (idx, value): idx*value, enumerate(a))
print(answer)

Output

[0, 3, 10, 18, 32]

To extend Martijn Pieters' excellent answer, you could also use list comprehensions in combination with enumerate:

>>> a = [1, 3, 5, 6, 8]
>>> [i * v for i, v in enumerate(a)]
[0, 3, 10, 18, 32]

or

[mapfunction(i, v) for i, v in enumerate(a)]

I feel list comprehensions are often more readable than map/lambda constructs. When using a named mapping function that accepts the (i, v) tuple directly, map probably wins though.

To make compatible with Python3

def map_fn(data):
    idx, el = data
    return idx*el

my_list = [2,3,5,7]

print(list(map(map_fn, list(enumerate(my_list)))))
# Python3
map(lambda args: print(args[0],args[1]) ,enumerate(a_list))
# use arg list, just get the result with index

# Python2
map(lambda index,content: print(index,content) ,enumerate(a_list))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!