where the function has problem? n = 900 is OK , but n = 1000 is ERROR
# Get Fibonacci Value #Fibonacci(N) = Fibonacci(N-1) + Fibonacci(N-2) # # n = 900 is OK # n = 1000 is ERROR , Why # # What Wrong? # cache = [] def fibo( n ): try: if cache[n] != -1: return cache[n] else: if 0 == n: r = 0 elif 1 == n: r = 1 else: r = fibo(n-1) + fibo(n-2) cache[n] = r return r except: print "EXCEPT: " + str(n) if __name__ == '__main__': # This n = 900 is OK # But n = 1000 is ERROR n = 900 cache = range(0 , n + 1 , 1) for i in cache: cache[i] = -1 print "Fibo(" + str(n) + ") = " + str(fibo(n)) print "\n" print "\n" -- http://mail.python.org/mailman/listinfo/python-list
If One Line
Hello, Is it possible in python: if ((x = a(b,c)) == 'TRUE'): print x Thanks. -- https://mail.python.org/mailman/listinfo/python-list
CSV Error
Hello, I am trying to read a csv file using DictReader. I am getting error - Traceback (most recent call last): File "", line 1, in r.fieldnames File "/usr/lib/python2.7/csv.py", line 90, in fieldnames self._fieldnames = self.reader.next() ValueError: I/O operation on closed file Here is my code in a Python shell - >>> with open('x.csv','rb') as f: ... r = csv.DictReader(f,delimiter=",") >>> r.fieldnames I have tried to open the file in 'rU', 'r' mode. But still I am getting the above error. Please help. Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Re: CSV Error
On Sun, 28 Dec 2014 06:19:58 -0600, Skip Montanaro wrote: >> ValueError: I/O operation on closed file >> >> Here is my code in a Python shell - >> >> >>> with open('x.csv','rb') as f: >> ... r = csv.DictReader(f,delimiter=",") >> >>> r.fieldnames > > The file is only open during the context of the with statement. Indent > the last line to match the assignment to r and you should be fine. > > Skip > ValueError: I/O operation on closed file > > > > Here is my code in a Python shell - > > > > >>> with open('x.csv','rb') as f: > > ... r = csv.DictReader(f,delimiter=",") > > >>> r.fieldnames > The file is only open during the context of the with > statement. Indent the last line to match the assignment to r and you > should be fine. > Skip I have indented the line. I am working in the shell. The error is still there. Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Re: CSV Error
On Sun, 28 Dec 2014 14:41:55 +0200, Jussi Piitulainen wrote: > Skip Montanaro writes: > >> > ValueError: I/O operation on closed file >> > >> > Here is my code in a Python shell - >> > >> > >>> with open('x.csv','rb') as f: >> > ... r = csv.DictReader(f,delimiter=",") >> > >>> r.fieldnames >> >> The file is only open during the context of the with statement. Indent >> the last line to match the assignment to r and you should be fine. > > Or, don't use "with" when experimenting in the shell. > >>>> import csv f = open('x.csv') >>>> r = csv.DictReader(f, delimiter = ',') >>>> r.fieldnames >['Foo', 'Bar'] >>>> Yes, Thanks. It's fixed. Thanks. -- https://mail.python.org/mailman/listinfo/python-list
CSV Dictionary
Hello, I have csv file like: id,name,userid,password 1,ABC,def@ghi,1234 2,DEF,ghi@jkl,asdf 3,GHI,jkl@mno,zxcv . . . I need to get that csv file into a dictionary. Here is my code: import csv with open('x.csv','rb') as f: rdr = csv.DictReader(f,delimiter=',') flds = rdr.fieldnames rows = 0 d = {} for row in rdr: rows += 1 i = 0 for i in range(len(flds)): d[flds[i]] = row[flds[i]] It shows only the last record, like: d = {'Password': 'zxcv', 'UserId': 'jkl@mno', 'id': '3', 'Name': 'GHI'} How could I get the all the records? Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Re: CSV Dictionary
On Mon, 29 Dec 2014 09:47:23 -0600, Skip Montanaro wrote: > On Mon, Dec 29, 2014 at 9:35 AM, JC wrote: >> How could I get the all the records? > > This should work: > > with open('x.csv','rb') as f: > rdr = csv.DictReader(f,delimiter=',') > rows = list(rdr) > > You will be left with a list of dictionaries, one dict per row, keyed by > the values in the first row: > >>>> import csv with open('x.csv','rb') as f: > ... rdr = csv.DictReader(f,delimiter=',') > ... rows = list(rdr) > ... >>>> import pprint pprint.pprint(rows) > [{'id': '1', 'name': 'ABC', 'password': '1234', 'userid': 'def@ghi'}, > {'id': '2', 'name': 'DEF', 'password': 'asdf', 'userid': 'ghi@jkl'}, > {'id': '3', 'name': 'GHI', 'password': 'zxcv', 'userid': 'jkl@mno'}] > > Skip Thanks. That did the job. But now I see another problem. I cannot use the "rdr" object anymore. For example, I wanted to count the number of records. I used - count = sum(1 for r in rdr) It returned 0 records. Do I have to open the file again to get 'rdr' work again? Thanks again. -- https://mail.python.org/mailman/listinfo/python-list
Re: CSV Dictionary
On Mon, 29 Dec 2014 10:32:03 -0600, Skip Montanaro wrote: > On Mon, Dec 29, 2014 at 10:11 AM, JC wrote: >> Do I have to open the file again to get 'rdr' work again? > > Yes, but if you want the number of records, just operate on the rows > list, e.g. len(rows). > > Skip Yes, I did that. But if I need to use 'rdr' again, I have to open it again, right? Is there any ways to get 'rdr' point to first record again? Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Re: newbie graphing recommendations ?
Adam wrote: > Where should a py newbie start to do some 2D graphs on screen ? > > PythonGraphApi, > Gato, looks interesting > pygraphlib, > matplotlib, > > is there a best native Python place to start ? > > > If you are going to be in wxPython try the 'PyPlot.py' examples in the wxDemo. You will need to read the source cos' there is no official documentation. Matlibplot is also good but difficult because of all the options and lack of documentation API. -- http://mail.python.org/mailman/listinfo/python-list