繁体   English   中英

列表中的序列数

[英]number of sequences in a list

我需要确定整数列表中的序列数。 列表中的成员可以是序列的一部分,如果它落在 65 的范围内。所以在我的列表中,

43
83
90
250
265
500

43,83,90都是1序列的成员,都在65的范围内。另外250,265组成1序列。 现在我的代码从列表中创建元组。 (43,83) (83,90) 但它不知道它们都是同一序列的一部分。

while count < len(w_seq_list)-1:
 if((w_seq_list[count+1] > w_seq_list[count]) and ((w_seq_list[count+1] - w_seq_list[count]) <= 65)):

     wr.append((w_seq_list[count],w_seq_list[count+1])) // add tuple to sequence list
     count += 1
 else:
     count += 1
     continue

我可以添加什么来确定我的元组是否在相同的序列中以获得正确的计数? 如果您需要更多说明,请告诉我。 谢谢!

def grouper(my_list):
    previous, temp, result = my_list[0], [my_list[0]], []
    for number in my_list[1:]:
        if number - previous > 65:
            result.append(temp)
            temp, previous = [number], number
        else:
            temp.append(number)
    if temp:
        result.append(temp)
    return result

assert(grouper([43, 83, 90, 250, 265, 500]) == [[43, 83, 90], [250, 265], [500]])
assert(grouper([15, 43, 83, 90]) == [[15, 43], [83, 90]])

暂无
暂无

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

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