Best way to handle large lists?

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

    #1

    Best way to handle large lists?

    I have a system that has a few lists that are very large (thousands or
    tens of thousands of entries) and some that are rather small. Many times
    I have to produce the difference between a large list and a small one,
    without destroying the integrity of either list. I was wondering if
    anyone has any recommendations on how to do this and keep performance
    high? Is there a better way than

    [ i for i in bigList if i not in smallList ]

    Thanks.
    Chaz
  • Duncan Booth

    #2
    Re: Best way to handle large lists?

    Chaz Ginger <cginboston@hot mail.comwrote:
    I have a system that has a few lists that are very large (thousands or
    tens of thousands of entries) and some that are rather small. Many times
    I have to produce the difference between a large list and a small one,
    without destroying the integrity of either list. I was wondering if
    anyone has any recommendations on how to do this and keep performance
    high? Is there a better way than
    >
    [ i for i in bigList if i not in smallList ]
    How about:

    smallSet = set(smallList)
    something = [ i for i in bigList if i not in smallSet ]

    Use timeit.py on some representative data to see what difference that
    makes.

    Comment

    • Paul Rubin

      #3
      Re: Best way to handle large lists?

      Chaz Ginger <cginboston@hot mail.comwrites:
      I have a system that has a few lists that are very large (thousands or
      tens of thousands of entries) and some that are rather small. Many times
      I have to produce the difference between a large list and a small one,
      without destroying the integrity of either list. I was wondering if
      anyone has any recommendations on how to do this and keep performance
      high? Is there a better way than
      >
      [ i for i in bigList if i not in smallList ]
      diff = list(set(bigLis t) - set(smallList))

      Note that doesn't get you the elements in any particular order.

      Comment

      • durumdara

        #4
        Re: Best way to handle large lists?

        Chaz Ginger írta:
        I have a system that has a few lists that are very large (thousands or
        tens of thousands of entries) and some that are rather small. Many times
        I have to produce the difference between a large list and a small one,
        without destroying the integrity of either list. I was wondering if
        anyone has any recommendations on how to do this and keep performance
        high? Is there a better way than
        >
        [ i for i in bigList if i not in smallList ]
        >
        Thanks.
        Chaz
        >
        Hi !

        If you have big list, you can use dbm like databases.
        They are very quick. BSDDB, flashdb, etc. See SleepyCat, or see python help.

        In is very slow in large datasets, but bsddb is use hash values, so it
        is very quick.
        The SleepyCat database have many extras, you can set the cache size and
        many other parameters.

        Or if you don't like dbm style databases, you can use SQLite. Also
        quick, you can use SQL commands.
        A little slower than bsddb, but it is like SQL server. You can improve
        the speed with special parameters.

        dd

        Comment

        • Bill Williams

          #5
          Re: Best way to handle large lists?

          I don't know enough about Python internals, but the suggested solutions
          all seem to involve scanning bigList. Can this presumably linear
          operation be avoided by using dict or similar to find all occurrences of
          smallist items in biglist and then deleting those occurrences?

          Bill Williams



          In article <prrUg.1662$We. 477@trndny08>,
          Chaz Ginger <cginboston@hot mail.comwrote:
          I have a system that has a few lists that are very large (thousands or
          tens of thousands of entries) and some that are rather small. Many times
          I have to produce the difference between a large list and a small one,
          without destroying the integrity of either list. I was wondering if
          anyone has any recommendations on how to do this and keep performance
          high? Is there a better way than
          >
          [ i for i in bigList if i not in smallList ]
          >
          Thanks.
          Chaz

          Comment

          • Hari Sekhon

            #6
            Re: Best way to handle large lists?

            I don't know much about the python internals either, so this may be the
            blind leading the blind, but aren't dicts much slower to work with than
            lists and therefore wouldn't your suggestion to use dicts be much
            slower? I think it's something to do with the comparative overhead of
            using keys in dicts rather than using positional indexes in lists/arrays...

            At least that is what I thought.

            Can anyone confirm this?

            -h

            Hari Sekhon



            Bill Williams wrote:
            I don't know enough about Python internals, but the suggested solutions
            all seem to involve scanning bigList. Can this presumably linear
            operation be avoided by using dict or similar to find all occurrences of
            smallist items in biglist and then deleting those occurrences?
            >
            Bill Williams
            >
            >
            >
            In article <prrUg.1662$We. 477@trndny08>,
            Chaz Ginger <cginboston@hot mail.comwrote:
            >
            >
            >I have a system that has a few lists that are very large (thousands or
            >tens of thousands of entries) and some that are rather small. Many times
            >I have to produce the difference between a large list and a small one,
            >without destroying the integrity of either list. I was wondering if
            >anyone has any recommendations on how to do this and keep performance
            >high? Is there a better way than
            >>
            >[ i for i in bigList if i not in smallList ]
            >>
            >Thanks.
            >Chaz
            >>

            Comment

            • Sybren Stuvel

              #7
              Re: Best way to handle large lists?

              Bill Williams enlightened us with:
              I don't know enough about Python internals, but the suggested
              solutions all seem to involve scanning bigList. Can this presumably
              linear operation be avoided by using dict or similar to find all
              occurrences of smallist items in biglist and then deleting those
              occurrences?
              And how would that beat O(n)? Every element of bigList has to be
              scanned at one point, either to compare it to every earlier element in
              bigList and eliminate it, or to compare it to every element in
              smallList.

              Run benchmarks on the suggestions, and see which is fastest for
              yourself.

              Sybren
              --
              Sybren Stüvel
              Stüvel IT - http://www.stuvel.eu/

              Comment

              • Chaz Ginger

                #8
                Re: Best way to handle large lists?

                I've done that and decided that Python's 'list comprehension' isn't a
                way to go. I was hoping that perhaps someone had some experience with
                some C or C++ library that has a Python interface that would make a
                difference.

                Chaz

                Sybren Stuvel wrote:
                Bill Williams enlightened us with:
                >I don't know enough about Python internals, but the suggested
                >solutions all seem to involve scanning bigList. Can this presumably
                >linear operation be avoided by using dict or similar to find all
                >occurrences of smallist items in biglist and then deleting those
                >occurrences?
                >
                And how would that beat O(n)? Every element of bigList has to be
                scanned at one point, either to compare it to every earlier element in
                bigList and eliminate it, or to compare it to every element in
                smallList.
                >
                Run benchmarks on the suggestions, and see which is fastest for
                yourself.
                >
                Sybren

                Comment

                • Paul Rubin

                  #9
                  Re: Best way to handle large lists?

                  Sybren Stuvel <sybrenUSE@YOUR thirdtower.com. imaginationwrit es:
                  I don't know enough about Python internals, but the suggested
                  solutions all seem to involve scanning bigList. Can this presumably
                  linear operation be avoided by using dict or similar to find all
                  occurrences of smallist items in biglist and then deleting those
                  occurrences?
                  >
                  And how would that beat O(n)? Every element of bigList has to be
                  scanned at one point, either to compare it to every earlier element in
                  bigList and eliminate it, or to compare it to every element in
                  smallList.
                  Maybe the application should use sets instead of lists for these
                  collections.

                  Comment

                  • Chaz Ginger

                    #10
                    Re: Best way to handle large lists?

                    Paul Rubin wrote:
                    Sybren Stuvel <sybrenUSE@YOUR thirdtower.com. imaginationwrit es:
                    >>I don't know enough about Python internals, but the suggested
                    >>solutions all seem to involve scanning bigList. Can this presumably
                    >>linear operation be avoided by using dict or similar to find all
                    >>occurrences of smallist items in biglist and then deleting those
                    >>occurrences ?
                    >And how would that beat O(n)? Every element of bigList has to be
                    >scanned at one point, either to compare it to every earlier element in
                    >bigList and eliminate it, or to compare it to every element in
                    >smallList.
                    >
                    Maybe the application should use sets instead of lists for these
                    collections.
                    What would sets do for me over lists?

                    Chaz

                    Comment

                    • Larry Bates

                      #11
                      Re: Best way to handle large lists?

                      Chaz Ginger wrote:
                      I have a system that has a few lists that are very large (thousands or
                      tens of thousands of entries) and some that are rather small. Many times
                      I have to produce the difference between a large list and a small one,
                      without destroying the integrity of either list. I was wondering if
                      anyone has any recommendations on how to do this and keep performance
                      high? Is there a better way than
                      >
                      [ i for i in bigList if i not in smallList ]
                      >
                      Thanks.
                      Chaz

                      IMHO the only way to speed things up is to know more about the
                      actual data in the lists (e.g are the elements unique, can they
                      be sorted, etc) and take advantage of all that information to
                      come up with a "faster" algorithm. If they are unique, sets
                      might be a good choice. If they are sorted, bisect module
                      might help. The specifics about the list(s) may yield a faster
                      method.

                      -Larry

                      Comment

                      • Jeremy Sanders

                        #12
                        Re: Best way to handle large lists?

                        Chaz Ginger wrote:
                        What would sets do for me over lists?
                        It's faster to tell whether something is in a set or dict than in a list
                        (for some minimum size).

                        Jeremy

                        --
                        Jeremy Sanders

                        Comment

                        • Chaz Ginger

                          #13
                          Re: Best way to handle large lists?

                          Larry Bates wrote:
                          Chaz Ginger wrote:
                          >I have a system that has a few lists that are very large (thousands or
                          >tens of thousands of entries) and some that are rather small. Many times
                          >I have to produce the difference between a large list and a small one,
                          >without destroying the integrity of either list. I was wondering if
                          >anyone has any recommendations on how to do this and keep performance
                          >high? Is there a better way than
                          >>
                          >[ i for i in bigList if i not in smallList ]
                          >>
                          >Thanks.
                          >Chaz
                          >
                          >
                          IMHO the only way to speed things up is to know more about the
                          actual data in the lists (e.g are the elements unique, can they
                          be sorted, etc) and take advantage of all that information to
                          come up with a "faster" algorithm. If they are unique, sets
                          might be a good choice. If they are sorted, bisect module
                          might help. The specifics about the list(s) may yield a faster
                          method.
                          >
                          -Larry
                          Each item in the list is a fully qualified domain name, e.g.
                          foo.bar.com. The order in the list has no importance. That is about all
                          there is to the list other than to say the number of items in a list can
                          top out about 10,000.

                          Chaz

                          Comment

                          • Richard Brodie

                            #14
                            Re: Best way to handle large lists?


                            "Chaz Ginger" <cginboston@hot mail.comwrote in message
                            news:45227BA1.6 080107@hotmail. com...
                            Each item in the list is a fully qualified domain name, e.g.
                            foo.bar.com. The order in the list has no importance.
                            So you don't actually need to use lists at all, then.
                            You can just use sets and write:

                            newSet = bigSet - littleSet


                            Comment

                            • Jeremy Sanders

                              #15
                              Re: Best way to handle large lists?

                              Jeremy Sanders wrote:
                              Chaz Ginger wrote:
                              >
                              >What would sets do for me over lists?
                              >
                              It's faster to tell whether something is in a set or dict than in a list
                              (for some minimum size).
                              As a footnote, this program

                              import random
                              num = 100000

                              a = set( range(num) )
                              for i in range(100000):
                              x = random.randint( 0, num-1) in a

                              completes in less than a second, whereas

                              import random
                              num = 100000

                              a = range(num)
                              for i in range(100000):
                              x = random.randint( 0, num-1) in a

                              takes a long time on my computer.

                              Jeremy

                              --
                              Jeremy Sanders

                              Comment

                              Working...