mercredi 8 juin 2016

List with sublists inside

I want to create a list made in this way :

  1. the length of the list is a number that I decide, say s (pretty big, not exaggerated)

  2. The entries are all zeroes, apart from one, which has value n

  3. The entry with value n could be in the middle, but I want it to appear randomly in the list, i.e. if I run the program again (or with some kind of loop) it gives me a vector where the entry different from 0 is in another position. E.g. v1 = [0,0,n,0], v2 = [n,0,0,0], etc

How can I do that?

This is what I tried for a list s of size 4:

s = [0,0,0,0]
r = random.random()
intr = int(r*len(s))
s[intr] = n

Thanks




Aucun commentaire:

Enregistrer un commentaire