- #python生成随机时间段
- #2016年12月6日18:34:26 codegay
- #参考资料 http://www.runoob.com/python/python-date-time.html
- #Python 取一个时间段里面的时间 https://segmentfault.com/q/1010000006617581
-
- import time
- import random
- st = "2016-12-6 18:40:36"
- et = "2016-12-16 18:40:44"
- tf = "%Y-%m-%d %H:%M:%S"
-
- name = """11
- 22
- 33
- 44
- 狗三
- 猫4
- 猪八"""
- namelist = name.splitlines()
- count = len(namelist)
-
- print(namelist)
-
- def random_time(st,et,count=1):
- start = int(time.mktime(time.strptime(st,tf)))
- end = int(time.mktime(time.strptime(et,tf)))
- mktime = random.sample(range(start,end),count)
- strtime = [time.strftime(tf,time.localtime(r)) for r in sorted(mktime)]
- return strtime
-
- result = random_time(st,et,count)
-
- random.shuffle(namelist)#打乱名单的顺序
-
- for t in zip(result,namelist):
- print(*t)
-
-
- """
- 输出:
- ['11', '22', '33', '44', '狗三', '猫4', '猪八']
- 2016-12-08 04:00:26 33
- 2016-12-09 11:16:06 猪八
- 2016-12-12 08:13:51 狗三
- 2016-12-12 11:57:52 猫4
- 2016-12-12 12:56:37 11
- 2016-12-12 22:20:52 44
- 2016-12-16 02:47:04 22
- """
复制代码
|