简体   繁体   English

比较Python列表中值之间的差异

[英]Comparing Difference between Values within Python List

Let's say I have a list of integers: 假设我有一个整数列表:

list = [1,2,3,5,6,7,10,11,12]

And I'd like to divide the list in to three separate lists, with the split occurring between consecutive integers with a difference >=2, which would give me 我想将列表分为三个单独的列表,拆分发生在连续整数之间,且差值> = 2,这将给我

list1 = [1, 2, 3]
list2 = [5, 6, 7]
list3 = [10, 11, 12]

Is there a straightforward way to do this in Python? 有没有一种简单的方法可以在Python中做到这一点? I would like to do this in order to analyze data from a psychology experiment, where I have a list of timestamped response and want to cluster responses based on how far apart they are 为了分析来自心理学实验的数据,我想这样做,在该实验中,我列出了带有时间戳记的响应,并希望根据响应之间的距离对响应进行聚类

Take a look at this StackOverflow question . 看一下这个StackOverflow问题 The answers there show you how to divide a list into sublists of consecutive integers. 那里的答案显示了如何将列表划分为连续整数的子列表。

From the accepted answer there: 从接受的答案那里:

>>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
>>> for k, g in groupby(enumerate(data), lambda (i,x):i-x):
...     print map(itemgetter(1), g)
...
[1]
[4, 5, 6]
[10]
[15, 16, 17, 18]
[22]
[25, 26, 27, 28]

The answer doesn't offer any explanation of what's going on here, so I'll explain. 答案未提供任何解释,所以我将解释。 First, it assumes that data is sorted in ascending order. 首先,它假定data按升序排序。 Enumerating data then gives a list of index, value pairs. 枚举data然后给出索引,值对的列表。 It then uses the index minus the value as a key for grouping the items. 然后,它使用索引减去值作为组项目的键。 Take a look at what this does for your list: 看看这对您的列表有什么作用:

>>> myList = [1,2,3,5,6,7,10,11,12]
>>> [i - x for i, x in enumerate(myList)]
[-1, -1, -1, -2, -2, -2, -4, -4, -4]

As you can see, consecutive values end up having the same grouping key. 如您所见,连续的值最终具有相同的分组键。 This is because 这是因为

if data[i] + 1 == data[i+1]: 如果data [i] + 1 == data [i + 1]:

then data[i] - i == data[i] + 1 - 1 - i == data[i+1] - (i + 1) 然后data [i]-i == data [i] +1-1-i == data [i + 1]-(i +1)

FYI, groupby comes from itertools and itemgetter comes from operator . 仅供参考, groupby来自itertoolsitemgetter来自operator So add these lines to your imports: 因此,将以下几行添加到您的导入中:

from itertools import groupby
from operator import itemgetter

Just be aware that this solution will only work if data is sorted and does not contain any duplicates. 请注意,此解决方案仅在对data进行排序且不包含任何重复项的情况下才有效。 Of course, it's fairly straightforward to turn a list into a sorted set: 当然,将列表转换为排序集非常简单:

>>> myList = [1, 1, 3, 5, 6, 4, 10, 12, 11, 1, 2]
>>> myList = list(sorted(set(myList)))
>>> print myList
[1, 2, 3, 4, 5, 6, 10, 11, 12]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM