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]])
number of sequences in a list
سؤال
I need to determine the # of sequences that are in a list of integers. A member in the list can be part of the sequence is it falls in a range of 65. So in my list,
43
83
90
250
265
500
43,83,90 are all members in 1 sequence, they all fall in a range of 65. Also 250,265 make up 1 sequence. Right now my code creates tuples from the list. (43,83) (83,90) but It dosent know that they both are part of the same sequence.
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
What can I add to determine if my tuples are in the same sequence to get a correct count? Let me know if you need any more clarification. Thanks!
المحلول
لا تنتمي إلى StackOverflow