1

I want to create a list and fill it with 15 zeros, then I want to change the 0 to 1 in 5 random spots of the list, so it has 10 zeros and 5 ones, here is what I tried

import random, time

dasos = []

for i in range(1, 16):
  dasos.append(0)

for k in range(1, 6):
  dasos[random.randint(0, 15)] = 1

Sometimes I would get anywhere from 0 to 5 ones but I want exactly 5 ones, if I add:

print(dasos) 

...to see my list I get:

IndexError: list assignment index out of range
4
  • Perhaps you'll be fine using random.sample. Commented Feb 27, 2016 at 19:49
  • Use randrange(0, 15) or randint(0, 14) Commented Feb 27, 2016 at 20:00
  • As @Gassa says, random.sample(range(15), 5) will give you 5 unique numbers between 0 and 14. Commented Feb 27, 2016 at 20:10
  • Another way to do it could be to first save all possible indices outside the loop (e.g. canditates = range(15)), shuffle this list with random.shuffle(candidates), and then use the first 5 numbers as your indices. Commented Feb 27, 2016 at 20:13

1 Answer 1

1

I think the best solution would be to use random.sample:

my_lst = [0 for _ in range(15)]

for i in random.sample(range(15), 5):
    my_lst[i] = 1

You could also consider using random.shuffle and use the first 5 entries:

my_lst = [0 for _ in range(15)]
candidates = list(range(15))
random.shuffle(candidates)

for i in candidates[0:5]:
    my_lst[i] = 1

TL;DR: Read the the Python random documentation, this can be done in multiple ways.

Sign up to request clarification or add additional context in comments.

6 Comments

I didn't suggest any of these things because clearly this would only cause confusion to the OP, which apparently is very new to programming. In my opinion, things like list-comprehensions are incredibly horrible for learning programming. My solution might not be on purpose the most pythonic one, but it is more intuitive for a beginner.
If you really want to learn how to program a today's computer, then learn C.
Note that the random.choice implementation has the same problem as the Questioner's original code: It might pick the same index more than once. The shuffle solution also needs to be tweaked to work in Python 3, where range returns a non-list iterable. Use candidates = list(range(15)) to ensure you get an actual list that can be shuffled.
@Blckknght Thanks for the comments. I was "sure" that random.choice popped the elements, but apparently my memory served me wrong :-) Also I didn't notice that the question was about Python 3, so thank you for notifying me.
@nbro The OP already use a method from the random library in his code. Pointing out that he's using the wrong method is obviously correct rather than trying to hack something together that works with his choice of method.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.