我试图将一个列表转换为一个具有随机值的列表,并带有一些限制。
a= [[10],[8],[4]] #this list will tell us how many elements/items should be in each list of list. For example in our new list, the first list will have 10 elements/items and so on.
b=[[15],[10],[5]] #this list will be used to determine the maximum number that each element can be. For example in our new list, since it can only have 10 elements/items we can only reach up to and including number 15. This process is done at random.
我需要一个新的列表a,看起来像下面的列表。列表中的每个项目都必须是唯一的(因此不允许重复值)
new_list=[[1,2,4,5,8,9,10,11,12,13],[3,4,5,6,7,8,9,10],[1,2,4,5]] # sample output 1
new_list=[[5,6,7,8,9,10,11,12,13,14,15],[1,2,3,4,5,6,7,8],[2,3,4,5]] # sample output 2
这就是我目前所拥有的(MWE)
import random
a=[[10],[8],[4]] # list that informs the number of items in each list of list.
b=[[15],[10],[5]] # the restriction list.
new_list=[] #my final list
for values in a:
new_list.append(values[0])
new_list=[[item] for item in new_list]
print(new_list)
我的问题是如何使用列表b将新_列表中的第一个列表转换为随机列表。
我希望这有意义。