【问题标题】:I need to generate x amount of unique lists inside another list我需要在另一个列表中生成 x 个唯一列表
【发布时间】:2020-09-12 11:46:50
【问题描述】:

我能够生成所需的输出,但我需要其中的 10 个,并且每个列表都必须是唯一的。我想到的最好的解决方案是创建第二个函数,生成一个空列表并用第一个函数的列表填充每个元素。到目前为止我得到的输出是 x 数量的列表,但它们不是唯一的,当我尝试调用第二个函数中的第一个函数时,python 给了我错误。


import random

numbers = list(range(1, 35))
out = []
final = []
print(numbers)  # to see all numbers

# returns 7 unique pop'd numbers from list 'numbers' and appends them to list 'out'
def do():
    for x in range(7):
        out.append(numbers.pop(random.randrange(len(numbers))))

    print(sorted(out))


# In other words i want to print output from function do() 10 times but each item in list has to be unique, not the lists themself

def do_ten():
    for x in range(10):
        final.append(out)
        # do()  python doesnt like this call 
    print(sorted(final))


do_ten()

【问题讨论】:

标签: python list function for-loop


【解决方案1】:

这会生成特定数量的列表,在一个列表中,其中包含从1100的随机数,您可以使用ln分别控制列表和数字的数量。

import random

l, n = 3, 5 # The amount of lists and numbers respectively.
lis = [[i for i in random.sample(range(1, 35), n)] for group in range(l)]
print(lis)

随机输出:

[[16, 11, 17, 13, 9], [26, 6, 16, 29, 24], [24, 2, 4, 1, 20]]

【讨论】:

  • 可以多次获取同一个列表:[[1,2,3,4],[1,2,3,4],...]
  • 它是随机的。使用l, n = 30, 1 - 如果您有 1..34 的数字可供选择,则原则上可以获得 30 个长度为 1 的列表,这些列表是唯一的 - 但不太可能这样做。增加n 将减少获得相同数字的机会,但仍有可能。您的代码中没有任何内容阻止随机两次获得相同的列表
  • @PatrickArtner 它运行得很好,你刚才这么说,这里的答案也验证了stackoverflow.com/questions/22842289/… 那么为什么要对我和谢尔盖投反对票呢?出于恶意?此外,final 变量中的代码与我之前发布的类似。
【解决方案2】:

您正在从包含 34 个元素(从 1 到 34)的列表中弹出 10 次 7 的数字。这是不可能的。您的列表中至少需要有 70 个元素numbers(例如,从 0 到 69)。

根据您已经编写的代码,这是一个应该可行的解决方案:

import random

numbers = list(range(0, 70))
final = []
print(numbers)  # to see all numbers

# returns a list of 7 unique popped numbers from list 'numbers'
def do():
    out = []
    for x in range(7):
        l = len(numbers)
        r = random.randrange(l)
        t = numbers.pop(r)
        out.append(t)
    return out

# Call 10 times do() and add the returned list to 'final'
def do_ten():
    for x in range(10):
        out = do() # Get result from do()
        final.append(out) # Add it to 'final'

do_ten()
print(final)

【讨论】:

    【解决方案3】:

    有帮助吗:

    num_lists = 10
    len_list = 10 
    
    [list(np.random.randint(1,11,len_list)) for _ in range(num_lists)]
    

    由于有些人对“唯一性”的定义可能不同,你可以试试:

    source_list = range(0, num_lists*len_list,1)
    [list(np.random.choice(source_list, len_list, replace=False)) for _ in range(num_lists)]
    

    【讨论】:

    • 它不是 - 您可以随机获得多个列表(具有唯一值),它们本身是重复的:简化 [[1,2,3,4],[1,2,3,4],...] 还是不能?
    【解决方案4】:

    可以使用random.sample 从您的数字范围中提取 34 个数字中的 7 个而不重复 - 为确保您不会得到重复的列表,您可以将列表的元组添加到集合和最终结果中,并且只添加到最终结果如果这个元组还没有在集合中:

    import random
    
    numbers = range(1, 35)  # 1...34
    
    final = []
    chosen = set()
    while len(final) < 10:
        # replace popping numbers with random.sample
        one_list = random.sample(numbers, k=7) # 7 numbers out of 34 - no repeats
        # create a tuple of this list and only add to final if not yet added
        t = tuple(one_list)
        if t not in chosen:
            chosen.add(t)
            final.append(one_list)
    
    print (final)
    

    输出:

    [[1, 5, 10, 26, 14, 33, 6], 
     [3, 11, 1, 30, 7, 21, 18], 
     [24, 23, 28, 2, 13, 18, 1], 
     [4, 25, 32, 15, 22, 8, 27], 
     [32, 9, 10, 16, 17, 26, 12], 
     [34, 32, 10, 26, 16, 21, 20], 
     [6, 34, 22, 11, 26, 12, 5], 
     [29, 17, 25, 15, 3, 6, 5], 
     [24, 8, 31, 28, 17, 12, 15], 
     [6, 19, 11, 22, 30, 33, 15]]
    

    如果您不需要唯一的结果列表,您可以将其简化为单行,但其中可能有欺骗:

    final = [random.sample(range(1,11),k=7) for _ in range(10)]
    

    【讨论】:

    • 是的,这给了我想要的输出。但是我有一点可以使用 pop() 来解决这个问题。我只用第一个 for 循环就得到了我想要的结果,但我只得到了一次。有没有办法让它打印 10 次(不是同一个列表 10 次)按运行 10 次?
    • @dennis 将 numbers = ..out = ... 放入 do() (在循环之前)。不要在do() 中打印,而是在return out 中打印。在do_ten() 中将final.append(out) 更改为final.append(do()) - 如果你很不幸,你可能会得到两次相同的列表 - 因为它可能会在对do() 的不同调用中随机生成相同的列表。使用“pop”效率非常低 - 您实际上创建了一个包含 34 个数字的列表,然后通过在需要数据复制的随机位置弹出该列表来缩小该列表
    猜你喜欢
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    相关资源
    最近更新 更多