Python Forum
Generate a list of numbers within a list of arbitrary numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generate a list of numbers within a list of arbitrary numbers
#1
Hi Guys,

Suppose I have a list of arbitrary numbers (23, 2, 49, 100, 101) and from this list, I want to randomly pick 3 unique numbers and store them inside a new list. I can’t use random.sample() function, as the function will generate the numbers based on a range of sequence numbers.

Can you please advise me on how to pick 3 unique numbers from a list of arbitrary numbers, and store them in a new list?

Thank you.
Reply
#2
I don't understand what the problem with using random.sample() is
from random import sample

population = (23, 2, 49, 100, 101)
my_sample = sample(population, 3)
print(my_sample)
Output:
[101, 100, 49] >>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Nov-07-2018, 12:30 PM)buran Wrote: I don't understand what the problem with using random.sample() is
from random import sample

population = (23, 2, 49, 100, 101)
my_sample = sample(population, 3)
print(my_sample)
Output:
[101, 100, 49] >>>

Thanks Buran. Maybe I misunderstood that random.sample() will need to specify “range” of sequence numbers as its parameter, in order for it to work.

Once again, thank you for your help.
Reply
#4
I think you were just confused by the example with range.
Note that if you have repeated numbers in the original list it's also possible to have repeating elements in the sample
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Nov-07-2018, 02:36 PM)buran Wrote: I think you were just confused by the example with range.
Note that if you have repeated numbers in the original list it's also possible to have repeating elements in the sample

Yup. Understand that repeated numbers may be picked up if there are duplicates in the population list.

Thanks Buran.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compile list of dictianories out of another list of dictianories by certain keys CatorCanulis 10 652 Jun-10-2021, 08:35 PM
Last Post: perfringo
  convert numbers into list lokesh 1 408 Jun-03-2021, 06:37 AM
Last Post: menator01
  Help with CV2 pytesseract detcet numbers korenron 0 383 Apr-29-2021, 02:48 PM
Last Post: korenron
  Calcolate the average of numbers from a .txt file francescomiles 2 704 Mar-27-2021, 02:43 PM
Last Post: francescomiles
  Raising numbers to power ** GJG 3 542 Mar-23-2021, 03:43 PM
Last Post: deanhystad
  Order a list with successive permutations based on another list yvrob 3 627 Mar-19-2021, 08:20 AM
Last Post: supuflounder
  Saving list in a list quest_ 3 748 Mar-10-2021, 09:58 AM
Last Post: quest_
Star Convert Bytearray into List using list() Shlok 2 514 Feb-18-2021, 10:44 AM
Last Post: deanhystad
  adding numbers in a list Nickd12 2 574 Jan-15-2021, 12:46 PM
Last Post: Serafim
  Adding List Element if Second part of the List Elements are the Same quest_ 3 630 Nov-25-2020, 04:33 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020