473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Calling all experts: Python Tips and Tricks

bartonc
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 2756
bartonc
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
ilikepython
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
  7.  
  8.     ind = start + ((end-start)/2)
  9.     it = s[ind]
  10.  
  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)
  17.  
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

1
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...
0
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...
0
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,...
0
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 ...
0
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...
4
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...
68
bartonc
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...
20
Nepomuk
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...
8
Frinavale
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.