I Googled it a lot but could not found it. I want to know the use of random.sample()
method and what does it give? When should it be used and some example usage.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
According to documentation:
Basically, it picks k unique random elements, a sample, from a sequence:
random.sample
works also directly from a range:In addition to sequences,
random.sample
works with sets too:However,
random.sample
doesn't work with arbitrary iterators:random.sample()
also works on textexample:
\n is also seen as a character so that can also be returned
you could use
random.sample()
to return random words from a text file if you first use the split methodexample: