![]() |
|
Re: ** Python Support Thread **
Quote:
Code:
import timeitCode:
t1 |
Re: ** Python Support Thread **
Cool thanks. Haha I actually tried using timeit but got stuck an infinite loop somehow.
|
Re: ** Python Support Thread **
If you only want to do the dict comprehension once, you can do
Code:
row_data, row_color = zip(*(([],[]) for key in dic))Code:
row_data, row_color = map(list, zip(*(([],[]) for key in dic))) |
Re: ** Python Support Thread **
of course, if all you want is two lists of empty lists, each the same length as the dictionary, there are lots of efficient ways to do it. But the above is the generalizable version.
|
Re: ** Python Support Thread **
Code:
file = open("test.bin","wb")But the python process uses 388MB of RAM. Can anyone explain why it is using so much memory and maybe suggest a more memory efficient code? |
Re: ** Python Support Thread **
immediate thought is just change range to xrange (assuming your on python 2.X)
|
Re: ** Python Support Thread **
Quote:
Now it uses 1.7 MB of RAM. Thanks |
Re: ** Python Support Thread **
The reason it used so much ram before is because range() creates the entire object in memory before iterating over it, while xrange uses a generator expression (http://docs.python.org/library/functions.html#xrange).
|
Re: ** Python Support Thread **
I'm a newb trying to figure out the string format method for python 3. This is from http://learnpythonthehardway.org/book/ex24.html written for python 2.
Code:
def secret_formula(started):Code:
print ("We'd have {0} beans, {1} jars, and {2} crates.".format(secret_formula(start_point)))Code:
a,b,c = secret_formula(start_point) |
Re: ** Python Support Thread **
secret_formula(start_point) is a tuple of three items (at least I assume it is). So you can use it either as one item or as three. But Python can't always tell which you mean.
When you try Code:
print ("We'd have {0} beans, {1} jars, and {2} crates.".format(secret_formula(start_point)))When you do Code:
a,b,c = secret_formula(start_point)To force it to unpack the tuple, you can use the * operator, like so: Code:
print ("We'd have {0} beans, {1} jars, and {2} crates.".format(*secret_formula(start_point))) |
Re: ** Python Support Thread **
merci
|
Re: ** Python Support Thread **
Optimization help please!
The idea of this program is that we have a binary file that is 25.2MB big and we want to compare it bit by bit and count the number of bits that match. In the example below, we read each byte directly from the file into <byte_wiki>. This method takes approx 5-7 minutes and uses 1.8MB of RAM. The commented out section reads the file before hand into a list and uses 690MB of ram and takes much longer to run, 9++ minutes. nBytes = 25 million+ Code:
for b in xrange(1,nBytes+1):I am guessing that when the code is read from file that i/o is a bottleneck. I am guessing that when fed into a list that the 25+million elements are what is slowing it down. My guess is that splitting the Hex byte values and formatting them into lists, and then comparing each bit are expensive operations. Also having list_copy = [] and list_wiki = [] within a loop looks silly to me but I couldn't think of another way around that. Looks like I should probably learn how to do some bitwise manipulation tomorrow. |
Re: ** Python Support Thread **
I'm rusty and not 100% that this is what you're trying to do, but it looks like you can dispense with all this list stuff and just XOR them...
Code:
for b in xrange(1,nBytes+1):EDIT2: Actually now that I think about it some more I think this works even better: Code:
score += 8 - bin(byte_copy ^ byte_wiki).count('1') |
Re: ** Python Support Thread **
You can use izip to iterate over the two lists at the same time. This way you don't need to do the copy loop followed by the compare loop.
|
Re: ** Python Support Thread **
what are you actually trying to do?
Quote:
|
Re: ** Python Support Thread **
Code:
b = [1,2,3,4,5,6,7] |
Re: ** Python Support Thread **
Quote:
Try: Code:
for g in b:Code:
while b: |
Re: ** Python Support Thread **
Quote:
Just playing around with this, python returns about half the list no matter the size and raises no errors. |
Re: ** Python Support Thread **
oddly enough, if you make it "for i in range(len(b))" it works properly. I guess the "for x in b" construct makes it so that when you pop and iterate, whatever counting method it's using ends up moving two spots instead of one.
|
Re: ** Python Support Thread **
Using Python 3.
I can't figure out why r returns as None: This is one of those things where I know I should know the answer, but for some reason.... Code:
def fac(t, r = 1):while this will print out the answer: Code:
def fac(t = t, r = 1): |
Re: ** Python Support Thread **
You forgot to put "return" at the end
|
Re: ** Python Support Thread **
<headslap.gif>
|
Re: ** Python Support Thread **
if it makes you feel any better I literally just made the same mistake
|
Re: ** Python Support Thread **
So I've been casually chugging along learn python the hard way until this,
http://learnpythonthehardway.org/book/ex46.html where the author basically says download a bunch of testing and installation packages and figure out how they work on your own :( wtf! all the documentation is for linux also but I'm using windows. I finally figured out how to install the packages but I can't get this nosetests thing to work. I've found this file, C:\python32\scripts\nosetests-script.py, that runs but it doesn't actually test anything. Do I have to pass in the directory of my "project skeleton" as an argument or something? Also is any of this stuff necessary? or is there an easier way to do testing that doesn't require using the command prompt like its 1996? I would gladly give up on trying to figure this out at this point if this stuff is out dated or just unnecessary. |
Re: ** Python Support Thread **
Normally you just have to do:
Code:
C:\Path\To\Your\Project> nosetests.exe . |
| All times are GMT -4. The time now is 04:26 PM. |
|
Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Copyright © 2008-2020, Two Plus Two Interactive