简体   繁体   中英

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.

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

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

The idea behind itertools recipes is to consume it lazily. 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:

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:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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