有几率出现重复
tools.py
#!/usr/bin/env python3 # coding=utf-8 # Version:python3.6.1 # File:tools.py import random from datetime import datetime __date__ = '2020/6/30 11:10' __author__ = 'LGSP_Harold' def gen_trans_id(date=None): """ 根据所传入的时间得到一个唯一的交易流水ID :param date:日期 :return:交易流水ID字符串 """ # 如果没有传入时间,则使用系统当前时间 if date is None: date = datetime.now() # 保证字符串的唯一(日期+时间+毫秒+6位随机数) date = date.strftime('%Y%m%d%H%M%S%f') date = date + str(random.randint(100000, 999999)) # date = '{0}{1}'.format(date, random.randint(100000, 999999)) return date if __name__ == '__main__': pass
test_module.py
#!/usr/bin/env python3 # coding=utf-8 # Version:python3.6.1 # File:test_module.py from datetime import datetime from trans.tools import gen_trans_id __date__ = '2020/6/30 11:36' __author__ = 'LGSP_Harold' def test_trans_tool(): id1 = gen_trans_id() return id1 # date = datetime(2016, 9, 9, 18, 27, 36) # id2 = gen_trans_id(date) # print(id2) if __name__ == '__main__': trans_list = [] for i in range(0, 10000): lst = test_trans_tool() trans_list.append(lst) print(trans_list) # 通过集合去重列表 count_set = set(trans_list) # print(len(count_set)) lst_str = set() for i in count_set: # 查找列表重复出现的元素 if trans_list.count(i) > 1: lst_str.add(i) print(i, '有', trans_list.count(i), '个') index_list = [] # 查找列表中某个元素出现的全部索引 for index, nums in enumerate(trans_list): if nums == i: index_list.append(index) print('索引位置:', index_list) if lst_str: print(lst_str) else: print('无重复')
略懂,略懂....