在列表中查找数量递增的组
问题内容:
目的是找到给定整数列表的递增/单调数组。 结果组中的每一项必须比上一项增加+1
给定输入:
x = [7, 8, 9, 10, 6, 0, 1, 2, 3, 4, 5]
我需要找到数量不断增加的群体并实现以下目标:
increasing_numbers = [(7,8,9,10), (0,1,2,3,4,5)]
最终还有越来越多的数字:
len(list(chain(*increasing_numbers)))
还有小组的len:
increasing_num_groups_length = [len(i) for i in increasing_numbers]
我已经尝试了以下方法来获得越来越多的数字:
>>> from itertools import tee, chain
>>> def pairwise(iterable):
... a, b = tee(iterable)
... next(b, None)
... return zip(a, b)
...
>>> x = [8, 9, 10, 11, 7, 1, 2, 3, 4, 5, 6]
>>> set(list(chain(*[(i,j) for i,j in pairwise(x) if j-1==i])))
set([1, 2, 3, 4, 5, 6, 8, 9, 10, 11])
>>> len(set(list(chain(*[(i,j) for i,j in pairwise(x) if j-1==i]))))
10
但是我无法保持顺序和数量不断增加的群体。
如何获得increasing_numbers
整数元组以及increasing_num_groups_length
?的组?
另外,是否有针对此类/类似问题的名称?
已编辑
我想出了这个解决方案,但是它太冗长了,我敢肯定有一种更简单的方法可以实现increasing_numbers
输出:
>>> from itertools import tee, chain
>>> def pairwise(iterable):
... a, b = tee(iterable)
... next(b, None)
... return zip(a, b)
...
>>> x = [8, 9, 10, 11, 7, 1, 2, 3, 4, 5, 6]
>>> boundary = iter([0] + [i+1 for i, (j,k) in enumerate(pairwise(x)) if j+1!=k] + [len(x)])
>>> [tuple(x[i:next(boundary)]) for i in boundary]
[(8, 9, 10, 11), (1, 2, 3, 4, 5, 6)]
有没有更多的pythonic /较少冗长的方法来做到这一点?
另一个输入/输出示例:
[在]:
[17、17、19、20、21、22、0、1、2、2、4、5、6、7、8、9、10、11、12、13、14、14、14、28、29
,30、31、32、33、34、35、36、40]
[出]:
[(19,20,21,22),(0,1,2),(4,5,6,7,8,9,10,11,12,13,14),(28,29,30,
31,32,33,34,35,36)]
问题答案:
使用itertools和numpy的几种不同方式:
from itertools import groupby, tee, cycle
x = [17, 17, 19, 20, 21, 22, 0, 1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 14, 28, 29, 30, 31, 32, 33, 34, 35,
36, 1, 2, 3, 4,34,54]
def sequences(l):
x2 = cycle(l)
next(x2)
grps = groupby(l, key=lambda j: j + 1 == next(x2))
for k, v in grps:
if k:
yield tuple(v) + (next((next(grps)[1])),)
print(list(sequences(x)))
[(19, 20, 21, 22), (0, 1, 2), (4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14), (28, 29, 30, 31, 32, 33, 34, 35, 36), (1, 2, 3, 4)]
或者使用python3并从中产生:
def sequences(l):
x2 = cycle(l)
next(x2)
grps = groupby(l, key=lambda j: j + 1 == next(x2))
yield from (tuple(v) + (next((next(grps)[1])),) for k,v in grps if k)
print(list(sequences(x)))
在这里使用我的答案的变化与numpy.split:
out = [tuple(arr) for arr in np.split(x, np.where(np.diff(x) != 1)[0] + 1) if arr.size > 1]
print(out)
[(19, 20, 21, 22), (0, 1, 2), (4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14), (28, 29, 30, 31, 32, 33, 34, 35, 36), (1, 2, 3, 4)]
类似于ekhumoro的回答:
def sequences(x):
it = iter(x)
prev, temp = next(it), []
while prev is not None:
start = next(it, None)
if prev + 1 == start:
temp.append(prev)
elif temp:
yield tuple(temp + [prev])
temp = []
prev = start
获取长度和元组:
def sequences(l):
x2 = cycle(l)
next(x2)
grps = groupby(l, key=lambda j: j + 1 == next(x2))
for k, v in grps:
if k:
t = tuple(v) + (next(next(grps)[1]),)
yield t, len(t)
def sequences(l):
x2 = cycle(l)
next(x2)
grps = groupby(l, lambda j: j + 1 == next(x2))
yield from ((t, len(t)) for t in (tuple(v) + (next(next(grps)[1]),)
for k, v in grps if k))
def sequences(x):
it = iter(x)
prev, temp = next(it), []
while prev is not None:
start = next(it, None)
if prev + 1 == start:
temp.append(prev)
elif temp:
yield tuple(temp + [prev]), len(temp) + 1
temp = []
prev = start
这三个输出都将相同:
[((19, 20, 21, 22), 4), ((0, 1, 2), 3), ((4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14), 11)
, ((28, 29, 30, 31, 32, 33, 34, 35, 36), 9), ((1, 2, 3, 4), 4)]