Home  >  Article  >  Backend Development  >  How to randomly select elements from a list in python

How to randomly select elements from a list in python

不言
不言Original
2018-04-09 09:27:324970browse

Below I will share with you a method of randomly selecting elements from a list in python. It has certain reference value and I hope it can be helpful to everyone.

Randomly select elements from list random.sample

##

import random
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = random.sample(a, 5)

Related recommendations:


How to achieve mutual conversion from str to list in python


The above is the detailed content of How to randomly select elements from a list in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn