classification
Title: random.choices(seq, k)
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: christian.heimes, mark.dickinson, pitrou, rhettinger
Priority: normal Keywords: patch

Created on 2013-07-09 13:11 by christian.heimes, last changed 2013-07-11 10:23 by christian.heimes. This issue is now closed.

Files
File name Uploaded Description Edit
random_choices.patch christian.heimes, 2013-07-09 13:11 review
Messages (4)
msg192747 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-09 13:11
The random module has no method to obvious way to select k elements from a sequence without enforcing unique values in the result set. It's a rather useful feature for salts, randomly generated passwords and other applications. See #18405

The patch implements random.choices(seq, k) -> list
msg192781 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-07-09 23:02
How about [random.choice(seq) for i in range(k)]? Isn't it obvious enough?
msg192847 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-07-11 07:21
I concur with Antoine.  A list comprehension is the standard, obvious idiom for making a list from repeated function calls.  

Putting sampling without replacement in its own method makes the operation more opaque than a plain list comprehension (which doesn't need documentation to be clear about how many calls are made, what the output type is, its relationship to random.choice, whether k can be larger than the population, etc).
msg192860 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-11 10:23
You have convinced me. It's not worth yet another API method.
History
Date User Action Args
2013-08-31 09:24:20mark.dickinsonlinkissue18888 superseder
2013-07-11 10:23:14christian.heimessetstatus: open -> closed
resolution: rejected
messages: + msg192860

stage: patch review -> resolved
2013-07-11 07:21:28rhettingersetmessages: + msg192847
2013-07-09 23:02:29pitrousetnosy: + pitrou
messages: + msg192781
2013-07-09 22:05:15rhettingersetassignee: rhettinger
2013-07-09 13:11:25christian.heimescreate