Writing a function that alternates plus and minus signs between list indices

后端 未结 7 821
既然无缘
既然无缘 2020-12-10 07:40

In a homework set I\'m working on, I\'ve come across the following question, which I am having trouble answering in a Python-3 function:

\"Write a fun

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-10 08:11

    Here is one way using operator module:

    In [21]: from operator import pos, neg
    
    In [23]: ops = (pos, neg)
    
    In [24]: sum(ops[ind%2](value) for ind, value in enumerate(lst))
    Out[24]: -2
    

提交回复
热议问题