Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37700 > unrolled thread
| Started by | MRAB <python@mrabarnett.plus.com> |
|---|---|
| First post | 2013-01-25 23:45 +0000 |
| Last post | 2013-01-25 23:45 +0000 |
| 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 MRAB <python@mrabarnett.plus.com> - 2013-01-25 23:45 +0000
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-01-25 23:45 +0000 |
| Subject | Re: Retrieving an object from a set |
| Message-ID | <mailman.1060.1359157543.2939.python-list@python.org> |
On 2013-01-25 23:14, Arnaud Delobelle 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.
> Here is a simple illustration with tuples (my actual scenario is not
> with tuples but with a custom class):
>
>>>> y = (1, 2, 3) # This is the 'hidden object'
>>>> S = set([y] + range(10000))
>>>> x = (1, 2, 3)
>>>> x in S
> True
>>>> x is y
> False
>
You could first limit the search to only those which it could be:
S & set([y])
A search would be:
>>> f = [m for m in S & set([y]) if m is y][0]
>>> f is y
True
Back to top | Article view | comp.lang.python
csiph-web