Python 将列表中的绝对时间转换为相对时间

Python 将列表中的绝对时间转换为相对时间,python,list,loops,Python,List,Loops,问题是: 我有一个事件列表及其绝对时间:- [0, 10, 30, 50, ... ] 我的任务是从这个列表中选取一个子集,并使时间相对 [0, 10, 20, 20,....] 我现在正在做这样的事情:- for element in list : if(some criteria) : append the element to new empty list result = [] result.append(new_list[0]) for x in xra

问题是:

我有一个事件列表及其绝对时间:-

[0, 10, 30, 50, ... ]
我的任务是从这个列表中选取一个子集,并使时间相对

[0, 10, 20, 20,....] 
我现在正在做这样的事情:-

for element in list :

   if(some criteria) :

       append the element to new empty list

result = []
result.append(new_list[0])
for x in xrange(len(new_list) - 1)
    result.append(new_list[i + 1] - new_list[i])     

有没有更好、更有效的方法

您可以尝试使用
zip
和列表切片 :

示例:

times = [0, 10, 30, 50]
relative_times = [b - a for a, b in zip(times, times[1:])]
# [10, 20, 20]

您可以尝试使用
zip
和列表切片 :

示例:

times = [0, 10, 30, 50]
relative_times = [b - a for a, b in zip(times, times[1:])]
# [10, 20, 20]

您可以尝试使用
zip
和列表切片 :

示例:

times = [0, 10, 30, 50]
relative_times = [b - a for a, b in zip(times, times[1:])]
# [10, 20, 20]

您可以尝试使用
zip
和列表切片 :

示例:

times = [0, 10, 30, 50]
relative_times = [b - a for a, b in zip(times, times[1:])]
# [10, 20, 20]

您所拥有的东西是否存在特定的性能问题?没有什么重大问题。我仍然希望避免重复两次循环。如果有可能的话,你所拥有的东西有什么特别的性能问题吗?没什么大不了的。我仍然想避免重复两次循环。如果有可能的话,你所拥有的东西有什么特别的性能问题吗?没什么大不了的。我仍然想避免重复两次循环。如果有可能的话,你所拥有的东西有什么特别的性能问题吗?没什么大不了的。我仍然想避免重复两次循环。如果可能的话,谢谢你的快速回复!谢谢你的快速回复!谢谢你的快速回复!谢谢你的快速回复!