简体   繁体   English

如何创建随机列表列表

[英]How to create a random list of lists

How can you create a random list of lists.如何创建随机列表。 The random.sample(range(80), 10) produces a list of 10 items up to 80. random.sample(range(80), 10)生成一个包含 10 个项目的列表,最多为 80 个。

Example Output:示例输出:

[1,4,6,44,78,45,32,56,72,23]

But can can you add more data so it is a list of lists?但是您能否添加更多数据使其成为列表列表?

Desired Output:期望输出:

[1,4,6,44,78,45,32,56,72,23]
[1,4,6,44,78,45,32,56,72,23]
[1,4,6,44,78,45,32,56,72,23]
[1,4,6,44,78,45,32,56,72,23]
[1,4,6,44,78,45,32,56,72,23]
[1,4,6,44,78,45,32,56,72,23]

random.sample的结果封装在一个列表中并应用*运算符:

new_list = [random.sample(range(80), 10)]*6

You can use itertools.repeat您可以使用itertools.repeat

>>> from itertools import repeat
>>> list(repeat(random.sample(range(80),10),6))

The idea behind itertools recipes is to consume it lazily. itertools食谱背后的想法是懒惰地消费它。 So its a better to use it like:所以最好像这样使用它:

>>> items = repeat(random.sample(range(80),10),6)
>>> for item in items:
        #consume item here with your program logic

Maybe the solution to give a better explanation is to think about what kind of list you need to generate.也许给出更好解释的解决方案是考虑您需要生成什么样的列表。 A list can represent a lot of data like items in a table, a market list and lots of other good representations.列表可以表示大量数据,例如表中的项目、市场列表和许多其他良好的表示。

Following some quality patterns of production all in relation of code you can made each item be generate by a factor and the list presented on the stack pile or collection of data be generated in a right portable way.遵循一些与代码相关的生产质量模式,您可以使每个项目由一个因素生成,并以正确的可移植方式生成堆栈堆上的列表或数据集合。

Get arguments based on the program input:根据程序输入获取参数:

from sys import argv

First of all utilize that can be made a shot a-top from a horse:首先利用可以从马身上进行射击:

from collections import UserList

The ceil function to work with a calculation of the seed factor a prediction generation be a machine round not a human like sequences:用于计算种子因子和预测生成的 ceil 函数是机器轮而不是像序列一样的人:

from math import ceil

The sample function generation offered on the box:盒子上提供的示例函数生成:

from random import sample

A seed function utilize to limit random generation or increase without a default value offered by the implementation:种子函数用于限制随机生成或增加,而没有实现提供的默认值:

from random import seed

A better usage of iterations can be made using the following function:可以使用以下函数更好地使用迭代:

from itertools import repeat

Now you can have this with the following behavior attached round ups with correction:现在,您可以使用以下行为附加整整和更正:

seed(ceil(1024*8.6))

One first anonymous lambda function for a small generation:小代的第一个匿名 lambda 函数:

sample_gen_fn = lambda l=80, n=10, s=0: sample(range(s,l),n)

Another to work for a nice process of the list:另一个为列表工作的一个很好的过程:

user_sample_gen_fn = lambda l=80, n=10, s=0: UserList(sample_gen_fn(l,n,s))

In a world who cats can be predict or read the list this can not made a damage on the brain of the animal by example.在一个猫可以预测或阅读列表的世界中,这不会对动物的大脑造成损害,例如。 On the another running a lot have an damage, thinking on the cat like your processor... this can be extend the computer life time.在另一个运行很多有损坏,想像你的处理器的猫......这可以延长计算机的使用寿命。

All of that can be used on a large context not explained on the description but you need to think on your implementation.所有这些都可以在描述中没有解释的大上下文中使用,但您需要考虑您的实现。

A little observation the pattern used on the native function for a iteration can made an argument specific allocation without call up on the variable with an addressable content this avoid the usage on context just for debug.对用于迭代的本机函数的模式进行一点观察,可以在不调用具有可寻址内容的变量的情况下进行参数特定的分配,这避免了仅用于调试的上下文的使用。

def reproduce_pile_fn(ll=6):
    return repeat(lambda: user_sample_gen_fn(),ll)

def stack_pile_cmd(ll=6, l=80, n=10, s=0):
    for seq in reproduce_pile_fn(ll):
        print(seq())

I think this can help in equality with another answers because is need a good explanation for a small or big implementation.我认为这可以帮助与其他答案平等,因为需要对小型或大型实现进行很好的解释。

After that you can call the function on the specific condition.之后,您可以在特定条件下调用该函数。

if __name__ == '__main__':
    # Usage with arguments comming from the program
    # executed as an script.
    #
    # args = argv[1:]
    # stack_pile_cmd(args[0])
    #
    # Common usage

    stack_pile_cmd()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM