简体   繁体   English

从下一个元素减去每个元素的最佳方法是什么

[英]what's the best way to subtract each element from the next element

I have very long accumulated list, and i want to subtract each element from the next element. 我有很长的累积列表,我想从下一个元素中减去每个元素。 So what's the best way? 那么最好的方法是什么? "The best in terms of readability and speed " “在可读性和速度方面最好”

Ex: the list is: 例如:清单为:

res = [0, 1, 4, 6, 7]  

And the result should be : output=[1, 3, 2, 1] 结果应为: output=[1, 3, 2, 1]
To clarify, the last element of the output came from output[-1]= res[-1] - res[-2] 为了明确起见,输出的最后一个元素来自output[-1]= res[-1] - res[-2]

So I've tried [xy for x, y in zip (res[::-1], res[::-1][1:])][::-1] 因此,我尝试了[xy for x, y in zip (res[::-1], res[::-1][1:])][::-1]

It's so fast, but it's complicated and not readable "I think i will not remember what i did in the next couple of weeks" 它是如此之快,但却是复杂且难以理解的:“我想我将不记得接下来几周的工作”

PS I am reversing the list using res[::-1] PS我正在使用res[::-1]反转列表

No need to reverse; 无需反转; just zip the list up into pairs: 只需将列表压缩成对:

>>> [i - j for i, j in zip(res[1:], res[:-1])]
[1, 3, 2, 1]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从python(numpy)数组中的每个元素创建对象的最佳方法是什么? - What is the best way to create an object from each element in a python (numpy) array? 以某种方式将列表中的每个元素相乘 - 每个元素与下一个相乘并将值存储在表中 - multiplying each element in a list in a way that- each element multiply with next and stores the value in a table 如何用numpy中的另一个ndarray的每个元素减去ndarray中的每个元素 - how to subtract each element in a ndarray with each and every element of another ndarray in numpy 将列表中的每个元素与另一个列表中的对应元素进行比较的最快方法是什么? - What is the fastest way to compare each element of a list with corresponding element of another list? 有没有办法从每个元素中获取所有特定段落? - Is there a way to get all of the specific paragraphs from each element? 重新组装从客户端发送到服务器的字节的最佳方法是什么? - What's the best way to reassemble bytes sent from client to server? 从值列表中获取日期的最佳方法是什么 - What's the best way to get a date from a list of values 有没有办法为每个选项设置文本和值的组合元素(PySimpleGuiQt's)? (类似于 HTML 的选项列表) - Is there a way to set to Combo element(PySimpleGuiQt's) with text and value to each option? (Something like HTML's option list) 我需要从列表中的每个数字中减去 1。有什么简单的方法吗? - I need to subtract 1 from each digit in the list .is It any easy way? 在 Python 的列表中找到第一个和最后一个元素出现的最佳方法是什么? - What is the best possible way to find the first AND the last occurrences of an element in a list in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM