Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

python - How to get 80% random elements from a list?

I have a list with integers. I want to choose 80% from them. For example, for [1,...,30] I want to keep 80% from the list with random so I get for example [1,...,32][3,7,19,21,25,29] (It means it deleted 3,7,19,21,25,29 values from the original list. How can I do it?

question from:https://stackoverflow.com/questions/65905051/how-to-get-80-random-elements-from-a-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
import random

random.sample(lst, k=round(len(lst) * 0.8))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...