[Tutor] Set changing order of items?

Python python at venix.com
Fri Jan 19 23:53:28 CET 2007


On Fri, 2007-01-19 at 20:05 +0000, Adam Cripps wrote:
> On 1/19/07, Simon Brunning <simon at brunningonline.net> wrote:
> > On 1/19/07, Adam Cripps <kabads at gmail.com> wrote:
> > > I'm adding strings to a Set to prevent duplicates. However, the
> > > strings are meant to be in the correct order. When I add the string to
> > > the Set, the order seems to change (and I don't seem to be able to
> > > predict what order they are in).
> >
> > Sets, like dictionaries, hold their items in an arbitrary order - see
> > <http://tinyurl.com/a2nkg>.
> 
> OK - thanks for that - so it seems that using a Set will complicate
> the matter more.
> 
> Really, I want to have a set of sets which won't have any duplicates.
> An example might look something ilke this:
> 
> sums = [['1) 10 + 2 =', '12'], ['2) 13 + 4 =', '17']]
> return sums

It looks like you have embedded the numbering into your strings.  If the
numbers were removed from the strings would you still care about keeping
the strings in order?

>>> sums = set([('10 + 2 =', '12'), ('13 + 4 =', '17')])

I changed the inside lists of pairs to tuples of pairs

>>> for ndx,(query,answer) in enumerate(sums):
...     print "%d) %s %s" % (ndx+1, query, answer)
... 
1) 10 + 2 = 12
2) 13 + 4 = 17


> 
> How might I achieve this list, without the duplicates (the duplicate
> bit is the bit I'm stuck on).
> 
> Adam
-- 
Lloyd Kvam
Venix Corp



More information about the Tutor mailing list