Re: [Tutor] Removing duplicates in a list with a fixed length of items in the list.
Why not sorting the items and throw out all multiples until you reach the fixed length ? Klaus Ramelow ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Removing duplicates in a list with a fixed length of items in the list.
Adam Cripps wrote: > I've not worked with Sets before but will give it a go. Is there a way > of turning the Set back to a list? list(mySet) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Removing duplicates in a list with a fixed length of items in the list.
On 1/11/07, Adam Cripps <[EMAIL PROTECTED]> wrote: > On 1/11/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > > Adam Cripps wrote: > > > I have a list which comprises of simple random arithmetic problems for > > > teachers to give to their pupils. This list must be a set length (if > > > the teacher asks for 10 questions, they should get 10 questions), but > > > should not have any duplicates. > > > > > > I've seen the use of sets, but this reduces the size of the list, when > > > I have to have a fixed length. > > > > > > Is there a quick way of checking if a string is already within a list > > > without iterating over the items of the list (which will slow the > > > application down slightly)? > > > > Hmm. If you are trying to pick 10 elements at random from a larger list, > > use random.sample(problems, 10) > > > > If for some reason that doesn't work for you (maybe you are creating > > problems on the fly?) you could build a set in a loop, adding items > > until it is the size you want: > > problemSet = set() > > while len(problemSet) < 10: > ># do something to add a (possibly) new problem to the set > > > I'm creating them on the fly - so this suggestion seems to be the sensible way forward. I've not worked with Sets before but will give it a go. Is there a way of turning the Set back to a list? TIA Adam -- http://www.monkeez.org PGP key: 0x7111B833 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Removing duplicates in a list with a fixed length of items in the list.
Adam Cripps wrote: > I have a list which comprises of simple random arithmetic problems for > teachers to give to their pupils. This list must be a set length (if > the teacher asks for 10 questions, they should get 10 questions), but > should not have any duplicates. > > I've seen the use of sets, but this reduces the size of the list, when > I have to have a fixed length. > > Is there a quick way of checking if a string is already within a list > without iterating over the items of the list (which will slow the > application down slightly)? Hmm. If you are trying to pick 10 elements at random from a larger list, use random.sample(problems, 10) If for some reason that doesn't work for you (maybe you are creating problems on the fly?) you could build a set in a loop, adding items until it is the size you want: problemSet = set() while len(problemSet) < 10: # do something to add a (possibly) new problem to the set Finally, unless your lists are huge (thousands of items, at a guess) or you are doing this very many times (thousands of times), you will not notice the time it takes to search the list for duplicates. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Removing duplicates in a list with a fixed length of items in the list.
I have a list which comprises of simple random arithmetic problems for teachers to give to their pupils. This list must be a set length (if the teacher asks for 10 questions, they should get 10 questions), but should not have any duplicates. I've seen the use of sets, but this reduces the size of the list, when I have to have a fixed length. Is there a quick way of checking if a string is already within a list without iterating over the items of the list (which will slow the application down slightly)? TIA Adam -- http://www.monkeez.org PGP key: 0x7111B833 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor