- Strength to Increase Rep
- +5
- Strength to Decrease Rep
- -1
- Upvotes Received
- 5
- Posts with Upvotes
- 4
- Upvoting Members
- 4
- Downvotes Received
- 0
- Posts with Downvotes
- 0
- Downvoting Members
- 0
Re: The method of Gribouillis uses a lot of sorted(). They generate new lists. A sort "in place" might be faster and you only need one sort() call [CODE] L = [ (k,x) for k, v in dict1.items() for x in v] L.sort() [/CODE] Tuples are sorted by element. | |
Re: Maybe this might give you a clue. [ICODE]doc_wp = open('path', 'wb')[/ICODE] Opens a file with name: path [CODE=python] while out_block: listing = os.listdir(root) out_block = f.read(int(carved)) for each_file in listing: doc_wp = open(os.path.join(root,each_file), 'ab') doc_wp.write(out_block) doc_wp.close() [/CODE] | |
Re: why don't you use the re module [CODE=python] import re def countSubStringMatchRecursive(target,key): return len(re.findall(key,target)) [/CODE] | |
Re: It can all be done in one call to re.findall() or re.finditer() [CODE] mynums = [int(x) for x in re.findall('\d+(?=:)',dd)] [/CODE] | |
Re: The fastest way to make a 2D array of a constant value is [CODE=python] array2D= [[0]*numCols]*numRows [/CODE] | |
Re: The solution of tonyjv can be written in a single line. Only sort on one index here. [CODE=python] korvatunturi = { 'Father Christmas': [[130, 123,12], [12,223,122]], 'Rudolf' : [[12,3,23], [235,24,3]] } dummy = [v.sort(key = lambda x: x[index]) for v in korvatunturi.values()] [/CODE] Because the lists are sorted in place … | |
Re: To be able to see the improvement of an optimization timing the code is a good instrument. If I time your original code with your example images using the following [code=python] import datetime t1=datetime.datetime.now() matchTemplate(searchImage, templateImage) delta=datetime.datetime.now()-t1 print "Time=%d.%d"%(delta.seconds,delta.microseconds) [/code] I get [icode]Time=4.625000[/icode] If we look at the code we … | |
Re: Because the keyword is not always ending with an '&' character a slight adjustment [code=python]>>> patt = r'keywords=([^&]+)' >>> re.findall(patt, "keywords=james&keywords=john&keywords=brian") ['james', 'john', 'brian'][/code] | |
Re: What kind of steps are you talking about. Do the steps only apply to the current character? If yes, you just made a substitution encryption and that is easy to crack. Just count the character frequencys and compare with known counts for certain languages. If you use the previous characters … | |
Re: re.search() returns a MatchObejct, not the string that matches the regex. Look up in the docs what you can do with it to get the match string. A better regex to use is [CODE=python] matchObj = re.search('(?<=http://user.url.com/)[^"]+', pg_r) if matchObj: pass # found a match [/CODE] | |
Re: It made with Microsoft Word. The WORST HTML editor there is. A good cadidate to use is module: HTMLParser First clean up the file and get rid of all the style stuff and comments. Then you have a better view of what the file is made up of. | |
| Re: [CODE=python] for i in range(1, 638): try: temp=start+str(i) permlist.append(str(url.urlopen(temp).readlines()[88])) textlist.append(str(url.urlopen(temp).readlines()[77])) except Error: # catch any exception and continue the for loop print "Error at index %d."%i [/CODE] |
Re: On MS-Windows there is a system window message that you can process to indicate that you don't want the screensaver to become active. You can use it to monitor when the screensaver gets active. You must be in a GUI program, from a commandline python program it does not work. … | |
Re: The following code is a bit cleaner [code=python] import re infileName = raw_input("filename ") infile = open(infileName, 'r') data = infile.readlines() printLines = False skipCount = 0 for index, line in enumerate(data): if "KEY1:" in line: printLines = True skipCount = 4 continue if "---" in line: printLines = False … | |
Re: [QUOTE=mahela007;991473]Is it good programming practice to refer to class variables by using their instances? like mycow.numcows?[/QUOTE] There are times that referring by self is the way to go. If you use [ICODE]self.somevar[/ICODE] python first looks if there is a member variable named [ICODE]somevar[/ICODE]. If not found: look for a class … | |
Re: if the number in str format contains a '.' character use split() [code=python] numsplit = str(num).split('.') num_int = int(numsplit[0]) num_dec = numsplit[1] # this is a string [/code] | |
Re: The reason is that you can't represent all numbers exact with floating point objects. You have to approximate the number because you have a limited number of bits. The str() function does some pretty printing of float numbers (that includes some rounding). If you print the exponential version you see … | |
Re: It takes a long time because many fib-numbers are calculated a lot. If you store the numbers you already know it goes pretty fast. And these results are stored for the duration of the program so the next call to getFib(50) is instantaneous Use a class variable to store the … | |
Re: have a look at the 'break' statement in the python docs. | |
Re: Have a look for the "grep" tool that is available for your OS. Ignore case searching is done with the -i option | |
| Re: If your called function for a customer takes 100%CPU load you will take longer to complete the whole task due to scheduling the tasks. If the function is an RPC you move the load to the server, only if it is a server farm or it has multi CPU you … |
Re: [QUOTE=Gribouillis;984981]You could use [icode](bool(example) and example in "hello there")[/icode]. This is true only if example is a non empty substring of "hello there".[/QUOTE] There is no need to convert the example string to bool before testing. All objects in Python have a True/False value. [code=python]if example and (example in "hello … | |
Re: It would have been better if you gave an example of your input and required output. The summing of VAR1 and VAR2 is not clear. [code=python] from itertools import izip #row=[] #col=[] #var1=[] #var2=[] def getSum(data): if not isinstance(data,list): data=[data] return sum(data,0) result = [(r,c,getSum(v1),getSum(v2)) for r,c,v1,v2 in izip(row,col,var1,var2) if … | |
Re: You can use the Image.paste(imagesrc, box) method [URL="http://www.effbot.org/imagingbook/image.htm#tag-Image.Image.paste"]PIL-doc Image.Image.paste[/URL] |