+ 3
How do I generate a list with <N> unique elements randomly selected from another (already existing) list?
Hello SoloLearners, I just have an idea to create a `list` which contains unique <N> (a user provided integer value) elements randomly selected from an existing `list`, but I'm not sure how to do that efficiently. I thought about using randint() to generate <N> indices, yes it worked, but there exists duplicates. Example: main_list = [ 1, 2, 3, 4, 5 ] N = 3 # need 3 unique elements Now I need to generate (using loop) something like ... [ 1, 4, 3 ] [ 2, 1, 5 ] ... And so on, where each generated `list` has <N> unique elements randomly selected from <main_list> Please rather not to provide me code. Instead point me in direction, or share me a link to learn from. Big Thanks ...
26 Respostas
+ 10
random.choices()
Or generate 3 random integers in the index range of the original list?
Or shuffle the list and always take the N first elements?
+ 8
Ipang ,
a bit late, but i like to share this code with you.
https://code.sololearn.com/cMAUc3DuKw35/?ref=app
+ 8
sarafoster ,
may be there is a misunderstanding about random.shuffle():
> shuffle() does *NOT* generate any random numbers, but it uses an existing iterable and *rearrange* the index positions of the iterable members.
+ 4
I know this probably does not fit your requirements, but given that most other solutions have been discussed, I pitch you UUIDs. :>
https://docs.python.org/3/library/uuid.html
The pros would be that it's trivial to generate them, can be generated randomly (via uuid4) and given its intended use cases, is virtually impossible to obtain duplicates. However, a UUID is 128 bits long, and reducing that in any way would greatly increase probability of collision.
https://code.sololearn.com/cVJUaBRX4oig/?ref=app
+ 3
Lothar
Thanks, that was the change I had in mind. I was about to try it cause now I realize one shuffle() call surely is far less of a work compared to multiple randint() calls in attempt to generate unique indices + calls to pop() to reduce possible duplicates : )
+ 3
Bob_Li
Does list.copy() make up shallow / deep copy? good idea with shuffling a local copy though š
That's what I was thinking of, cause I'd prefer to shuffle a copy and leave the main list intact ...
+ 3
Adeleke Samuel
Bright Osei
Please provide ONLY relevant answers/comments.
+ 3
Ipang shallow copy should be enough unless you're working with nested lists.
And yes, shuffle mutating the original list can be problematic if you are reusing the list, thus the need for copy.
+ 3
Thanks for the info Bob_Li
I got it clearly now ...
+ 3
Mirielle
i think one would have to start with a list of unique items in order to have unique shuffled items.
and creating a list of unique ordered numbers is easier than creating a list of unique random numbers.
Shuffling a unique sorted list is less costly than generating random numbers and checking if it was already used.
+ 2
D_Stark Do you mean the 2nd one?
+ 2
Ipang, I give you...
The shuffled list dealer:
comes with the following features:
1. works on a copy. does not mutate the original list.
2. guaranteed no duplication, since it generates a sequential slice of the list. If the original items are unique, the items in the slices are also unique. The slice length can be set from n=1 to n <= list length.
3. shuffle is also made as a default option, which can be turned off. To shuffle or not is your choice.
https://code.sololearn.com/cD94P5qG36wp/?ref=app
+ 2
Hatsy Rei
Hehehe you're right, it is not exactly what I was looking for.
However, it's an additional info for thread viewers š
+ 2
I made this as an example it works but memory efficiently not gaurenteed.
https://code.sololearn.com/cH5AaiKNuFlG/?ref=app
+ 1
in this case you could use, achieve the desired result by using the "random.sample" function from the "random" module in Python. "random.sample" allows you to randomly sample a specified number of unique elements from a list.
+ 1
Lisa most efficient š
+ 1
Lisa the 3rd one š, because it seems he wants a list of numbers which are shuffled without duplicates.
I would create a 2d array fill each row 0-10 and then shuffle each row down the column then take n first from each row.
+ 1
Big Thanks for the answers Jay Matthews Lisa ArsenicolupinIII Mirielle and D_Stark š
Y'all probably will be laughing out should you see how I did with the randint(), such a hassle LOL
Well, since we haven't got any reference links posted yet so far, guess I might as well be doing it.
https://docs.python.org/3/library/random.html
https://code.sololearn.com/c1pZbcxC4JZy/?ref=app
+ 1
Mirielle
Sorry I didn't get it. Which test case was that again? did you mean the <N>?
You're right about choices(), it does generate duplicates ...
Dunno about random(), haven't implemented it yet ...
+ 1
Mirielle
Well observed! but good news is, sample() seems to manage small <N> unlike choices() ...
My next idea is to generate some lists each containing <N> unique elements randomly selected from main list. Just stuck on what to do with the last list that contains leftovers only (less than <N> elements) ...
Partially done