[Tutor] How to get at the list that set() seems to produce?
Dick Moores
rdm at rcblue.com
Thu Sep 28 20:31:33 CEST 2006
I'm very interested in the data type, set.
Python 2.5:
>>> lst = [9,23,45,9,45,78,23,78]
>>> set(lst)
set([9, 45, 78, 23])
>>> s = "etywtqyertwytqywetrtwyetrqywetry"
>>> set(s)
set(['e', 'q', 'r', 't', 'w', 'y'])
>>>
I'm wondering if there isn't a way to get at what seems to be the
list of unique elements set() seems to produce. For example, I would
think it might be useful if the "list" of set([9, 45, 78, 23]) could
be extracted, for sorting, taking the mean, etc. And it might be nice
if the "list" of set(['e', 'q', 'r', 't', 'w', 'y']) could be
converted into the sorted string, "eqrtwy". But note:
>>> set(['e', 'q', 'r', 't', 'w', 'y'])[:]
Traceback (most recent call last):
File "<string>", line 1, in <string>
TypeError: 'set' object is unsubscriptable
>>> set(['e', 'q', 'r', 't', 'w', 'y'])[0]
Traceback (most recent call last):
File "<string>", line 1, in <string>
TypeError: 'set' object is unindexable
>>>
To be sure I'm clear, I'm not asking how to do the above things in
other ways (I know how)--I'm just wondering why set() was set up
(pun) so they can't be done with its help. Or can they?
Dick Moores
More information about the Tutor
mailing list