嵌套列表中的唯一值 - 随机

时间:2013-11-18 12:31:01

标签: python python-3.x nested-lists

我有一个很大程度上工作的程序,它会创建由用户设置的嵌套列表,并根据用户输入重复。

但是我希望各个集合只包含当前这是我输出的唯一值。

> python3 testv.py 
Size of your Range?: 12
Size of your Sets?: 3
How many sets?: 4
[['Two', 'Seven', 'Five'], ['Four', 'Six', 'Two'], ['Three', 'Five', 'Thirteen'], ['Six', 'Two', 'Two']]

这是我的程序,x是我创建嵌套列表的列表理解,有一种很好的方法来定义它以保持值唯一。

import random


class WeightedRandomizer:

    def __init__(self, weights):
        self.__max = .0
        self.__weights = []
        for value, weight in weights.items():
            self.__max += weight
            self.__weights.append((self.__max, value))

    def random(self):
        r = random.random() * self.__max
        for ceil, value in self.__weights:
            if ceil > r:
                return value


range_size = 0
if range_size == 0:
    try:
        rSize = int(input('Size of your Range?: '))
        setSize = int(input('Size of your Sets?: '))
        numSets = int(input('How many sets?: '))
    except ValueError:
        print('That was not an integer!')
        range_size = 0


base_value = 100 / rSize
num_weighted = [base_value] * rSize
num_weighted[0] = round(base_value * 2.2, 1)
num_weighted[1] = round(base_value * 1.8, 1)
num_weighted[2] = round(base_value * 1.8, 1)
num_weighted[3] = round(base_value * 1.5, 1)
num_weighted[4] = round(base_value * 1.4, 1)
num_weighted[5] = round(base_value * 1.3, 1)


# redistribute the difference of top 6 and rest of range
top6 = (sum(num_weighted[0:6]))
not_top6 = rSize - 6
pts_alloc = round((100 - top6) / not_top6, 1)

num_weighted[6:] = [pts_alloc for i in range(len(num_weighted) - 6)]

keys = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight',
        'Nine', 'Ten', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen']

dictionary = dict(zip(keys, num_weighted))

wr = WeightedRandomizer(dictionary)

x = [[wr.random() for i in range(setSize)] for j in range(numSets)]

print(x)

1 个答案:

答案 0 :(得分:1)

将此功能添加到您的班级:

def take(self, amount):
    ret = []
    while True:
        item = self.random()
        if item in ret:
            continue
        ret.append(item)
        if len(ret) == amount:
            return ret

然后将列表理解改为:

x = [wr.take(setSize) for j in range(numSets)]

基本上答案是获取随机项目,然后存储它们,直到您拥有所需数量的唯一项目。


应该是这样的:

import random


class WeightedRandomizer:

    def __init__(self, weights):
        self.__max = .0
        self.__weights = []
        for value, weight in weights.items():
            self.__max += weight
            self.__weights.append((self.__max, value))

    def take(self, amount):
        ret = []
        while True:
            item = self.random()
            if item in ret:
                continue
            ret.append(item)
            if len(ret) == amount:
                return ret

    def random(self):
        r = random.random() * self.__max
        for ceil, value in self.__weights:
            if ceil > r:
                return value


range_size = 0
if range_size == 0:
    try:
        rSize = int(input('Size of your Range?: '))
        setSize = int(input('Size of your Sets?: '))
        numSets = int(input('How many sets?: '))
    except ValueError:
        print('That was not an integer!')
        range_size = 0


base_value = 100 / rSize
num_weighted = [base_value] * rSize
num_weighted[0] = round(base_value * 2.2, 1)
num_weighted[1] = round(base_value * 1.8, 1)
num_weighted[2] = round(base_value * 1.8, 1)
num_weighted[3] = round(base_value * 1.5, 1)
num_weighted[4] = round(base_value * 1.4, 1)
num_weighted[5] = round(base_value * 1.3, 1)


# redistribute the difference of top 6 and rest of range
top6 = (sum(num_weighted[0:6]))
not_top6 = rSize - 6
pts_alloc = round((100 - top6) / not_top6, 1)

num_weighted[6:] = [pts_alloc for i in range(len(num_weighted) - 6)]

keys = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight',
        'Nine', 'Ten', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen']

dictionary = dict(zip(keys, num_weighted))

wr = WeightedRandomizer(dictionary)

x = [wr.take(setSize) for j in range(numSets)]

print(x)