列表中的连续值(python)
我试图找出下面的列表是否包含超过4个连续值 小时数=[9,10,11,12,13,14,15] 我已经尝试过这段代码,并返回上面的列表有4个以上的连续值,但它不适用于其他数字列表中的连续值(python),python,Python,我试图找出下面的列表是否包含超过4个连续值 小时数=[9,10,11,12,13,14,15] 我已经尝试过这段代码,并返回上面的列表有4个以上的连续值,但它不适用于其他数字 hours=[9,28,11,23,13,9,15] y = sorted(hours) consecutive_hours = 0 while consecutive_hours <=4: for hours in hours: if len(y) == 7 and y[6]-y[0] =
hours=[9,28,11,23,13,9,15]
y = sorted(hours)
consecutive_hours = 0
while consecutive_hours <=4:
for hours in hours:
if len(y) == 7 and y[6]-y[0] == 6:
consecutive_hours += 1
else:
break
if consecutive_hours > 4:
print("The list", hours, "contains more than 4 consecutive hours.")
else:
print("The list",hours, "does not contain more than 4 consecutive hours.")
hours=[9,28,11,23,13,9,15]
y=已排序(小时)
连续_小时=0
连续4小时:
打印(“列表”,小时,“包含超过4个连续小时。”)
其他:
打印(“列表”,小时,“不包含超过4个连续小时。”)
试试这个
hours = [9, 28, 11, 23, 13, 9, 15]
y = sorted(hours)
# Starts with 1 consecutive hour, we will subtract later
consecutive_hours = 1
# Check for the number of consecutive hours
for i in range(len(y)):
if y[i] == y[i - 1] + 1:
consecutive_hours += 1
if consecutive_hours == 1: consecutive_hours = 0
# Print results
if consecutive_hours > 4:
print("The list ", hours, " contains more than 4 consecutive hours.")
else:
print("The list ", hours, " does not contain more than 4 consecutive hours.")
你可以试试这样的
hours=[9,28,11,23,13,9,15]
y = sorted(hours)
consecutive_hours = 1
for i in range(0, len(y)):
j = i + 1
if j >= len(y):
break
if y[j] == y[i] + 1:
consecutive_hours += 1
elif consecutive_hours > 4:
break
else:
consecutive_hours = 1
if consecutive_hours > 4:
print("The list", hours, "contains more than 4 consecutive hours.")
else:
print("The list",hours, "does not contain more than 4 consecutive hours.")
下面的代码将允许您检查列表中的下一个数字是否是
for
循环所在的当前数字的连续数字,如果是,它将向continuous_hours
变量添加一个。如果for
循环达到的数值与前一个数值不连续,它会将连续时间设置为0
。如果在for
循环在整个列表上迭代之前continuous_hours
变量变得大于4
,则for
循环将中断,如果循环到达列表末尾时该值小于或等于4
,则循环将结束。以下if
语句将在for
循环后检查连续小时数
值,并打印相应的语句
hours=[9,28,11,23,13,14,15,16,17,9,15,1,2,3,4]
y = sorted(hours)
consecutive_hours = 0
for num in hours:
if hours.index(num) == len(hours)-1:
break
elif hours[hours.index(num)+1] == num + 1:
consecutive_hours += 1
if consecutive_hours > 4:
break
else:
consecutive_hours = 0
if consecutive_hours == 4:
print("The list", hours, "contains more than 4 consecutive hours.")
else:
print("The list",hours, "does not contain more than 4 consecutive hours.")
在不进行排序的情况下,可以使用计数器类(来自集合)计算将所有值及其偏移量按0、1、2和3组合时得到的匹配数:
from collections import Counter
def maxConsec(hours,span=4):
counts = Counter(h-i for h in {*hours} for i in range(span))
return span in counts.values()
输出:
maxConsec([9,10,11,12,13,14,15]) # True
maxConsec([9,28,11,23,13,9,15]) # False
你的问题不太清楚,你到底想实现什么,输出是什么样子的?除非连续工作小时数
达到4,否则while循环无法结束。您的break语句仅中断for循环。@Ade_1我需要一个代码来检查列表是否有超过4个连续值。在这种情况下,我有一个不同小时的列表,我试图找到一个代码来检查列表是否包含超过4个连续小时。小时数=[9,10,11,12,13,14,15]hours.index(num)
?如果列表中包含重复项怎么办?@Andre Liberty我不明白这有什么问题。代码用于检测4个数字是否连续,这是OP要求的。通过做这个索引,我的方法可能不如你的方法有效,但它是有效的ật Huy,len(y)返回一个整数。不能在整数上循环阿德欧17分钟前Delete@Ade_1非常感谢你指出这一点。我已经修好了。