import random
def split(full_list, shuffle=False, ratio=0.2):
n_total = len(full_list)
offset = int(n_total * ratio)
if n_total == 0 or offset < 1:
return [], full_list
if shuffle:
random.shuffle(full_list)
sublist_1 = full_list[:offset]
sublist_2 = full_list[offset:]
return sublist_1, sublist_2
if __name__ == "__main__":
li = range(1956)#范围
a = list(li)
sublist_1, sublist_2 = split(a, shuffle=True, ratio=0.2)#比例为0.2
print(sublist_1, len(sublist_1))
print(sublist_2, len(sublist_2))
Python随机数生成(可按比例)
最新推荐文章于 2023-04-11 15:34:33 发布