472,975 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,975 software developers and data experts.

Calling all experts: Python Tips and Tricks

6,596 Expert 4TB
I've decide to compile a bunch of your favorite tips and tricks for the Articles section.

Post your favorite tips and tricks here, in this thread, and I'll copy the best ones to a Tips and Tricks article (to be created soon).

Thanks. And have fun!
Jul 28 '07 #1
2 2733
6,596 Expert 4TB
It's really not hard. Nor does it need to take much time. This thread is what I have in mind.

I know that you all have it in you. So c'mon...
Aug 9 '07 #2
844 Expert 512MB
Ok, I don't know if this is any good, but I'll post it anyway. I wrote a function that is like a binary search on a built in list object. I was doing one of the problems from project euler and my problem was I kept repeatedly checking wether an element was in a list. When I used this function the program ran much faster. But it only works on sorted lists:
Expand|Select|Wrap|Line Numbers
  1. def lookfor(s, obj, start, end, stopAt = 50):
  2.     if end - start < stopAt:
  3.         if obj in s[start:end]:
  4.             return True
  5.         else:
  6.             return False
  8.     ind = start + ((end-start)/2)
  9.     it = s[ind]
  11.     if it == obj:
  12.         return True
  13.     elif it > obj:
  14.         return lookfor(s, obj, start, ind)
  15.     elif it < obj:
  16.         return lookfor(s, obj, ind, end)
It can also be written as a custom list object that inherits from list and overrides the __contains__ operator. What do you guys think?
Sep 22 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Emile van Sebille | last post by:
QOTW: "If we get 2.3.3c1 out in early December, we could release 2.3.3 final before the end of the year, and start 2004 with a 100% bug-free codebase <wink>." -- Tim Peters "cjOr proWe vbCould...
by: Emile van Sebille | last post by:
QOTW (advanced interfaces track): "I'm firmly in favour of any language that can DWIMNWIS." -- Tim Delaney QOTW (MS roadkill track): "Underestimate MS at your own risk. It is one thing to not...
by: Emile van Sebille | last post by:
QOTW: There are probably many less productive ways to spend your time than studying Mark Pilgrim's code. -- Skip Montanaro QOTW: 'enumerate'...? As in, enumerate(n*) ... ? -- Alex Martelli,...
by: Ville Vainio | last post by:
QOTW: "Other than the parts about 'SADISTS and PERVERTS', what portion was on topic?" - Peter Hansen, commenting a spam posting http://groups.google.com/groups?frame=left&th=50285732810cb768 ...
by: Diez B. Roggisch | last post by:
QOTW: "In my view, the doctrinaire', indeed religious, adherence to OO purity has harmed our discipline considerably. Python was a nice breath of fresh air when I discovered it exactly because it...
by: Gibby Koldenhof | last post by:
Hiya, I'm setting up some code in the spirit of Design Patterns, OOP, etc. All nice and well, it handles pretty much all OO style things and serves my purposes well. There's one last final...
by: bartonc | last post by:
I've decide to compile a bunch of your favorite tips and tricks for the Articles section. I found a post yesterday by Chrisjc that is a perfect example. I copied his post over to create Dealing with...
by: Nepomuk | last post by:
As Linux and the various flavours of Unix are slowly spreading into the world of personal computers, I thought we could collect a few Tips and Tricks here to help each other making the best out of...
by: Frinavale | last post by:
Edit Many times we spend hours and hours trying to solve a problem. When we finally figure it out, we want to share it to keep others from suffering the same way! That's why we have this "Tips...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.