Finding a tuple in a tuple

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bg_ie@yahoo.com

    Finding a tuple in a tuple

    Hi,

    Lists say I have the following tuple -

    t1 = ("ONE","THREE", "SIX")

    and then the following tuples -

    t2 = ("ONE","TWO","T HREE")

    t3 = ("TWO","FOUR"," FIVE","SIX")

    t4 = ("TWO",)

    t5 = ("TWO","FIVE ")

    What I want to do is return true if any member of tuple t1 is found in
    the remaining tuples.

    Therefore -

    2) ("ONE","TWO","T HREE") : TRUE

    3) ("TWO","FOUR"," FIVE","SIX") : TRUE

    4) ("TWO",) FALSE

    5) ("TWO","FIVE ")

    How do I do this?

    Cheers,

    Barry.

  • Paul Rubin

    #2
    Re: Finding a tuple in a tuple

    bg_ie@yahoo.com writes:
    Lists say I have the following tuple -
    t1 = ("ONE","THREE", "SIX")
    t2 = ("ONE","TWO","T HREE")
    t3 = ("TWO","FOUR"," FIVE","SIX")
    t4 = ("TWO",)
    t5 = ("TWO","FIVE ")
    >
    What I want to do is return true if any member of tuple t1 is found in
    the remaining tuples.
    Convert them into sets and use the set intersection (&) operator,
    then convert to bool to represent whether the intersection is empty.

    print bool(set(t1) & set(t2))
    print bool(set(t1) & set(t3))
    print bool(set(t1) & set(t4))
    print bool(set(t1) & set(t5))

    Comment

    • Paul McGuire

      #3
      Re: Finding a tuple in a tuple

      On Feb 22, 3:05 am, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
      b...@yahoo.com writes:
      Lists say I have the following tuple -
      t1 = ("ONE","THREE", "SIX")
      t2 = ("ONE","TWO","T HREE")
      t3 = ("TWO","FOUR"," FIVE","SIX")
      t4 = ("TWO",)
      t5 = ("TWO","FIVE ")
      >
      What I want to do is return true if any member of tuple t1 is found in
      the remaining tuples.
      >
      Convert them into sets and use the set intersection (&) operator,
      then convert to bool to represent whether the intersection is empty.
      >
      print bool(set(t1) & set(t2))
      print bool(set(t1) & set(t3))
      print bool(set(t1) & set(t4))
      print bool(set(t1) & set(t5))
      A step further: use union to make a superset of t2-tN, then use & on
      this superset.

      setlist = [t2,t3,t4,t5]
      superset = reduce(set.unio n, map(set,setlist ) )
      print bool(t1 & superset)


      -- Paul

      Comment

      • Paul Rubin

        #4
        Re: Finding a tuple in a tuple

        "Paul McGuire" <ptmcg@austin.r r.comwrites:
        A step further: use union to make a superset of t2-tN, then use & on
        this superset.
        >
        setlist = [t2,t3,t4,t5]
        superset = reduce(set.unio n, map(set,setlist ) )
        print bool(t1 & superset)
        Well you do have to convert them to sets. Also I thought each
        intersection was wanted separately. Otherwise if we're getting this
        fancy, I guess I'd use (untested, uses new 2.5 "any" function):

        s1 = set(t1)
        print any((s1 & set(tn)) for tn in (t2,t3,t4,t5))

        which avoids creating a potentially big intermediate set, and which
        short-circuits (exits early) as soon as a match is found.

        Comment

        • Philipp Pagel

          #5
          Re: Finding a tuple in a tuple

          bg_ie@yahoo.com wrote:
          t1 = ("ONE","THREE", "SIX")
          t2 = ("ONE","TWO","T HREE")
          t3 = ("TWO","FOUR"," FIVE","SIX")
          t4 = ("TWO",)
          t5 = ("TWO","FIVE ")
          What I want to do is return true if any member of tuple t1 is found in
          the remaining tuples.
          Another way to go instead of using sets, although probably less elegant:
          >>True in [x in t1 for x in t2]
          True
          >>True in [x in t1 for x in t3]
          True
          >>True in [x in t1 for x in t4]
          False
          >>True in [x in t1 for x in t5]
          False

          cu
          Philipp

          --
          Dr. Philipp Pagel Tel. +49-8161-71 2131
          Dept. of Genome Oriented Bioinformatics Fax. +49-8161-71 2186
          Technical University of Munich

          Comment

          • Jussi Salmela

            #6
            Re: Finding a tuple in a tuple

            bg_ie@yahoo.com kirjoitti:
            Hi,
            >
            Lists say I have the following tuple -
            >
            t1 = ("ONE","THREE", "SIX")
            >
            and then the following tuples -
            >
            t2 = ("ONE","TWO","T HREE")
            >
            t3 = ("TWO","FOUR"," FIVE","SIX")
            >
            t4 = ("TWO",)
            >
            t5 = ("TWO","FIVE ")
            >
            What I want to do is return true if any member of tuple t1 is found in
            the remaining tuples.
            >
            Therefore -
            >
            2) ("ONE","TWO","T HREE") : TRUE
            >
            3) ("TWO","FOUR"," FIVE","SIX") : TRUE
            >
            4) ("TWO",) FALSE
            >
            5) ("TWO","FIVE ")
            >
            How do I do this?
            >
            Cheers,
            >
            Barry.
            >
            Another variation of the theme:

            #============== ======
            for t in (t2, t3, t4, t5):
            for x in t1:
            if x in t:
            print True
            break
            else: print False
            #============== ======


            HTH,
            Jussi

            Comment

            • Duncan Booth

              #7
              Re: Finding a tuple in a tuple

              Philipp Pagel <pDOTpagel@gsf. dewrote:
              Another way to go instead of using sets, although probably less elegant:
              >
              >>>True in [x in t1 for x in t2]
              True
              >>>True in [x in t1 for x in t3]
              True
              >>>True in [x in t1 for x in t4]
              False
              >>>True in [x in t1 for x in t5]
              False
              Slightly more elegant for Python 2.5 users:
              >>any(x in t1 for x in t2)
              True
              >>any(x in t1 for x in t3)
              True
              >>any(x in t1 for x in t4)
              False
              >>any(x in t1 for x in t5)
              False


              Comment

              Working...