python list of difference beetwen values in list

Solutions on MaxInterview for python list of difference beetwen values in list by the best coders in the world

showing results for - "python list of difference beetwen values in list"
Zayden
10 Nov 2019
1>>> t = [1, 3, 6]
2>>> v = [t[i+1]-t[i] for i in range(len(t)-1)]
3>>> v
4[2, 3]
5
Thais
01 Oct 2017
1v = np.diff(t)
2
Riccardo
27 Jun 2019
1v = np.diff(t + [t[-1]])
2
Lorenzo
15 Oct 2017
1v = np.diff(np.append(t[0], t))
2
Enya
07 Jan 2021
1v = np.diff([t[0]] + t) # for python 3.x
2
Niclas
26 Mar 2019
1>>> t
2[1, 3, 6]
3>>> [j-i for i, j in zip(t[:-1], t[1:])]  # or use itertools.izip in py2k
4[2, 3]
5