Set overlapping

Chris Rebert clp2 at rebertia.com
Sun Mar 15 21:51:05 CET 2009


On Sun, Mar 15, 2009 at 1:41 PM, mattia <gervaz at gmail.com> wrote:
> How can I determine the common values found in two differents sets and
> then assign this values?
> E.g.
> dayset = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
> monthset = ["Jan", "Feb", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
> "Oct", "Nov", "Dec"]
> this are the "fixed and standard" sets.
> Then I have others sets that contains one value from the previous sets
> (one for each).
> exset1 = ["SS" ,"33" ,"err" ,"val" ,"Tue" ,"XYZ" ,"40444" ,"Jan" ,"2008"]
> exset2 = ["53" ,"hello" ,"YY" ,"43" ,"2009" ,"Sun" ,"Feb"]
> I want to find:
> day_guess1 = value in dayset that is in exset1
> day_guess2 = value in dayset that is in exset2
> the same with monthset
> For now, I've got:
> for d in dayset:
>    if d in exset1:
>        guess_day = d
>        break
> Is there something better?

Use Python's actual `set` type. You're looking for the 'intersection' operation:

dayset = set(("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"))
exset1 = set(("SS" ,"33" ,"err" ,"val" ,"Tue" ,"XYZ" ,"40444" ,"Jan" ,"2008"))
exset2 = set(("53" ,"hello" ,"YY" ,"43" ,"2009" ,"Sun" ,"Feb"))
print dayset & exset1 #=> set(['Tue'])
the_value = (dayset & exset1).pop()
print dayset & exset2 #=> set(['Sun'])
other_value = (dayset & exset2).pop()
print the_value, other_value #=> Tue Sun

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com



More information about the Python-list mailing list