按范围对数字的平面列表进行分组 [英] grouping a flat list of number by range

查看:65
本文介绍了按范围对数字的平面列表进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我正在寻找一种方法,可以在搜索之后按照连续的

间隔分组编号,例如:


[3,6,7,8,12,13,15]


=>

[[3,4],[6,9],[12,14],[15,16]]


(6,不是3,所以3 => [3:4]; 7,8跟随6,所​​以6,7,8 =>

[6:9],依此类推)


i能够在没有发电机/产量的情况下使用它,但我认为它们可以更好地与它们相比,可能是你的想法吗?


最好的问候,


J.

解决方案

jo ****** @ yahoo.fr 写道:

我正在寻找一种方法在搜索之后有一个按连续的间隔分组的编号列表,例如:

[3,6,7,8,12,13,15]

=>

[[3,4],[6,9],[12,14],[15,16]]

(6,不是F 3,所以3 => [3:4]; 7,8以下6所以6,7,8 =>
[6:9],依此类推)

我能够没有发电机/产量但我认为它可能对他们来说更好,可能是你的主意吗?




不要阻止你的代码。是否可以更换每次出现的


result_list.append([start,stop])




收益率[开始,停止]





彼得


< blockquote>文章< 11 ********************* @ c74g2000cwc.googlegroups。 com>,

< jo ****** @ yahoo.fr>写道:

你好,

我正在寻找一种方法,在搜索后有一个按连续的间隔分组的编号列表,例如:

[3,6,7,8,12,13,15]

=>

[[3,4],[ 6,9],[12,14],[15,16]]

(6,不跟随3,所以3 => [3:4]; 7,8跟随6所以6 ,7,8 =>
[6:9],等等)

我能够没有发电机/产量,但我认为它可能更好跟他们一起,可能是你的主意吗?


可能有更好的方法,但这有效

最好的问候,

J。




class IterInterval(object):

"""创建一个迭代器,给定一个整数列表,每次运行
连续整数i ... j,产生一个两个

元素列表[i,j + 1]


Singleton列表[i]返回[i, i + 1]

空列表返回无

""

def __init __(self,seq):

self.seq = seq

self.firstval =无

self.index = 0

def __iter __( self):

#firstval =连续整数运行的开始

#lastval =在运行中找到的最后一个值

#nextval =从输入列表中获取的最新值

如果不是self.firstval:

#设置第一次迭代

如果self.index > = len(self.seq):

#空列表,返回

提高StopIteration

self.firstval = lastval = int(self.seq [self.index])

self.index + = 1

而True:

如果self.index> = len(self.seq):

#list耗尽,输出最后读取的值

yield [self .firstval,lastval + 1]

提高StopIteration

nextval = int(self.seq [self.index])

self.index + = 1

如果nextval == lastval + 1:

lastval = nextval

继续

else:

#运行结束 - 输出运行,重置以进行下一次调用

yield [self.firstval,lastval + 1]

self.firstval = lastval = nextval

继续


if __name__ ==''__ main__'':

for l in [[3,6, 7,8,12,13,15],[2],[]]:

print l," =>",[lst for IstInterval(l)]


/ usr / home / jes%python interval.py

[3,6,7,8,12,13,15] => [[3,4],[6,9],[12,14],[15,16]]

[3] => [[3,4]]

[] => []


-

Jim Segrave(je*@jes-2.demon.nl)


jo******@yahoo.fr 写道:

我正在寻找一种方法,在搜索后按照连续的间隔分组编号列表,例如:

[3,6,7,8,12 ,13,15]

=>

[[3,4],[6,9],[12,14],[15,16]]

(6,不跟随3,所以3 => [3:4]; 7,8跟随6所以6,7,8 =>
[6:9],和所以)

我能够没有发电机/产量,但我认为它可以更好用它们,可能是你的主意吗?




当然:


def group_intervals(it):

it = iter(it)

val = it.next()

run = [val,val + 1]

for val:

如果val == run [1]:

run [1] + = 1

else:

收益率运行

run = [ val,val + 1]

yie ld run


--Ben


hello,

i''m looking for a way to have a list of number grouped by consecutive
interval, after a search, for example :

[3, 6, 7, 8, 12, 13, 15]

=>

[[3, 4], [6,9], [12, 14], [15, 16]]

(6, not following 3, so 3 => [3:4] ; 7, 8 following 6 so 6, 7, 8 =>
[6:9], and so on)

i was able to to it without generators/yield but i think it could be
better with them, may be do you an idea?

best regards,

J.

解决方案

jo******@yahoo.fr wrote:

i''m looking for a way to have a list of number grouped by consecutive
interval, after a search, for example :

[3, 6, 7, 8, 12, 13, 15]

=>

[[3, 4], [6,9], [12, 14], [15, 16]]

(6, not following 3, so 3 => [3:4] ; 7, 8 following 6 so 6, 7, 8 =>
[6:9], and so on)

i was able to to it without generators/yield but i think it could be
better with them, may be do you an idea?



Don''t hold back your code. Would it work to replace each occurrence of

result_list.append([start, stop])

with

yield [start, stop]

?

Peter


In article <11*********************@c74g2000cwc.googlegroups. com>,
<jo******@yahoo.fr> wrote:

hello,

i''m looking for a way to have a list of number grouped by consecutive
interval, after a search, for example :

[3, 6, 7, 8, 12, 13, 15]

=>

[[3, 4], [6,9], [12, 14], [15, 16]]

(6, not following 3, so 3 => [3:4] ; 7, 8 following 6 so 6, 7, 8 =>
[6:9], and so on)

i was able to to it without generators/yield but i think it could be
better with them, may be do you an idea?
There are probably better ways, but this works

best regards,

J.



class IterInterval(object):
"""Create an iterator which, given a list of integers,
for each run of consecutive integers i...j, yields a two
element list [i, j + 1]

Singleton lists [i] return [i, i + 1]
Empty lists return None
"""
def __init__(self, seq):
self.seq = seq
self.firstval = None
self.index = 0

def __iter__(self):
# firstval = the start of a run of consecutive integers
# lastval = the last value found in the run
# nextval = the most recent value taken from the input list
if not self.firstval:
# set up the first iteration
if self.index >= len(self.seq):
# empty list, return
raise StopIteration
self.firstval = lastval = int(self.seq[self.index])
self.index += 1
while True:
if self.index >= len(self.seq):
# list exhausted, output the last value read
yield [self.firstval, lastval + 1]
raise StopIteration
nextval = int(self.seq[self.index])
self.index += 1
if nextval == lastval + 1:
lastval = nextval
continue
else:
# end of run - output the run, reset for next call
yield [self.firstval, lastval + 1]
self.firstval = lastval = nextval
continue

if __name__ == ''__main__'':
for l in [[3, 6, 7, 8, 12, 13, 15], [2], []]:
print l, "=>", [lst for lst in IterInterval(l)]

/usr/home/jes% python interval.py
[3, 6, 7, 8, 12, 13, 15] => [[3, 4], [6, 9], [12, 14], [15, 16]]
[3] => [[3, 4]]
[] => []

--
Jim Segrave (je*@jes-2.demon.nl)


jo******@yahoo.fr wrote:

i''m looking for a way to have a list of number grouped by consecutive
interval, after a search, for example :

[3, 6, 7, 8, 12, 13, 15]

=>

[[3, 4], [6,9], [12, 14], [15, 16]]

(6, not following 3, so 3 => [3:4] ; 7, 8 following 6 so 6, 7, 8 =>
[6:9], and so on)

i was able to to it without generators/yield but i think it could be
better with them, may be do you an idea?



Sure:

def group_intervals(it):
it = iter(it)
val = it.next()
run = [val, val+1]
for val in it:
if val == run[1]:
run[1] += 1
else:
yield run
run = [val, val+1]
yield run

--Ben


这篇关于按范围对数字的平面列表进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆