tags:

views:

112

answers:

1

Hi I have a List say 100 items, now i want a slice of say 6 items which should be randomly selected. Any way to do it in very simple simple concise statement???

This is what i came up with (but it will fetch in sequence)

 mylist #100 items
 N=100
 L=6
 start=random.randint(0,N-L);
 mylist[start:start+L]
+4  A: 
Azz
1. This does something different. 2. there is random.population for what you do.
THC4k
Something different to? The OPs code? The way I understood the question was that he/she wanted to select a group of random items from the list, not a slice from a random position in the list. But yes, random.sample looks good (that's what I found when looking for random.population in the docs).
Azz
whoops yeah I meant random.sample. The Ops code selects consecutive items, I assume that is intentional. But you're right, it could be either way ..
THC4k
This is the bit that confused me a little "now i want a slice of say 6 items which should be randomly selected", I sort of assumed that meant 6 random items in the list, but yeah, could be either way I guess.
Azz
Yes, Thanks everyone for your answer random.sample(mylist, L) is what i wanted
Markandey Singh