Getting index of item while processing a list using map in python

后端 未结 4 1378
刺人心
刺人心 2020-12-14 14:39

While processing a list using map(), I want to access index of the item while inside lambda. How can I do that?

For example

ranked_users = [\'jon\',\         


        
4条回答
  •  醉梦人生
    2020-12-14 15:01

    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__()))
    

提交回复
热议问题