Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
<pre>import random

longList = [ 0, 3, 4, 5, 6, 7, 8, 12, 15, 16, 17 ]

# Pick 2 unique numbers from longList, at random - any 2 numbers as long
# as they're not the same

uniques = [ longList[ random.randint( 0, len( longList ) - 1 ) ] ]

while len( uniques ) < 2:
    nextCandidate = random.randint( 0, len( longList ) - 1 )
    if longList[ nextCandidate ] != uniques[ 0 ]:
        uniques = uniques + [ longList[ nextCandidate ] ]

print(longList)
print("The unique numbers are", uniques[ 0 ], "and", uniques[ 1 ])


What I have tried:

I lost with this I have tried something but non works
Posted
Updated 12-Nov-17 23:00pm
v3

1 solution

First, dump that code of yours.
My suggested approach is:
1. convert the original list to a set which will eliminate duplicate elements.
2. Get the size of the set which is the answer.
How? check out your programming note or ask Google.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900