dimanche 21 janvier 2018

Determing length of a random list being altered

I have a list being accessed randomly within a list, to which I am adding an element. (note that the element has to be inserted randomly inside of the list, i.e. I don't want to insert at the end or start) For example:

myList = [[0, 1, 4, 7],[0, 3, 2, 7]]
toinsert = [5, 6]

for item in toinsert:
    random.choice(myList).insert(random.randint(1,len(`the list that got chosen`)-2), item)

I have tried using

choicelist = random.choice(myList)
choicelist.insert(randint(1,len(choicelist)))

but then I'm not sure how to put that back in where the original list was - considering it's a random list.

I know I could randomly choose an index for myList and use that method, but I was looking for a hopefully more Pythonic - and shorter - way of doing so.




Aucun commentaire:

Enregistrer un commentaire