fast way to filter a set?

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

    fast way to filter a set?

    I know I can do things like

    s=Set(range(1,1 1))
    s=Set(filter(la mbda x:x%2==0,s))

    But this seems a bit slow since filter returns a list which then must
    be converted back to a set. Any tips? Thanks!
  • Nicola Mingotti

    #2
    Re: fast way to filter a set?

    fortepianissimo @yahoo.com.tw (Fortepianissim o) writes:
    [color=blue]
    > I know I can do things like
    >
    > s=Set(range(1,1 1))
    > s=Set(filter(la mbda x:x%2==0,s))
    >
    > But this seems a bit slow since filter returns a list which then must
    > be converted back to a set. Any tips? Thanks![/color]

    Can you ?
    I tried python2.3 and 2.2 and it replies
    : "NameError: name 'Set' is not defined" .

    There is a PEP but it seems it's steel 'under consideration'.
    This PEP proposes adding a Set module to the standard Python library, and to then make sets a built-in Python type if that module is widely used. After explaining why sets are desirable, and why the common idiom of using dictionaries in their place is ...


    May be , if this proposal is accepted you
    one day could write :
    { x for x in S if x%2 == 0 } # S be a set .

    bye.


    Comment

    • Skip Montanaro

      #3
      Re: fast way to filter a set?


      fortepianissimo > I know I can do things like
      fortepianissimo > s=Set(range(1,1 1))
      fortepianissimo > s=Set(filter(la mbda x:x%2==0,s))

      fortepianissimo > But this seems a bit slow since filter returns a list
      fortepianissimo > which then must be converted back to a set. Any tips?

      The only thing which comes to mind is:
      [color=blue][color=green][color=darkred]
      >>> s = sets.Set(range( 1,11))
      >>> s[/color][/color][/color]
      Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])[color=blue][color=green][color=darkred]
      >>> keys = list(s)
      >>> dummy = [s.discard(x) for x in keys if x%2]
      >>> s[/color][/color][/color]
      Set([2, 4, 6, 8, 10])

      You still create a list, but don't create a second set.

      Skip

      Comment

      • Peter Otten

        #4
        Re: fast way to filter a set?

        Nicola Mingotti wrote:
        [color=blue]
        > fortepianissimo @yahoo.com.tw (Fortepianissim o) writes:
        >[color=green]
        >> I know I can do things like
        >>
        >> s=Set(range(1,1 1))
        >> s=Set(filter(la mbda x:x%2==0,s))
        >>
        >> But this seems a bit slow since filter returns a list which then must
        >> be converted back to a set. Any tips? Thanks![/color]
        >
        > Can you ?
        > I tried python2.3 and 2.2 and it replies
        > : "NameError: name 'Set' is not defined" .[/color]

        In 2.3 it works if you do

        from sets import Set

        first.

        Peter

        Comment

        • Peter Otten

          #5
          Re: fast way to filter a set?

          Fortepianissimo wrote:
          [color=blue]
          > I know I can do things like
          >
          > s=Set(range(1,1 1))
          > s=Set(filter(la mbda x:x%2==0,s))
          >
          > But this seems a bit slow since filter returns a list which then must
          > be converted back to a set. Any tips? Thanks![/color]

          The Set constructor accepts any iterable, so you can do it all with
          iterators instead of temporary lists:

          from sets import Set
          from itertools import ifilter

          s = Set(range(1, 11))
          print Set(ifilter(lam bda x: x % 2 == 0, s))

          Peter

          Comment

          Working...