how to find a lable quickly?
Hi, I am a new user on Python and I really love it. I have a big text file with each line like: label 3 teststart 5 endtest 100 newrun 2345 I opened the file by uu=open('test.txt','r') and then read the data as xx=uu.readlines() In xx, it contains the list of each line. I want to find a spcefic labels and read the data. Currently, I do this by for ss in xx: zz=ss.split( ) if zz[0] = endtest: index=zz[1] Since the file is big and I need find more lables, this code runs slowly. Are there anyway to speed up the process? I thought to convert the data xx from list to a dictionay, so I can get the index quickly based on the label. Can I do that effeciently? Thanks Frank _ メッセンジャーお友達紹介プレゼント第2弾開始!ラスベガス旅行プレゼント http://campaign.live.jp/dizon/ -- http://mail.python.org/mailman/listinfo/python-list
Do I have to quit python to load a module?
Hi, When I edit a module, I have to quit python and then restart python and then import the module. Are there any way to avoid quit python to load an updated module? When I am debugging a module code, I need to constantly make changes. It is not convenient to quit and reload. Thanks Frank _ メッセンジャーお友達紹介プレゼント第2弾開始!ラスベガス旅行プレゼント http://campaign.live.jp/dizon/ -- http://mail.python.org/mailman/listinfo/python-list
A newbie question
Hi, I am trying to write a python class with a new data type such as: class Cc14: def __init__(self, realpart, imagpart): self.r=realart self.i=imagpart def __add__(self,x): return self.r+x,r, self.i+x.i If I have x=Cc14(4,5) y=Cc14(4,5) z=x+y z will be a tuple instead of Cc14. How can I return a Cc14 class? Thanks Frank _ ウェブページを印刷しても途切れない!便利なブラウザ MSN版IE7 を使おう http://promotion.msn.co.jp/ie7/ -- http://mail.python.org/mailman/listinfo/python-list
Re: A newbie question
Thanks all for the help. It solves my problem. I want to build a class type for fixed point operation for a specifc chip. I could not use the build in complex data type. It is a daunting job for me since I has not use python before. Frank >From: Dan Bishop <[EMAIL PROTECTED]> >To: python-list@python.org >Subject: Re: A newbie question >Date: 21 May 2007 16:22:06 -0700 > >On May 21, 6:04 pm, "wang frank" <[EMAIL PROTECTED]> wrote: > > Hi, > > > > I am trying to write a python class with a new data type such as: > > class Cc14: > >def __init__(self, realpart, imagpart): > > self.r=realart > > self.i=imagpart > > > >def __add__(self,x): > > return self.r+x,r, self.i+x.i > > > > If I have > > x=Cc14(4,5) > > y=Cc14(4,5) > > z=x+y > > > > z will be a tuple instead of Cc14. How can I return a Cc14 class? > >return Cc14(self.r+x,r, self.i+x.i) > >FYI, Python has a built-in "complex" type. > >-- >http://mail.python.org/mailman/listinfo/python-list _ オンライン地図マガジン「地図マガ」創刊!全国水族館マップを特集 http://chizumaga.jp/ -- http://mail.python.org/mailman/listinfo/python-list
how to use private method in a class
Hi, I am trying to write a python class with a new data type such as: class Cc14: def __init__(self, realpart, imagpart): self.r=realart self.i=imagpart def __saturator(x): return x+1 def out(self,x): return Cc14(__saturator(x.r), __saturator(x,i)) When I use the method out such as: z.out Python complains: global name '_Cc14_saturator' is not defined. Is the way put two underscore in front of the definitio making the method becomes private? Why in the same clase, I could not use the __saturator method? Thanks Frank >From: "wang frank" <[EMAIL PROTECTED]> >To: python-list@python.org >Subject: A newbie question >Date: Mon, 21 May 2007 23:04:06 + > >Hi, > >I am trying to write a python class with a new data type such as: >class Cc14: >def __init__(self, realpart, imagpart): > self.r=realart > self.i=imagpart > >def __add__(self,x): > return self.r+x,r, self.i+x.i > >If I have >x=Cc14(4,5) >y=Cc14(4,5) >z=x+y > >z will be a tuple instead of Cc14. How can I return a Cc14 class? > >Thanks >Frank > >_ >ウェブページを印刷しても途切れない!便利なブラウザ MSN版IE7 を使おう >http://promotion.msn.co.jp/ie7/ > >-- >http://mail.python.org/mailman/listinfo/python-list _ オンライン地図マガジン「地図マガ」創刊!全国水族館マップを特集 http://chizumaga.jp/ -- http://mail.python.org/mailman/listinfo/python-list
Tkinter or wxpython?
Hi, I want to build a GUI to execut python script. I found TKinter and wxpython. Which one is easier for a newbie? and which one is better? Thanks Frank _ 豪華!大リーグ観戦ツアーや高級外車が当たるスペシャルキャンペーンをお見逃しな く http://clk.atdmt.com/GBL/go/msnjpqjl006010gbl/direct/01/ -- http://mail.python.org/mailman/listinfo/python-list
Speed of Python
Hi, While comparing the speed of octave and matlab, I decided to do a similar test for python and matlab. The result shows that python is slower than matlab by a factor of 5. It is not bad since octave is about 30 time slower than matlab. Here is the result in matlab: Elapsed time is 0.015389 seconds. and in Python: t=timeit.Timer("bench1.bench1(10)","import bench1") t.repeat(1,1) [0.071012377266015392] Here is the bench1.py: import math def bench1(n): for i in range(n): for j in range(1000): m=j+1 z=math.log(m) z1=math.log(m+1) z2=math.log(m+2) z3=math.log(m+3) z4=math.log(m+4) z5=math.log(m+5) z6=math.log(m+6) z7=math.log(m+7) z8=math.log(m+8) z9=math.log(m+9) return z9 Is my conclusion correct that Python is slower than matlab? Are there any way to speed it up? It seems Python automatically created bench1.pyc. Does Python automatically execute the bench1.pyc to speed it up? Thanks Frank _ 「地図マガ」特集 残暑を吹きとばせ!ご当地アイスクリームマップが登場 http://chizumaga.jp/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed of Python
Hi, Here is the matlab code: function [z]=bench1(n) for i=1:n, for j=1:1000, z=log(j); z1=log(j+1); z2=log(j+2); z3=log(j+3); z4=log(j+4); z5=log(j+5); z6=log(j+6); z7=log(j+7); z8=log(j+8); z9=log(j+9); end end z = z9; I am not familiar with python, so I just simply try to reproduce the same code in python. If you think that my python script is not efficient, could you tell me how to make it more efficient? Thanks Frank From: Roberto Bonvallet <[EMAIL PROTECTED]> To: python-list@python.org Subject: Re: Speed of Python Date: Fri, 07 Sep 2007 17:19:02 - On Sep 7, 12:42 pm, "wang frank" <[EMAIL PROTECTED]> wrote: > Here is the bench1.py: > import math > def bench1(n): > for i in range(n): > for j in range(1000): > m=j+1 > z=math.log(m) > z1=math.log(m+1) > z2=math.log(m+2) > z3=math.log(m+3) > z4=math.log(m+4) > z5=math.log(m+5) > z6=math.log(m+6) > z7=math.log(m+7) > z8=math.log(m+8) > z9=math.log(m+9) > return z9 > > Is my conclusion correct that Python is slower than matlab? Show us your Matlab code in order to see if both are equivalent. Your Python code creates n lists of 1000 elements, so you're not actually measuring only the numeric computations. Cheers, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list _ メッセンジャー用アイコンに大人気オンラインゲームの萌えなキャラが登場! http://messenger.live.jp/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed of Python
Thanks for all your help. Using Istvan Albert's suggestion, I have recompared the speed of the following funciton: matlab: function [z]=bench2(n) for i=1:n, j=(0:999); z=log(j+1); end python: from numpy import arange, log def bench4(n): for i in xrange(n): nums = arange( n ) a = log( nums + 1) Python actually is faster than Matlab. tic; z=bench2(1000); toc Elapsed time is 0.159485 seconds. Python: import timeit t=timeit.Timer("bench1.bench4(1000)","import bench1") t.repeat(1,1) [0.10052953657517924] Thanks again. Frank From: ajaksu <[EMAIL PROTECTED]> To: python-list@python.org Subject: Re: Speed of Python Date: Fri, 07 Sep 2007 19:27:45 - On Sep 7, 2:37 pm, "wang frank" <[EMAIL PROTECTED]> wrote: > I am not familiar with python, so I just simply try to reproduce the same > code in python. Seems almost correct, but from what I guess of MatLab, George's suggestions make it a bit more fair. > If you think that my python script is not efficient, could you tell me how > to make it more efficient? In pure Python? No idea (besides using Roberto's and George's suggestions). If you allow for extensions, Istvan has the answer. If you allow compiling Python to C++ (using ShedSkin: http://shed-skin.blogspot.com/), here's a small report: -- [EMAIL PROTECTED]:~/sandbox$ cat bench.py import math n = 1 def bench1(n): for i in range(n): for j in range(1000): m=j+1 z=math.log(m) z1=math.log(m+1) z2=math.log(m+2) z3=math.log(m+3) z4=math.log(m+4) z5=math.log(m+5) z6=math.log(m+6) z7=math.log(m+7) z8=math.log(m+8) z9=math.log(m+9) return z9 a = bench1(10) [EMAIL PROTECTED]:~/sandbox$ ss -e bench.py *** SHED SKIN Python-to-C++ Compiler 0.0.22 *** Copyright 2005-2007 Mark Dufour; License GNU GPL version 2 (See LICENSE) (Please send bug reports here: [EMAIL PROTECTED]) [iterative type analysis..] ** iterations: 2 templates: 44 [generating c++ code..] [EMAIL PROTECTED]:~/sandbox$ make bench.so g++ -O3 -s -pipe -fomit-frame-pointer -I/home/ajaksu/shedskin-0.0.22/ lib -g -fPIC -I/usr/include/python2.5 -D__SS_BIND /home/ajaksu/ shedskin-0.0.22/lib/builtin.cpp /home/ajaksu/shedskin-0.0.22/lib/ math.cpp bench.cpp -lgc -shared -Xlinker -export-dynamic -lpython2.5 - o bench.so [EMAIL PROTECTED]:~/sandbox$ mv bench.py pbench.py [EMAIL PROTECTED]:~/sandbox$ ipython Python 2.5.1 (r251:54863, May 2 2007, 16:56:35) [...] In [1]: from pbench import bench1 as pbench1 In [2]: from bench import bench1 In [3]: %timeit a = bench1(10) 100 loops, best of 3: 10.2 ms per loop In [4]: %timeit a = pbench1(10) 10 loops, best of 3: 92.8 ms per loop -- I guess you'd also see nice improvements from Pyrex or Cython, Blitz and other tools. Check http://wiki.python.org/moin/PythonSpeed/PerformanceTips for the general ideas and http://scipy.org/PerformancePython for an insight on available tools that even compares their speeds to Matlab. And-if-you-run-more-benchmarks-please-do-post-them-ly yrs, Daniel -- http://mail.python.org/mailman/listinfo/python-list _ 3つの”ホンモノ”プレゼント 賞品第3弾スタート!アルファ スパイダー2.2が当た る http://clk.atdmt.com/GBL/go/msnjpqjl006010gbl/direct/01/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed of Python
I also have tried to use numpy to speed it up. However, surprisingly, it is slower than the pure python code. Here is the code: import numpy arange=numpy.arange nlog=numpy.log def bench6(n): for i in xrange(n): for j in xrange(1000): m=j+1 z=nlog(m) z1=nlog(m+1) z2=nlog(m+2) z3=nlog(m+3) z4=nlog(m+4) z5=nlog(m+5) z6=nlog(m+6) z7=nlog(m+7) z8=nlog(m+8) z9=nlog(m+9) return z9 from math import log def bench3(n): for i in xrange(n): for j in xrange(1000): # m=j+1 z=log(j+1) z1=log(j+2) z2=log(j+3) z3=log(j+4) z4=log(j+5) z5=log(j+6) z6=log(j+7) z7=log(j+8) z8=log(j+9) z9=log(j+10) return z9 Here is the result: t6=timeit.Timer("bench1.bench6(10)", "import bench1") t6.repeat(1,1) [0.73878858905254674] t3=timeit.Timer("bench1.bench3(10)", "import bench1") t3.repeat(1,1) [0.056632337350038142] Anyone know why? Thanks Frank From: "Kurt Smith" <[EMAIL PROTECTED]> To: "wang frank" <[EMAIL PROTECTED]> Subject: Re: Speed of Python Date: Fri, 7 Sep 2007 16:49:05 -0500 On 9/7/07, wang frank <[EMAIL PROTECTED]> wrote: > Hi, > Here is the matlab code: > function [z]=bench1(n) > for i=1:n, > for j=1:1000, > z=log(j); > z1=log(j+1); > z2=log(j+2); > z3=log(j+3); > z4=log(j+4); > z5=log(j+5); > z6=log(j+6); > z7=log(j+7); > z8=log(j+8); > z9=log(j+9); > end > end > z = z9; > > I am not familiar with python, so I just simply try to reproduce the same > code in python. > If you think that my python script is not efficient, could you tell me how > to make it more efficient? One thing you can do is bind math.log to the function's namespace thusly: import math def bench1_opt(n): log = math.log for i in range(n): for j in range(1000): m=j+1 z=log(m) z1=log(m+1) z2=log(m+2) z3=log(m+3) z4=log(m+4) z5=log(m+5) z6=log(m+6) z7=log(m+7) z8=log(m+8) z9=log(m+9) return z9 On my system I get about a 20% speedup over the 'unoptimized' version (even though this optimization is rather trivial and may even help readability). Still not matlab speed, but better. You might be able to do better using xrange instead of range, but the loop overhead isn't the main slowdown (only about 1% ). For comparisons in real-world usage (if you are doing numerical work), I urge you to take a look at a specifically numerical package -- numpy/scipy or their equivalents: http://www.scipy.org/ Python is a *very* general language not suited for heavy numerical work out of the box -- dedicated numerical packages adapt python to this specialized envirornment, and are becoming more and more competitive with Matlab. The best part is you can put your time-critical code in FORTRAN or C and wrap it with pyrex, f2py, weave, etc. pretty easily, and still have the beauty of Python gluing everything together. Kurt _ メッセンジャー用アイコンに大人気オンラインゲームの萌えなキャラが登場! http://messenger.live.jp/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed of Python
I am just trying to compare the speed with matlab. The arrange is used for another test, that is why it shows up in the mail. Thanks Frank From: "Marc 'BlackJack' Rintsch" <[EMAIL PROTECTED]> To: python-list@python.org Subject: Re: Speed of Python Date: 7 Sep 2007 23:17:55 GMT On Fri, 07 Sep 2007 22:59:26 +, wang frank wrote: > I also have tried to use numpy to speed it up. However, surprisingly, it is > slower than the pure python code. > > Here is the code: > import numpy > arange=numpy.arange > nlog=numpy.log > def bench6(n): >for i in xrange(n): >for j in xrange(1000): >m=j+1 >z=nlog(m) >z1=nlog(m+1) >z2=nlog(m+2) >z3=nlog(m+3) >z4=nlog(m+4) >z5=nlog(m+5) >z6=nlog(m+6) >z7=nlog(m+7) >z8=nlog(m+8) >z9=nlog(m+9) >return z9 > > [窶ヲ] > > Anyone know why? Because you don't really take advantage of `numpy`. The `numpy.log()` function can be used with scalars but I guess it is slower because it has to check if its argument is a scalar or array. Untested: from numpy import arange, log as nlog def bench6(n): for dummy in xrange(n): for j in xrange(1000): z = nlog(arange(j + 1, j + 11)) return z[-1] -- http://mail.python.org/mailman/listinfo/python-list _ 3つの”ホンモノ”プレゼント 賞品第3弾スタート!アルファ スパイダー2.2が当た る http://clk.atdmt.com/GBL/go/msnjpqjl006010gbl/direct/01/ -- http://mail.python.org/mailman/listinfo/python-list
how to install numpy and scipy on debian?
Hi, I need to help to install these two packages on debian. I want to know what packages do I need to? I have installed fftw3,fftww2, sfftw2, atlas. Did I miss anything? In what way I can install an optimized numpy and scipy, since my project is very big and speed is important. Thanks Frank _ マイクロソフトの最新次世代ブラウザIE7にMSN版ならではの便利な機能をプラス http://promotion.msn.co.jp/ie7/-- http://mail.python.org/mailman/listinfo/python-list
Where to put the import command in the file?
Hi, I am writing Python script now. The project will grow bigger in future. I need to import some packages for several functions, such as numpy. Where is the best plalce to put the import numpy command? Is it fine to put on the first line in the file? Is it better to put it into each function after the def functionname? or they are the same. Since numpy function will be used in all files, so I have to import it in each files. Does this will increase the memory usuage or there are better way to do it. thanks Frank _ 広告表示なし!アカウント有効期限なし!Hotmail Plus のお申し込みはこちら http://get.live.com/mail/options-- http://mail.python.org/mailman/listinfo/python-list
RE: module confusion
Hi, I am moving from Matlab to Python+numpy+scipy. In Matlab you can use function dec2bin, hex2dec, dec2hex bin2dec functions to convert decimal to binary and heximal etc. Before I try to implement my own function in Python, I want to know whether in Python such functionalities are already there. Thanks Frank _ MSNミュージックとEMI Artistsが共同開催するオーディション SCHOOL OF SCHOOL http://music.jp.msn.com/-- http://mail.python.org/mailman/listinfo/python-list
function to convert data into binary, hex and back.
From: [EMAIL PROTECTED]: [EMAIL PROTECTED]: RE: module confusionDate: Wed, 3 Oct 2007 17:14:19 + Hi, I am moving from Matlab to Python+numpy+scipy. In Matlab you can use function dec2bin, hex2dec, dec2hex bin2dec functions to convert decimal to binary and heximal etc. Before I try to implement my own function in Python, I want to know whether in Python such functionalities are already there. Thanks Frank MSNミュージックとEMI Artistsが共同開催するオーディション SCHOOL OF SCHOOL http://music.jp.msn.com/ _ 【MSNビデオ】超貴重!驚きの大物対談が実現。作家 村上龍が話題のあの人に迫る http://video.msn.co.jp/rvr/default.htm-- http://mail.python.org/mailman/listinfo/python-list
RE: module confusion
Sorry for the wrong title of this email. Please ignore this email. I have resend the question with correct title. Thanks frank From: [EMAIL PROTECTED]: [EMAIL PROTECTED]: RE: module confusionDate: Wed, 3 Oct 2007 17:14:19 + Hi, I am moving from Matlab to Python+numpy+scipy. In Matlab you can use function dec2bin, hex2dec, dec2hex bin2dec functions to convert decimal to binary and heximal etc. Before I try to implement my own function in Python, I want to know whether in Python such functionalities are already there. Thanks Frank MSNミュージックとEMI Artistsが共同開催するオーディション SCHOOL OF SCHOOL http://music.jp.msn.com/ _ 今話題になってる出来事や有名人をランキングで毎週発表「MSN 気になる言葉」 http://keyword.jp.msn.com/default.aspx-- http://mail.python.org/mailman/listinfo/python-list
how to rerun a function after edit it?
Hi, I am looking for a way to rerun functions after I changed them. This is very critical during the development stage. Currently I have to quit python and restart the python to run the functions. I found the reload function. But it needs the module name. In my case, I import the module using from filename import *. I do not know how to reload it. Using IDLE, it seems that everytime when I click F5, it reloads the function and execute them. I want to find out how to do it without IDLE. For the python script, I use execfile('filename.py') to execute it. Is it possible by directly typing filename to run it? Thanks Frank _ 今話題になってる出来事や有名人をランキングで毎週発表「MSN 気になる言葉」 http://keyword.jp.msn.com/default.aspx-- http://mail.python.org/mailman/listinfo/python-list
RE: Launch file from Python
I have tried to use os.system to run an application inside python on ms-window. However, the applicaion will grap the python and I could not do anything inside python shell unless I quit the application. Are there any way to avoid this? so I can still type command in python shell. Here is the command I used to open the editor: os.sytem('gvim test.py') After this command if I do not quit the gvim, I could not enter command in python shell. Thanks Frank > From: [EMAIL PROTECTED]> Subject: Re: Launch file from Python> Date: Sun, 12 > Aug 2007 17:44:48 +> To: python-list@python.org> > On Wed, 08 Aug 2007 > 10:28:57 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:> > Good > afternoon from someone who is trying to learn Python.> >> > I would like to > launch an app from within a Python script. From the> > examples I have found, > I should be able to do this with os.system.> >> > I use this:> > > os.system("xplanet-1.2.0/xplanet.exe -fontsize 24 -label -target earth> > > -lat 33.65 -lon -84.42 -radius 40 -num_times 1 -tmpdir .")> > This is copied > directly from the .bat file that launches the xplanet> > app. It works > there.> >> > and get this:> > 1> > That means "error", as others noted.> > It > is odd that you get no printouts. Had this been on Unix, you'd> either get > "file not found" or similar from the shell trying to run> the thing, or > something from xplanet itself (only really badly> programs return failure > without prin! ting some kind of cause).> > Two more comments, assuming you are on Windows (you mention ".bat> files"):> > - You use the relative path xplanet-1.2.0/xplanet.exe. That should> require your program to have the parent of xplanet-1.2.0 as current> directory. Did the .bat script change directory first?> > - It is unusual to use / as a path separator on Windows --> xplanet-1.2.0\xplanet.exe is more normal. Some parts of Windows> tolerate both, others do not, IIRC. But Python itself should not> care in this case.> > /Jorgen> > -- > // Jorgen Grahn \X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!> -- > http://mail.python.org/mailman/listinfo/python-list _ 今話題になってる出来事や有名人をランキングで毎週発表「MSN 気になる言葉」 http://keyword.jp.msn.com/default.aspx-- http://mail.python.org/mailman/listinfo/python-list
how to convert string to number?
Hi, I have struggling to efficiently convert a string list to number. Here is my problem. I have a file that contains lines such as: data_1 1 1 2 3.5 After I read the data from the file by using readlines(), each line contains a string. I use the re moduel to split the line into ['data_1', '1','1','2','3.5']. I want to create a dictionary which contains {'data_1':[1 1 2 3.5]} The problem is I coud not efficiently find a way to convert the string to number. Does anyone know how to create such dictionary efficiently? thanks Frank _ 今話題になってる出来事や有名人をランキングで毎週発表「MSN 気になる言葉」 http://keyword.jp.msn.com/default.aspx-- http://mail.python.org/mailman/listinfo/python-list
RE: how to convert string to number?
It seems that I have problem with the python-list, so I resend this. Thanks Frank From: [EMAIL PROTECTED]: [EMAIL PROTECTED]: how to convert string to number?Date: Tue, 16 Oct 2007 18:15:06 + Hi, I have struggling to efficiently convert a string list to number. Here is my problem. I have a file that contains lines such as: data_1 1 1 2 3.5 After I read the data from the file by using readlines(), each line contains a string. I use the re moduel to split the line into ['data_1', '1','1','2','3.5']. I want to create a dictionary which contains {'data_1':[1 1 2 3.5]} The problem is I coud not efficiently find a way to convert the string to number. Does anyone know how to create such dictionary efficiently? thanks Frank 今話題になってる出来事や有名人をランキングで毎週発表「MSN 気になる言葉」 http://keyword.jp.msn.com/default.aspx _ MSNミュージックとEMI Artistsが共同開催するオーディション SCHOOL OF SCHOOL http://music.jp.msn.com/-- http://mail.python.org/mailman/listinfo/python-list
RE: how to convert string to number?
Hi, Tim, Thanks for your help. For some reason, I only get your reply from the forum. This is not a homework problem since I have left the school long time ago. I am new to Python and find it very interesting so I decided to try to port a big project from matlab to python. To prove the value of the python, I need to find an python way to do it. Otherwise, the result may be slower than matlab. The input file contains many lines of data starts with a label. The data lengths are not the same and the data type is mixed with int and float. Some lines start with comment sign # need to be removed from the dictionary. The mixed int and float really cause me trouble to convert the data efficiently. I will try your suggestion I am really appreciate your help. Frank Wang > Date: Tue, 16 Oct 2007 13:52:31 -0500> From: [EMAIL PROTECTED]> To: [EMAIL > PROTECTED]> CC: python-list@python.org> Subject: Re: how to convert string to > number?> > > I have struggling to efficiently convert a string list to> > > number. Here is my problem. I have a file that contains lines> > such as:> > > > > data_1 1 1 2 3.5> > > > After I read the data from the file by using > readlines(), each> > line contains a string. I use the re moduel to split the > line> > into ['data_1', '1','1','2','3.5']. I want to create a> > dictionary > which contains> > > > {'data_1':[1 1 2 3.5]}> > > > The problem is I coud not > efficiently find a way to convert> > the string to number.> > > > Does anyone > know how to create such dictionary efficiently?> > Despite my Spidey-sense > tingling that this is a homework > assignment, as similar forms of the > question have popped up > several times in the last week, I supress it this > time.> > Paraphrasing Andy Dufresne, "Mr. Wang, do you trust your! file?"[1]> > If you don't trust the content of your file, you have to know either> > 1) how many columns of data to expect or> 2) the type each should be (int or float)> > If the same type for each is okay, you can use something like> > >>> s = {}> >>> for line in file('in.txt'):> ... k,v = line.rstrip('\n').split(None, 1)> ... s[k] = map(float, v.split())> ...> >>> s> {'data_1': [1.0, 1.0, 2.0, 3.5], 'data_4': [1.0, 1.0, 8.0, 4.5]}> > However, if you want them to be the actual types that evaluating > them would give (thus the trust-your-source issue), you can use this:> > >>> s = {}> >>> for line in file('in.txt'):> ... k,v = line.rstrip('\n').split(None, 1)> ... s[k] = map(eval, v.split())> ...> >>> s> {'data_1': [1, 1, 2, 3.5], 'data_4': [1, 1, 8, 4.5]}> > > Both instances don't try to do anything smart with duplicate > keys, so if you want to append, Bruno Desthuilliers *just* posted > (in the last hour or so) a nice tip on this using > setdefault().append()> > -tkc> >! [1]http://www.finestquotes.com/movie_quotes/movie/Shawshank%20Redempt ion/page/0.htm> > > > > _ 【MSNビデオ】超貴重!驚きの大物対談が実現。作家 村上龍が話題のあの人に迫る http://video.msn.co.jp/rvr/default.htm-- http://mail.python.org/mailman/listinfo/python-list
parsing the output from matlab
Hi, I have a big log file generated from matlabe, for each variable, it print the name of the variable and an empty line and then the value. such as: x1 = 0.1 y = 7 z = 6.7 x1 = 0.5 I want to use python to parse the file and selectively print out the vairable and its value. For example, I want to print out all the value related with x1, so the output will be x1 = 0.1 x1 = 0.5. I really do not know how to do it. Thanks Frank _ 広告表示なし!アカウント有効期限なし!Hotmail Plus のお申し込みはこちら http://get.live.com/mail/options-- http://mail.python.org/mailman/listinfo/python-list
RE: parsing the output from matlab
Hi, Travis, Thanks very much for your help. Since each day, my mail box is flooded with python forum email. I simply overlooked your email, eventhough I am desperately waiting for the help. Today when I googled the topic and found your reply. I am sorry that I send a similar help request to the forum today. Frank Date: Tue, 23 Oct 2007 10:08:28 -0400From: [EMAIL PROTECTED]: [EMAIL PROTECTED]: Re: parsing the output from matlab On 10/22/07, wang frank <[EMAIL PROTECTED]> wrote I have a big log file generated from matlabe, for each variable, it print the name of the variable and an empty line and then the value. such as: x1 = 0.1 y =7 z = 6.7 x1 =0.5 I want to use python to parse the file and selectively print out the vairable and its value. For example, I want to print out all the value related with x1, so the output will be x1 = 0.1x1 = 0.5. Here is a fairly naive version with re named groups that should handle the example you pasted. In [62]: import re In [63]: px = re.compile('(?P\w+)\s=\s+(?P\d.*\d*)') In [64]: for var in px.finditer(s): print "%s = %s" %(var.group('variable'), var.group('value')) : : a = 0.1 y = 7 z = 6.7 x1 = 0.5 To filter for only the x1's just test for the group named 'variable': In [66]: for var in px.finditer(s): : if var.group('variable')=='x1': : print "%s = %s" %(var.group('variable'), var.group('value')) : : x1 = 0.5 But I'm betting these files get more complex than just the snippet you included, in which case it's probably worth looking at pyparsing http://pyparsing.wikispaces.com/ and regular expressions. -- Travis Bradyhttp://travisbrady.com/ _ 広告表示なし!アカウント有効期限なし!Hotmail Plus のお申し込みはこちら http://get.live.com/mail/options-- http://mail.python.org/mailman/listinfo/python-list
Need to help to parse the matlab log file.
I have a big log file generated by matlab. Here is a copy of part of it. I want to write a function to parse some results. For example, if I want to get the results of BandWidth and freq_offset_in_KHz, the function will print all lines with contain them as : BandWidth = 10 freq_offset_in_KHz=50 I have submited a similar question to this forum and have not got any answer. May be my first one is too simple. So I decided to send part of the log file. I could not attach the whole file since it is big and restricted by company. This is not a student homework. Thanks Frank Matlab log: ans = 24-Oct-2007 09:53:11 BandWidth = 10 freq_offset_in_KHz = 50 maxPreambMissCount = 1 maxlIterations = 300 testCasesToBeRun = 11 Bandwidth 10.00 Using config file: config_1024_V1_Acq_2 case_snr = -1 case_backoff = 20 case_refPid = 1 case_refRuns = 300 case_refTOmin = -12.9196 case_refTOmax = 20.8386 case_refTOmean = 3.8924 case_refTOstd = 3.6888 case_refFOEmin = -0.4730 case_refFOEmax = 0.3788 case_refFOEmean = 6.3300e-004 case_refFOEstd = 0.148 _ マイクロソフトの最新次世代ブラウザIE7にMSN版ならではの便利な機能をプラス http://promotion.msn.co.jp/ie7/-- http://mail.python.org/mailman/listinfo/python-list
Anyone knows how to use xemacs with ipython or python on WinXP?
Hi, I am struggling to make the ipython or python works in xemacs. I have been seraching on the internet for a solution for one day. I have put python-mode.el and ipython.el in the load-path and in the xemacs I type: M-x load library ipython. Then M-x py-shell. However, I could not get the ipython command window. emacs only creates a python window. If I exit the xemacs, it will say an active process exit, do you want to kill it? It seems the process is running but it could not display the command window. My ipython version is 0.8.1 and python-mode version is 4.76. Thanks Frank _ 10月のキーワード月間ランキング1位は「初音ミク」、2位は家族ぐるみの・・・ http://keyword.jp.msn.com/default.aspx-- http://mail.python.org/mailman/listinfo/python-list
RE: Anyone knows how to use xemacs with ipython or python on WinXP? [phishing][html-removed]
I did not byte-compile the python-mode.el. Does this change anything? I still cannot get the ipython commad prompt. Thanks Frank> Subject: RE: Anyone knows how to use xemacs with ipython or python on WinXP? [phishing][html-removed]> Date: Mon, 19 Nov 2007 15:57:59 -0500> From: [EMAIL PROTECTED]> To: python-list@python.org> > you did remember to "byte-compile" the python-mode.el file?> > > I am struggling to make the ipython or python works in > > xemacs. I have been seraching on the internet for a solution > > for one day. I have put python-mode.el and ipython.el in the > > load-path and in the xemacs I type: M-x load library ipython. > > Then M-x py-shell. However, I could not get the ipython > > command window. emacs only creates a python window. If I exit > > the xemacs, it will say an active process exit, do you want > > to kill it? It seems the process is running but it could not > > display the command window.> > > > My ipython version is 0.8.1 and python-mode version is 4.76.> > > > Thanks> > > > Frank> > _> > 10??! 1?2???> > http://keyword.jp.msn.com/default.aspx> > -- > > http://mail.python.org/mailman/listinfo/python-list> > > -- > http://mail.python.org/mailman/listinfo/python-list _ Hotmailがお届けする、幸せになるためのメールマガジン「ビジネス幸福論」実施中 http://go.windowslive.jp/-- http://mail.python.org/mailman/listinfo/python-list
how to pass parameter to a python script when running it in the interactive shell?
Hi, I am debugging a python script which takes a set of paramters. In the regular shell, I type: myscript.py --cl --cs 5 --ce 6 --bw 7 --set 1 However I want to debug the code in the interactive python shell, I do not know how to run it. I know that execfile("myscript.py") will run the script. But I could not figure out how to pass those parameters. Could some one help me to find out the solution? Thanks Frank _ ほら、変わったでしょ? マイクロソフトといっしょに、次のデジタルライフへ http://go.windowslive.jp/-- http://mail.python.org/mailman/listinfo/python-list
calling system command in window is very slow in python 2.5.1
Hi, I am running a python script that will change the attribute of a directory and its subdiretory by command: os.system("chmod -R .") or os.system("attrib -R * /S") Both commands chmod and attrib run quite fast in dos command shell. However, inside python, they are very slow and I have to kill them by Control-C. I do not know why? Can anyone help me to figure it out? Thanks Frank _ Hotmailがお届けする、幸せになるためのメールマガジン「ビジネス幸福論」実施中 http://go.windowslive.jp/-- http://mail.python.org/mailman/listinfo/python-list
Is Leo better than Vim and emacs?
Hi, Thanks for developing Leo. I have been using Vim for long time to write code. Is it a good time to switch to Leo? what is the advantage of Leo against vim and emacs? Thanks Frank _ マイクロソフトの最新次世代ブラウザIE7にMSN版ならではの便利な機能をプラス http://promotion.msn.co.jp/ie7/-- http://mail.python.org/mailman/listinfo/python-list