sets and subsets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bart Nessux

    sets and subsets

    By using lists, I can create sets of number. Suppose I have three lists.
    One list is the super-set, one is a set that contains all the numbers
    (just like the super-set) and the last is sub-set of the super-set. For
    example:

    a = [1,2,3,4,5] # The super-set.
    b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
    c = [2,4] # A sub-set

    I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
    this would make the sets look like this:

    a = [1,2,3,4,5]
    b = [1,3,5]
    c = [2,4]

    How do I test set c to find what it contains and then look at set b to
    see if it contains any of those same numbers, and if so, remove them.

  • Amy G

    #2
    Re: sets and subsets

    I am sure there is a much more elegant way to do this, but here is one
    solution.

    for item in c:
    if b.count(item) > 0:
    b.remove(item)


    "Bart Nessux" <bart_nessux@ho tmail.com> wrote in message
    news:c0e4g3$r1$ 1@solaris.cc.vt .edu...[color=blue]
    > By using lists, I can create sets of number. Suppose I have three lists.
    > One list is the super-set, one is a set that contains all the numbers
    > (just like the super-set) and the last is sub-set of the super-set. For
    > example:
    >
    > a = [1,2,3,4,5] # The super-set.
    > b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
    > c = [2,4] # A sub-set
    >
    > I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
    > this would make the sets look like this:
    >
    > a = [1,2,3,4,5]
    > b = [1,3,5]
    > c = [2,4]
    >
    > How do I test set c to find what it contains and then look at set b to
    > see if it contains any of those same numbers, and if so, remove them.
    >[/color]


    Comment

    • Elaine Jackson

      #3
      Re: sets and subsets

      b=[x for x in b if x not in c]

      "Bart Nessux" <bart_nessux@ho tmail.com> wrote in message
      news:c0e4g3$r1$ 1@solaris.cc.vt .edu...
      | By using lists, I can create sets of number. Suppose I have three lists.
      | One list is the super-set, one is a set that contains all the numbers
      | (just like the super-set) and the last is sub-set of the super-set. For
      | example:
      |
      | a = [1,2,3,4,5] # The super-set.
      | b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
      | c = [2,4] # A sub-set
      |
      | I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
      | this would make the sets look like this:
      |
      | a = [1,2,3,4,5]
      | b = [1,3,5]
      | c = [2,4]
      |
      | How do I test set c to find what it contains and then look at set b to
      | see if it contains any of those same numbers, and if so, remove them.
      |


      Comment

      • Amy G

        #4
        Re: sets and subsets

        There you go... list comprehension. That is definatly nicer to look at.


        "Elaine Jackson" <elainejackson7 355@home.com> wrote in message
        news:zcxWb.4640 65$JQ1.270296@p d7tw1no...[color=blue]
        > b=[x for x in b if x not in c]
        >
        > "Bart Nessux" <bart_nessux@ho tmail.com> wrote in message
        > news:c0e4g3$r1$ 1@solaris.cc.vt .edu...
        > | By using lists, I can create sets of number. Suppose I have three lists.
        > | One list is the super-set, one is a set that contains all the numbers
        > | (just like the super-set) and the last is sub-set of the super-set. For
        > | example:
        > |
        > | a = [1,2,3,4,5] # The super-set.
        > | b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
        > | c = [2,4] # A sub-set
        > |
        > | I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
        > | this would make the sets look like this:
        > |
        > | a = [1,2,3,4,5]
        > | b = [1,3,5]
        > | c = [2,4]
        > |
        > | How do I test set c to find what it contains and then look at set b to
        > | see if it contains any of those same numbers, and if so, remove them.
        > |
        >
        >[/color]


        Comment

        • Lee Harr

          #5
          Re: sets and subsets

          On 2004-02-11, Bart Nessux <bart_nessux@ho tmail.com> wrote:[color=blue]
          > By using lists, I can create sets of number. Suppose I have three lists.
          > One list is the super-set, one is a set that contains all the numbers
          > (just like the super-set) and the last is sub-set of the super-set. For
          > example:
          >
          > a = [1,2,3,4,5] # The super-set.
          > b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
          > c = [2,4] # A sub-set
          >
          > I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
          > this would make the sets look like this:
          >
          > a = [1,2,3,4,5]
          > b = [1,3,5]
          > c = [2,4]
          >
          > How do I test set c to find what it contains and then look at set b to
          > see if it contains any of those same numbers, and if so, remove them.
          >[/color]

          [color=blue][color=green][color=darkred]
          >>> from sets import Set
          >>> a = Set([1, 2, 3, 4, 5])
          >>> b = Set([1, 2, 3, 4, 5])
          >>> c = Set([2, 4])
          >>> s = b - c
          >>> s[/color][/color][/color]
          Set([1, 3, 5])

          Comment

          • Bart Nessux

            #6
            Re: sets and subsets

            Works great too. Thanks to all for the info.

            Amy G wrote:[color=blue]
            > There you go... list comprehension. That is definatly nicer to look at.
            >
            >
            > "Elaine Jackson" <elainejackson7 355@home.com> wrote in message
            > news:zcxWb.4640 65$JQ1.270296@p d7tw1no...
            >[color=green]
            >>b=[x for x in b if x not in c]
            >>
            >>"Bart Nessux" <bart_nessux@ho tmail.com> wrote in message
            >>news:c0e4g3$r 1$1@solaris.cc. vt.edu...
            >>| By using lists, I can create sets of number. Suppose I have three lists.
            >>| One list is the super-set, one is a set that contains all the numbers
            >>| (just like the super-set) and the last is sub-set of the super-set. For
            >>| example:
            >>|
            >>| a = [1,2,3,4,5] # The super-set.
            >>| b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
            >>| c = [2,4] # A sub-set
            >>|
            >>| I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
            >>| this would make the sets look like this:
            >>|
            >>| a = [1,2,3,4,5]
            >>| b = [1,3,5]
            >>| c = [2,4]
            >>|
            >>| How do I test set c to find what it contains and then look at set b to
            >>| see if it contains any of those same numbers, and if so, remove them.
            >>|
            >>
            >>[/color]
            >
            >
            >[/color]

            Comment

            • Peter Otten

              #7
              Re: sets and subsets

              Bart Nessux wrote:
              [color=blue]
              > By using lists, I can create sets of number. Suppose I have three lists.
              > One list is the super-set, one is a set that contains all the numbers
              > (just like the super-set) and the last is sub-set of the super-set. For
              > example:
              >
              > a = [1,2,3,4,5] # The super-set.
              > b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
              > c = [2,4] # A sub-set
              >
              > I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
              > this would make the sets look like this:
              >
              > a = [1,2,3,4,5]
              > b = [1,3,5]
              > c = [2,4]
              >
              > How do I test set c to find what it contains and then look at set b to
              > see if it contains any of those same numbers, and if so, remove them.[/color]

              You want set operations, so why would you use lists?
              [color=blue][color=green][color=darkred]
              >>> from sets import Set
              >>> a = Set([1,2,3,4,5])
              >>> c = Set([2,4])
              >>> b = a - c
              >>> b[/color][/color][/color]
              Set([1, 3, 5])

              Peter

              Comment

              • Bart Nessux

                #8
                Re: sets and subsets

                Peter Otten wrote:[color=blue]
                > Bart Nessux wrote:
                >
                >[color=green]
                >>By using lists, I can create sets of number. Suppose I have three lists.
                >>One list is the super-set, one is a set that contains all the numbers
                >>(just like the super-set) and the last is sub-set of the super-set. For
                >>example:
                >>
                >>a = [1,2,3,4,5] # The super-set.
                >>b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
                >>c = [2,4] # A sub-set
                >>
                >>I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
                >>this would make the sets look like this:
                >>
                >>a = [1,2,3,4,5]
                >>b = [1,3,5]
                >>c = [2,4]
                >>
                >>How do I test set c to find what it contains and then look at set b to
                >>see if it contains any of those same numbers, and if so, remove them.[/color]
                >
                >
                > You want set operations, so why would you use lists?[/color]

                All my data are in lists:

                inputFile = file('ips.txt', 'r') #Super-set
                include = inputFile.readl ines()
                inputFile.close ()

                # The file below is compiled manually by hand... add IPs to it
                # whenever you want to exclude them from IP_protection.
                readFile = file('excluded_ ips.txt', 'r') #Sub-set to exclude
                exclude = readFile.readli nes()
                readFile.close( )

                include = [x for x in include if x not in exclude] #Magic of Elaine

                outputFile = file('pruned_ip s.txt' , 'w')
                for i in include:
                print>> outputFile, i,
                outputFile.clos e()




                Comment

                • Aahz

                  #9
                  Re: sets and subsets

                  [content at *bottom*]

                  In article <c0e890$6hu$1@s olaris.cc.vt.ed u>,
                  Bart Nessux <bart_nessux@ho tmail.com> wrote:[color=blue]
                  >Works great too. Thanks to all for the info.
                  >
                  >Amy G wrote:[color=green]
                  >> There you go... list comprehension. That is definatly nicer to look at.
                  >>
                  >>
                  >> "Elaine Jackson" <elainejackson7 355@home.com> wrote in message
                  >> news:zcxWb.4640 65$JQ1.270296@p d7tw1no...
                  >>[color=darkred]
                  >>>b=[x for x in b if x not in c]
                  >>>
                  >>>"Bart Nessux" <bart_nessux@ho tmail.com> wrote in message
                  >>>news:c0e4g3$ r1$1@solaris.cc .vt.edu...
                  >>>| By using lists, I can create sets of number. Suppose I have three lists.
                  >>>| One list is the super-set, one is a set that contains all the numbers
                  >>>| (just like the super-set) and the last is sub-set of the super-set. For
                  >>>| example:
                  >>>|
                  >>>| a = [1,2,3,4,5] # The super-set.
                  >>>| b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
                  >>>| c = [2,4] # A sub-set
                  >>>|
                  >>>| I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
                  >>>| this would make the sets look like this:
                  >>>|
                  >>>| a = [1,2,3,4,5]
                  >>>| b = [1,3,5]
                  >>>| c = [2,4]
                  >>>|
                  >>>| How do I test set c to find what it contains and then look at set b to
                  >>>| see if it contains any of those same numbers, and if so, remove them.
                  >>>|[/color][/color][/color]

                  A: Because it messes up the order in which people normally read text.
                  Q: Why is top-posting such a bad thing?
                  A: Top-posting.
                  Q: What is the most annoying thing on usenet?
                  --
                  Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

                  "Argue for your limitations, and sure enough they're yours." --Richard Bach

                  Comment

                  • Peter Otten

                    #10
                    Re: sets and subsets

                    Bart Nessux wrote:
                    [color=blue]
                    > Peter Otten wrote:[color=green]
                    >> Bart Nessux wrote:
                    >>
                    >>[color=darkred]
                    >>>By using lists, I can create sets of number. Suppose I have three lists.
                    >>>One list is the super-set, one is a set that contains all the numbers
                    >>>(just like the super-set) and the last is sub-set of the super-set. For
                    >>>example:
                    >>>
                    >>>a = [1,2,3,4,5] # The super-set.
                    >>>b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
                    >>>c = [2,4] # A sub-set
                    >>>
                    >>>I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
                    >>>this would make the sets look like this:
                    >>>
                    >>>a = [1,2,3,4,5]
                    >>>b = [1,3,5]
                    >>>c = [2,4]
                    >>>
                    >>>How do I test set c to find what it contains and then look at set b to
                    >>>see if it contains any of those same numbers, and if so, remove them.[/color]
                    >>
                    >>
                    >> You want set operations, so why would you use lists?[/color]
                    >
                    > All my data are in lists:[/color]

                    All my beer is in sieves.
                    [color=blue]
                    >
                    > inputFile = file('ips.txt', 'r') #Super-set
                    > include = inputFile.readl ines()
                    > inputFile.close ()
                    >
                    > # The file below is compiled manually by hand... add IPs to it
                    > # whenever you want to exclude them from IP_protection.
                    > readFile = file('excluded_ ips.txt', 'r') #Sub-set to exclude
                    > exclude = readFile.readli nes()
                    > readFile.close( )
                    >
                    > include = [x for x in include if x not in exclude] #Magic of Elaine
                    >
                    > outputFile = file('pruned_ip s.txt' , 'w')
                    > for i in include:
                    > print>> outputFile, i,
                    > outputFile.clos e()[/color]

                    (untested)
                    from sets import set

                    inputFile = file('ips.txt', 'r') #Super-set
                    include = Set(inputFile.r eadlines())
                    inputFile.close ()

                    readFile = file('excluded_ ips.txt', 'r') #Sub-set to exclude
                    exclude = Set(readFile.re adlines())
                    readFile.close( )

                    # No Magic of Elaine

                    outputFile = file('pruned_ip s.txt' , 'w')
                    for i in include - exclude:
                    print >> outputFile, i,
                    outputFile.clos e()

                    Peter

                    Comment

                    • François Pinard

                      #11
                      Re: sets and subsets

                      [Peter Otten]
                      [color=blue]
                      > (untested)
                      > from sets import set[/color]
                      [color=blue]
                      > inputFile = file('ips.txt', 'r') #Super-set
                      > include = Set(inputFile.r eadlines())
                      > inputFile.close ()[/color]
                      [color=blue]
                      > readFile = file('excluded_ ips.txt', 'r') #Sub-set to exclude
                      > exclude = Set(readFile.re adlines())
                      > readFile.close( )[/color]
                      [color=blue]
                      > # No Magic of Elaine[/color]
                      [color=blue]
                      > outputFile = file('pruned_ip s.txt' , 'w')
                      > for i in include - exclude:
                      > print >> outputFile, i,
                      > outputFile.clos e()[/color]

                      Here is an equivalent, shorter algorithm (tested):

                      from sets import Set
                      file('pruned_ip s.txt', 'w').writelines (
                      Set(file('ips.t xt')) - Set(file('exclu ded_ips.txt')))

                      This code relies on `writelines' accepting an iterable, sets returning
                      their members whenever iterated, Set constructors accepting an iterable,
                      and files returning their lines whenever iterated. And of course, on
                      `close' rarely being needed in Python! :-)

                      The order of lines in the produced file is kind of random, however.

                      --
                      François Pinard http://www.iro.umontreal.ca/~pinard

                      Comment

                      • Bart Nessux

                        #12
                        Re: sets and subsets

                        Peter Otten wrote:
                        [color=blue]
                        > outputFile = file('pruned_ip s.txt' , 'w')
                        > for i in include - exclude:
                        > print >> outputFile, i,
                        > outputFile.clos e()[/color]

                        Wow! That makes a lot more sense than the list comprehension stuff. I think
                        I'll use it. Thanks!

                        Comment

                        • Bart Nessux

                          #13
                          Re: sets and subsets

                          François Pinard wrote:[color=blue]
                          > [Peter Otten]
                          >
                          >[color=green]
                          >>(untested)
                          >>from sets import set[/color]
                          >
                          >[color=green]
                          >>inputFile = file('ips.txt', 'r') #Super-set
                          >>include = Set(inputFile.r eadlines())
                          >>inputFile.clo se()[/color]
                          >
                          >[color=green]
                          >>readFile = file('excluded_ ips.txt', 'r') #Sub-set to exclude
                          >>exclude = Set(readFile.re adlines())
                          >>readFile.clos e()[/color]
                          >
                          >[color=green]
                          >># No Magic of Elaine[/color]
                          >
                          >[color=green]
                          >>outputFile = file('pruned_ip s.txt' , 'w')
                          >>for i in include - exclude:
                          >> print >> outputFile, i,
                          >>outputFile.cl ose()[/color]
                          >
                          >
                          > Here is an equivalent, shorter algorithm (tested):
                          >
                          > from sets import Set
                          > file('pruned_ip s.txt', 'w').writelines (
                          > Set(file('ips.t xt')) - Set(file('exclu ded_ips.txt')))
                          >
                          > This code relies on `writelines' accepting an iterable, sets returning
                          > their members whenever iterated, Set constructors accepting an iterable,
                          > and files returning their lines whenever iterated. And of course, on
                          > `close' rarely being needed in Python! :-)
                          >
                          > The order of lines in the produced file is kind of random, however.
                          >[/color]

                          Wow! Sets are awesome. I was thinking in terms of lists. A list is like
                          a set and a set is like a list, but depending on the task at hand, they
                          have very different applications. Sets work great when one has a
                          super-set and two sub-sets that need to be compared and modified based
                          on what they contain and what the super-set contains.

                          Sets are straight-forward and easy to use too... I can always tell when
                          I'm trying to do something with a tool that wasn't designed to do what
                          I'm attempting to do (in this case lists). The task becomes complex and
                          tedious. Forget about trying to read the code a couple of weeks from now.

                          Thanks to all for the info on sets!

                          Comment

                          • Dave K

                            #14
                            Re: sets and subsets

                            On Wed, 11 Feb 2004 18:57:15 -0500 in comp.lang.pytho n, François
                            Pinard <pinard@iro.umo ntreal.ca> wrote:
                            [color=blue]
                            >[Peter Otten]
                            >[color=green]
                            >> (untested)
                            >> from sets import set[/color]
                            >[color=green]
                            >> inputFile = file('ips.txt', 'r') #Super-set
                            >> include = Set(inputFile.r eadlines())
                            >> inputFile.close ()[/color]
                            >[color=green]
                            >> readFile = file('excluded_ ips.txt', 'r') #Sub-set to exclude
                            >> exclude = Set(readFile.re adlines())
                            >> readFile.close( )[/color]
                            >[color=green]
                            >> # No Magic of Elaine[/color]
                            >[color=green]
                            >> outputFile = file('pruned_ip s.txt' , 'w')
                            >> for i in include - exclude:
                            >> print >> outputFile, i,
                            >> outputFile.clos e()[/color]
                            >
                            >Here is an equivalent, shorter algorithm (tested):
                            >
                            >from sets import Set
                            >file('pruned_i ps.txt', 'w').writelines (
                            > Set(file('ips.t xt')) - Set(file('exclu ded_ips.txt')))
                            >
                            >This code relies on `writelines' accepting an iterable, sets returning
                            >their members whenever iterated, Set constructors accepting an iterable,
                            >and files returning their lines whenever iterated. And of course, on
                            >`close' rarely being needed in Python! :-)
                            >
                            >The order of lines in the produced file is kind of random, however.[/color]

                            That's very compact and neat, but for completeness I'd like to point
                            out that it could also be written (more clumsily) in one line with
                            list comprehensions, retaining the same order of elements as in the
                            original list:

                            file('pruned_ip s.txt', 'w').writelines ([ip for ip in file('ips.txt')
                            if ip not in file('excluded_ ips.txt')])

                            Of course, your example using sets is much clearer, so I prefer that.

                            Dave


                            Comment

                            • Tim Roberts

                              #15
                              Re: sets and subsets

                              aahz@pythoncraf t.com (Aahz) wrote:[color=blue]
                              >
                              >[content at *bottom*]
                              >...
                              >A: Because it messes up the order in which people normally read text.
                              >Q: Why is top-posting such a bad thing?
                              >A: Top-posting.
                              >Q: What is the most annoying thing on usenet?[/color]

                              You know, it may be time for this crusade to end. What you say is entirely
                              valid if a single Usenet post is read out of context. However, when using
                              a threaded newsreader, as 90% of us do, top-posting allows me to read the
                              new content without moving my eyes. I just press N, N, N to move to the
                              next message and scan the new content at the top of the message. In this
                              particular thread, the content quickly grew larger than my newsreader's
                              preview pane, so bottom-posting requires me to move the focus to the
                              preview pane and scroll down to read.

                              Besides, the MOST annoying thing on Usenet is HTML posts.
                              --
                              - Tim Roberts, timr@probo.com
                              Providenza & Boekelheide, Inc.

                              Comment

                              Working...