euglug.org

[작성자:] admin

  • [Python] Speed up this code?



    Bob MillerThu May 25 21:55:28 PDT 2006

    Martin Kelly wrote:
    
    > What is the purpose of sets? I didn't know about them yet.
    
    A set is an unordered collection with no duplicates, similar to a
    mathematical set.  Sets are a newish part of Python.  They appeared as
    a library in Python 2.3, and moved into the language core in 2.4.
    
    Sets happen to be implemented as hash tables, the same a dictionaries.
    The difference is that sets don't have values, just keys.  Hash table
    lookup is a fast operation, unlike searching a list.
    
    -- 
    Bob Miller                              K<bob>
                                            kbob at jogger-egg.com
    
  • [Python] Speed up this code?



    Martin KellyThu May 25 19:34:16 PDT 2006

    Thanks all, it worked! Quite zippy now :).
    
    What is the purpose of sets? I didn't know about them yet.
    
    It appears Tim Chase responded to my query on comp.lang.python as well ;).
    
    Martin
    
    Bob Miller wrote:
    > Martin Kelly wrote:
    > 
    >> def rmlist(original, deletions):
    >>    return [i for i in original if i not in deletions]
    > 
    > Is deletions a list?  If it is, turn it into a set.  Testing whether
    > an item is in a list takes O(n) time.  Testing whether an item is in a
    > set takes O(1) time.
    > 
    > def rmlist(original, deletions):
    >    delset = set(deletions)
    >    return [i for i in original if i not in delset]
    > 
    
  • [Python] Speed up this code?



    Martin KellyThu May 25 19:30:02 PDT 2006

    Nope, I tried that and no effect (at least noticeably).
    
    Mike Casaday wrote:
    > Not sure if it's faster, but try this:
    > 
    > def rmlist(original, deletions):
    >   return filter(lambda i: i not in deletions, original)
    > 
    > On May 25, 2006, at 6:10 PM, Martin Kelly wrote:
    > 
    >> I'm creating a program to calculate all primes numbers in a range of 0 
    >> to n, where n is whatever the user wants it to be. I've worked out the 
    >> algorithm and it works perfectly and is pretty fast, but the one thing 
    >> seriously slowing down the program is the following code:
    >>
    >> def rmlist(original, deletions):
    >>    return [i for i in original if i not in deletions]
    >>
    >> original will be a list of odd numbers and deletions will be numbers 
    >> that are not prime, thus this code will return all items in original 
    >> that are not in deletions. For n > 100,000 or so, the program takes a 
    >> very long time to run, whereas it's fine for numbers up to 10,000.
    >>
    >> Does anybody know a faster way to do this? (finding the difference all 
    >> items in list a that are not in list b)?
    >>
    >> Thanks,
    >> Martin
    >> _______________________________________________
    >> Python mailing list
    >> Python at euglug.org
    >> http://www.euglug.org/mailman/listinfo/python
    > 
    > _______________________________________________
    > Python mailing list
    > Python at euglug.org
    > http://www.euglug.org/mailman/listinfo/python
    > 
    
  • [Python] Speed up this code?



    Bob MillerThu May 25 18:38:53 PDT 2006

    Martin Kelly wrote:
    
    > def rmlist(original, deletions):
    >    return [i for i in original if i not in deletions]
    
    Is deletions a list?  If it is, turn it into a set.  Testing whether
    an item is in a list takes O(n) time.  Testing whether an item is in a
    set takes O(1) time.
    
    def rmlist(original, deletions):
       delset = set(deletions)
       return [i for i in original if i not in delset]
    
    -- 
    Bob Miller                              K<bob>
                                            kbob at jogger-egg.com
    
  • [Python] Speed up this code?



    Mike CasadayThu May 25 18:15:43 PDT 2006

    Not sure if it's faster, but try this:
    
    def rmlist(original, deletions):
       return filter(lambda i: i not in deletions, original)
    
    On May 25, 2006, at 6:10 PM, Martin Kelly wrote:
    
    > I'm creating a program to calculate all primes numbers in a range  
    > of 0 to n, where n is whatever the user wants it to be. I've worked  
    > out the algorithm and it works perfectly and is pretty fast, but  
    > the one thing seriously slowing down the program is the following  
    > code:
    >
    > def rmlist(original, deletions):
    >    return [i for i in original if i not in deletions]
    >
    > original will be a list of odd numbers and deletions will be  
    > numbers that are not prime, thus this code will return all items in  
    > original that are not in deletions. For n > 100,000 or so, the  
    > program takes a very long time to run, whereas it's fine for  
    > numbers up to 10,000.
    >
    > Does anybody know a faster way to do this? (finding the difference  
    > all items in list a that are not in list b)?
    >
    > Thanks,
    > Martin
    > _______________________________________________
    > Python mailing list
    > Python at euglug.org
    > http://www.euglug.org/mailman/listinfo/python
    
    
  • [Python] Speed up this code?



    Martin KellyThu May 25 18:10:12 PDT 2006

    I'm creating a program to calculate all primes numbers in a range of 0 
    to n, where n is whatever the user wants it to be. I've worked out the 
    algorithm and it works perfectly and is pretty fast, but the one thing 
    seriously slowing down the program is the following code:
    
    def rmlist(original, deletions):
        return [i for i in original if i not in deletions]
    
    original will be a list of odd numbers and deletions will be numbers 
    that are not prime, thus this code will return all items in original 
    that are not in deletions. For n > 100,000 or so, the program takes a 
    very long time to run, whereas it's fine for numbers up to 10,000.
    
    Does anybody know a faster way to do this? (finding the difference all 
    items in list a that are not in list b)?
    
    Thanks,
    Martin
    
  • [Python] shortcut?



    Bob MillerMon May 22 19:14:06 PDT 2006

    Rob Hudson wrote:
    
    > Is there a Python shortcut for the following?
    > 
    > if d.has_key(k):
    > 	d[k].append(v)
    > else:
    > 	d[k] = [v]
    
    	d.setdefault(k, []).append(v)
    
    -- 
    Bob Miller                              K<bob>
                                            kbob at jogger-egg.com
    
  • [Python] web.py



    Rob HudsonMon May 22 10:18:57 PDT 2006

    Has anyone seen web.py?  I saw a reference to it.  It seems like a 
    *very* light Python framework.  It looks kind of interesting.  Is there 
    any reason you wouldn't want to use something like this for larger websites?
    
    http://webpy.org/
    
  • [Python] shortcut?



    cmeyers at guardnet.comMon May 22 09:55:43 PDT 2006

    Oops
    
    This works for counters and stuff but as Horst points out, append is a
    modification in place function. Sorry
    
    Chris
    
    > How about
    >
    >    d[k] = d.get(k,[]).append(v)
    >
    > I use this quite a bit. The second arg to .get is a default value, if
    > missing it's None.
    >
    > Chris
    >
    >> Is there a Python shortcut for the following?
    >>
    >> if d.has_key(k):
    >> 	d[k].append(v)
    >> else:
    >> 	d[k] = [v]
    >>
    >> I'm keeping a dictionary of key:list pairs and I'm writing the above
    >> often.  I think I remember seeing a shortcut to something like this
    >> but I can't recall what it was.
    >>
    >> Thanks,
    >> Rob
    >> _______________________________________________
    >> Python mailing list
    >> Python at euglug.org
    >> http://www.euglug.org/mailman/listinfo/python
    >
    >
    >
    > _______________________________________________
    > Python mailing list
    > Python at euglug.org
    > http://www.euglug.org/mailman/listinfo/python