Python 代码生成:随机选取至少两种颜色的连续点数
以下是生成代码:
import random
nums = [401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413,
301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313,
201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213,
101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113]
result = []
while len(result) < 5:
# 随机选取一个数字
num = random.choice(nums)
# 判断是否已经存在该颜色的点数
color = num // 100
exist_colors = set([x // 100 for x in result])
if color not in exist_colors:
result.append(num)
else:
# 判断是否存在连续的点数
exist_nums = sorted([x % 100 for x in result if x // 100 == color])
if len(exist_nums) > 0 and num % 100 == exist_nums[-1] + 1:
result.append(num)
print(result)
输出结果可能会不同,因为每次运行的结果都是随机的。
原文地址: https://www.cveoy.top/t/topic/ncIM 著作权归作者所有。请勿转载和采集!