我有一个从(0是午夜)开始的小时列表。你知道吗
hour = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
我想随机生成一个连续3小时的序列。示例:
[3,6]
或者
[15, 18]
或者
[23,2]
等等。随机抽样没有达到我想要的!你知道吗
import random
hourSequence = sorted(random.sample(range(1,24), 2))
有什么建议吗?你知道吗
Tags:
不确定你想要什么,但可能
注意:其他答案都没有考虑可能的序列
[23,0,1]
请注意,使用python lib中的
itertools
可以实现以下功能:这将以循环方式打印
hours
列表中三个值的序列,这也将包括在结果中,例如[23,0,1]
。你知道吗你可以试试这个:
相关问题 更多 >
编程相关推荐