Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37699 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2013-01-25 16:37 -0700 |
| Last post | 2013-01-25 16:37 -0700 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Retrieving an object from a set Ian Kelly <ian.g.kelly@gmail.com> - 2013-01-25 16:37 -0700
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2013-01-25 16:37 -0700 |
| Subject | Re: Retrieving an object from a set |
| Message-ID | <mailman.1059.1359157062.2939.python-list@python.org> |
On Fri, Jan 25, 2013 at 4:30 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Fri, Jan 25, 2013 at 4:14 PM, Arnaud Delobelle <arnodel@gmail.com> wrote:
>> Dear Pythoneers,
>>
>> I've got a seemingly simple problem, but for which I cannot find a
>> simple solution.
>>
>> I have a set of objects (say S) containing an object which is equal to
>> a given object (say x). So
>>
>> x in S
>>
>> is true. So there is an object y in S which is equal to x. My
>> problem is how to retrieve y, without going through the whole set.
>
> You could use a dict.
>
>>>> y = (1, 2, 3)
>>>> S = {x: x for x in [y] + range(10000)}
>>>> x = (1, 2, 3)
>>>> x in S
> True
>>>> x is y
> False
>>>> S[x] is y
> True
Or you could use a set intersection:
>>> S = set([y] + list(range(10000)))
>>> S.intersection([x]).pop()
(1, 2, 3)
In my testing, the time needed for this is small and does not seem to
depend on the size of the set.
Back to top | Article view | comp.lang.python
csiph-web