Python-如何形成2个列表的随机分区 [英] Python- How to form a random partition of 2 lists

查看:100
本文介绍了Python-如何形成2个列表的随机分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在python中形成2个列表(List1和List2)的随机分区吗?列表不必具有相同的大小. 例如:

Does anyone know how to form a random partition of 2 lists (List1 and List2) in python? The lists do not have to have the same size. For example:

S = [1,2,3,4,5,6,7]
List1=[3,6,1,2]
List2=[5,4,7]

List1 =[3,5]
List2=[1,2,4,7,6]

推荐答案

我不确定您关于随机性和分区的规则是什么,但这应该可以帮助您入门:

I'm not sure what your rules are around randomness and partitioning, but this should get you started:

import random

s = [1,2,3,4,5,6,7]

random.shuffle(s)

cut = random.randint(0, len(s))
list_1 = s[:cut]
list_2 = s[cut:]

print list_1
print list_2

这篇关于Python-如何形成2个列表的随机分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆