Python随机数⽣成(可按⽐例)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]
python生成1到100之间随机数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))
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论