"Python Actual Combat" 1-Randomly select a specified number of unique elements from the list

Detailed description of the problem:

​From a set of lists, strings, tuples, etc., randomly select a specified number of unique elements.

Related problem description:

Randomly select multiple specified elements, randomly generate multiple specified unique numbers within a specified range, randomly select one element, and randomly select random unique elements.

Key words:

List, random, non-repetitive, specified number, random, sample, choice

solution:

1. Use the random.sample() function to randomly select multiple unique elements

The random.sample() function can randomly select a specified number of non-repeating elements from a set of lists, strings, tuples, etc., and output the result as a list.

The sentence structure is:

random.sample(sep, n)

Among them, sep represents a collection or sequence of lists, strings, tuples, etc., and n represents the number of selected unique elements.

In [1]: import random
​
In [2]: str = "Hello World!"
        random.sample(str, 3)
Out[2]: ['r', 'l', 'H']
​
In [3]: lst = ['You', 'need', 'Python', '!', str, 3, 5, 8]
        random.sample(lst, 3)
Out[3]: [8, 'Hello World!', 'You']
​
In [4]: tup = ('You', 'need', 'Python', '!', str, 3, 5, 8, lst)
        random.sample(tup, 3)
Out[4]: [3, ['You', 'need', 'Python', '!', 'Hello World!', 3, 5, 8], '!']

2. Randomly generate and specify multiple unique numbers within the specified range

Using random.sample() and range() functions, it is very convenient to randomly generate a specified number of unique numbers within a specified range.

In [5]: num = range(100)
        random.sample(num, 10)
Out[5]: [47, 73, 48, 53, 36, 18, 99, 49, 55, 1]

3. Use random.choice() to randomly select an element

The random.choice() function can randomly select an element from a set of lists, strings, tuples, etc.

The sentence structure is:

random.choice(sep)

Among them, sep represents a collection or sequence of lists, strings, tuples, etc.

In [6]: lst = ['You', 'need', 'Python', '!', str, 3, 5, 8]
        random.choice(lst)
Out[6]: 'Python'

Because the case of strings and tuples as sequences is similar to the introduction of the random.sample() function, the types of all collections are not given as examples every time. The following examples are also the same.

4. Randomly select random unique elements

The random function can be used to easily select random elements, but be careful not to exceed the length of the sequence, so use the len() function as the upper limit of the random number.

In [7]: lst = ['You', 'need', 'Python', '!', str, 3, 5, 8]
        random.sample(lst, random.randint(0,len(lst)))
Out[7]: [8, 'Python', 'need', 'Hello World!', 'You']

Because this is the first article in the "Python in practice" series of articles, please forgive me for the bad writing, and please give us your valuable comments. Thank you!

 

 


Thanks for reading this article! If you have any questions, please leave a message and discuss together ^_^

Welcome to scan the QR code below, follow the "Yesu Python" official account, and read the other articles in the "Python in practice" series.

Also talk about Python-a learning and sharing area for Python lovers

 

Guess you like

Origin blog.csdn.net/mnpy2019/article/details/98852241