Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

python - Matematical operation between couple fo number in list

I have a even list of numbers:

lst = [2,3,4,1,6,7]

I need to subtract the second number of each couple and sum the result of all couples, below the result:

out = 3-2+1-4+7-6

Is there any elegant solution? Appreciate any hints!

question from:https://stackoverflow.com/questions/65930604/matematical-operation-between-couple-fo-number-in-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
>>> sum(lst[1::2]) - sum(lst[::2])
-1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...