Question on Manipulating List and on Python
Dear Group, I have two questions one on list manipulation and other on Python. (i) I have a file of lists. Now, the first digit starts with a number or index, like, [001, "Obama", "USA", "President"] [002 "Major", "UK", "PM"] [003 "Singh", "INDIA", "PM"] Initially, I am reading the file and taking as for line in file: line_word=line.split print line_word I am getting the above result. But, my problem is if I read the array position 0, which is a number then it should give me reciprocal words like if word1=line_word[0] if word1==001: I should get line_word[1], line_word[2] reciprocating the value. Predefining it solves the problem, but I want to capture and do as it iterates. If any one can help? (ii) My second question is posted by one of my colleagues, what makes Python so fast? Regards, Subhabrata Banerjee. -- http://mail.python.org/mailman/listinfo/python-list
Re: Question on Manipulating List and on Python
On Sep 29, 9:27 pm, John Gordon wrote: > In > Subhabrata Banerjee writes: > > > (i) I have a file of lists. Now, the first digit starts with a number > > or index, like, > > [001, "Obama", "USA", "President"] > > [002 "Major", "UK", "PM"] > > [003 "Singh", "INDIA", "PM"] > > Initially, I am reading the file and taking as > > for line in file: > > line_word=line.split > > print line_word > > This isn't your actual code. Please show us your real code, along with > a sample of your input file. > > > (ii) My second question is posted by one of my colleagues, what makes > > Python so fast? > > Fast compared to what? Why does your colleague believe it should be > slower? > > -- > John Gordon A is for Amy, who fell down the stairs > gor...@panix.com B is for Basil, assaulted by bears > -- Edward Gorey, "The Gashlycrumb Tinies" Hi John, The actual code is till now is: def name_debugger(n): open_file=open("/python27/name1.txt") for line in open_file: line_word=line.split() #print line_word word1=line_word[0] print word1 And Python seems faster than C++/Java. It is indeed. I also experience it. Regards, Subhabrata Banerjee. -- http://mail.python.org/mailman/listinfo/python-list
Re: Question on Manipulating List and on Python
On Sep 30, 12:52 am, "Prasad, Ramit" wrote: > -Original Message- > From: python-list-bounces+ramit.prasad=jpmorgan@python.org > [mailto:python-list-bounces+ramit.prasad=jpmorgan@python.org] On Behalf > Of Chris Angelico > Sent: Thursday, September 29, 2011 11:51 AM > To: python-l...@python.org > Subject: Re: Question on Manipulating List and on Python > > On Fri, Sep 30, 2011 at 2:36 AM, Subhabrata Banerjee > wrote: > > And Python seems faster than C++/Java. It is indeed. I also experience > > it. > > Python compared to Java? Difficult to compare. Python to C++? > Impossible to compare. But performance depends MASSIVELY on > algorithmic quality; if you code the exact same thing in Python and in > C++, you would probably find that the C++ one is faster, but chances > are you're implementing approximately the same thing in two quite > different ways. Or possibly you're using a slow and inefficient > library or third-party function. > > I've sometimes written code in one language and directly ported it to > another, and then run both on the same hardware. With most such > experiments, CPython generally doesn't perform all that well, and C or > C++ rocks. But frequently, the C/C++ code is more verbose and more > error-prone than the Python - because Python's biggest boast is not > that it's fast, but that it's *fast enough*, while being easy to work > with. (And every once in a while there's something where I want to use > a pointer to some other variable, and that's a concept that just plain > doesn't work in Python. You have references to objects, but you can't > from one place change another variable, without using some kind of > mutable object that they both reference.) > --- > > I think Steven D'Aprano had an excellent post on this a week or so ago (on > the tutor list, not this one). > > See:http://mail.python.org/pipermail/tutor/2011-September/085573.html > > Ramit > > Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology > 712 Main Street | Houston, TX 77002 > work phone: 713 - 216 - 5423 > > ChrisA > --http://mail.python.org/mailman/listinfo/python-list > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of > securities, accuracy and completeness of information, viruses, > confidentiality, legal privilege, and legal entity disclaimers, > available athttp://www.jpmorgan.com/pages/disclosures/email. > > Dear Group, Thanks for your suggestions. I'll check if these work. Converting to dictionary I was thinking but others I have to test. Without patting my own back(I do not write algorithms that good), I can say Python is indeed damn fast. A reason many of my friends are shifting fast to Python and I was suggested by a reputed person of MIT CogLab to use Python instead of C++ and it worked just fabulous for me. To my collegue I would find out an answer. Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list
Re: File handling problem.
Dear Group, I am working on a code like the following: from decimal import* #SAMPLE TEST PROGRAM FOR FILE def sample_file_test(n): #FILE FOR STORING PROBABILITY VALUES open_file=open("/python26/Newfile1.txt","r+") #OPENING OF ENGLISH CORPUS open_corp_eng=open("/python26/TOTALENGLISHCORPUS1.txt","r") #READING THE ENGLISH CORPUS corp_read=open_corp_eng.read() #CONVERTING THE CORPUS FILE IN WORDS corp_word=corp_read.split() #EXTRACTING WORDS FROM CORPUS FILE OF WORDS for word in corp_word: #COUNTING THE WORD count1=corp_word.count(word) #COUNTING TOTAL NUMBER OF WORDS count2=len(corp_word) #COUNTING PROBABILITY OF WORD count1_dec=Decimal(count1) count2_dec=Decimal(count2) getcontext().prec = 6 prob_count=count1_dec/count2_dec print prob_count string_of_prob_count=str(prob_count) file_input_val=open_file.write(string_of_prob_count) open_file.close() The problems I am getting: (i) The probability values are not being stored properly in file. (ii)“Newfile1.txt” is storing not the series of values but an arbitrary value from series 0.0143096 (iii) As I was testing it again it gave me another error Traceback (most recent call last): File "", line 1, in sample_file_test(1) File "C:\Python26\testprogramforfiles1.py", line 25, in sample_file_test file_input_val=open_file.write(string_of_prob_count) ValueError: I/O operation on closed file If you can kindly let me know what is the wrong I am doing here. As I took out the code pasted it in MS-Word before posting, there may be slight indentation problem. Best Regards, Subhabrata. On Sat, May 2, 2009 at 2:16 PM, Pascal Chambon wrote: > subhakolkata1...@gmail.com a écrit : > >> Dear Group, >> >> I am using Python2.6 and has created a file where I like to write some >> statistical values I am generating. The statistical values are >> generating in a nice way, but as I am going to write it, it is not >> taking it, the file is opening or closing properly but the values are >> not getting stored. It is picking up arbitrary values from the >> generated set of values and storing it. Is there any solution for it? >> >> Best Regards, >> SBanerjee. >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> >> >> > Hello > > Could you post excerpt of your file-handling code ? > It might be a buffering problem (although when the file closes, I think > buffers get flushed), else it's really weird... > > Regards, > pascal > > -- http://mail.python.org/mailman/listinfo/python-list
Re: File handling problem.
Dear Sir, Thanx for your prompt reply, I would be trying to work on your suggestion and get back to you as soon as possible. Best Regards, Subhabrata. On Sun, May 3, 2009 at 10:47 PM, Chris Rebert wrote: > On Sun, May 3, 2009 at 9:51 AM, SUBHABRATA BANERJEE > wrote: > > Dear Group, > > > > > > > > I am working on a code like the following: > > > > > > > > from decimal import* > > > > #SAMPLE TEST PROGRAM FOR FILE > > > > def sample_file_test(n): > > > > #FILE FOR STORING PROBABILITY VALUES > > > > open_file=open("/python26/Newfile1.txt","r+") > > Is there a reason you must output the results to the same file the > input came from? It's possible this is part of your problems. > > > > > #OPENING OF ENGLISH CORPUS > > > > open_corp_eng=open("/python26/TOTALENGLISHCORPUS1.txt","r") > > > > #READING THE ENGLISH CORPUS > > > > corp_read=open_corp_eng.read() > > > > #CONVERTING THE CORPUS FILE IN WORDS > > > > corp_word=corp_read.split() > > > > #EXTRACTING WORDS FROM CORPUS FILE OF WORDS > > > > for word in corp_word: > > > > #COUNTING THE WORD > > > > count1=corp_word.count(word) > > Note: Your program is currently O(N^2) rather than O(N) because you > re-count the number of occurrences of each word /on every occurrence > of the word/. > > > #COUNTING TOTAL NUMBER OF WORDS > > > > count2=len(corp_word) > > > > #COUNTING PROBABILITY OF WORD > > > > count1_dec=Decimal(count1) > > > > count2_dec=Decimal(count2) > > > > getcontext().prec = 6 > > > > prob_count=count1_dec/count2_dec > > > > print prob_count > > > > string_of_prob_count=str(prob_count) > > > > file_input_val=open_file.write(string_of_prob_count) > > > > open_file.close() > > You shouldn't be closing the file until the /entire loop/ has finished > writing to the file. So the previous line should be dedented. > > > > > > > > > The problems I am getting: > > > > (i) The probability values are not being stored properly > in > > file. > > Also, you're currently not putting any separator between consecutive > entires, so it's all going to run together as one long line. > Have you considered using one of the std lib modules to output the > file in a well-defined human-readable format such as JSON or CSV? > > > (ii)“Newfile1.txt” is storing not the series of values > but > > an arbitrary value from series 0.0143096 > > > > (iii) As I was testing it again it gave me another error > > > > Traceback (most recent call last): > > > > File "", line 1, in > > > > sample_file_test(1) > > > > File "C:\Python26\testprogramforfiles1.py", line 25, in > sample_file_test > > > > file_input_val=open_file.write(string_of_prob_count) > > > > ValueError: I/O operation on closed file > > > Cheers, > Chris > -- > http://blog.rebertia.com > -- http://mail.python.org/mailman/listinfo/python-list
Re: Questions on XML
Should I help you? If you answered my questions I am differing from your view I do not get any problem in processing Hindi or Bangla or any Indian language in Python it is perfectly fine. Best Regards, Subhabrata. On Sat, Aug 22, 2009 at 9:48 AM, Rami Chowdhury wrote: > > I am using primarily UTF-8 based strings, like Hindi or Bengali. Can I >> use Python to help me in this regard? >> > > I can say from experience that Python on Windows (at least, Python 2.5 on > 32-bit Vista) works perfectly well with UTF-8 files containing Bangla. I > have had trouble with working with the data in IDLE, however, which seems to > prefer ASCII by default. > > - > Rami Chowdhury > "Never assume malice when stupidity will suffice." -- Hanlon's Razor > 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) > > > > > > On Aug 21, 2009, at 19:15 , joy99 wrote: > > Dear Group, >> >> I like to convert some simple strings of natural language to XML. May >> I use Python to do this? If any one can help me, on this. >> >> I am using primarily UTF-8 based strings, like Hindi or Bengali. Can I >> use Python to help me in this regard? >> >> How can I learn good XML aspects of Python. If any one can kindly name >> me a book or URL. >> >> I am using Python2.6 on Windows XP with IDLE as GUI. >> >> Best Regards, >> Subhabrata. >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Questions on XML
I was trying this. Looks perfectly fine. There must be something really wrong. If you can reinstall Python 2.5. >>> a1=raw_input("String") Stringআম On Mon, Aug 24, 2009 at 12:18 AM, Rami Chowdhury wrote: > My problem is with IDLE on Windows. When I try to type Bangla directly into > the IDLE window I only get '?' characters, and repr() fails with a > UnicodeDecodeError. I expect, though, that that's because of my specific > installation / Windows issues, as it works fine on Fedora 10... > > > I do not get any problem in processing Hindi or Bangla or any Indian > > language in Python it is perfectly fine. > I have no problems either -- my issues are with IDLE, and only on Windows. > > > Rami Chowdhury > "Strangers are just friends who haven't had enough gin." -- Howdle's Saying > 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) > > On Saturday 22 August 2009 13:27:02 SUBHABRATA BANERJEE wrote: > > Should I help you? If you answered my questions I am differing from your > > view I do not get any problem in processing Hindi or Bangla or any Indian > > language in Python it is perfectly fine. > > Best Regards, > > Subhabrata. > > > > On Sat, Aug 22, 2009 at 9:48 AM, Rami Chowdhury > wrote: > > > I am using primarily UTF-8 based strings, like Hindi or Bengali. Can I > > > > > >> use Python to help me in this regard? > > > > > > I can say from experience that Python on Windows (at least, Python 2.5 > on > > > 32-bit Vista) works perfectly well with UTF-8 files containing Bangla. > I > > > have had trouble with working with the data in IDLE, however, which > seems > > > to prefer ASCII by default. > > > > > > - > > > Rami Chowdhury > > > "Never assume malice when stupidity will suffice." -- Hanlon's Razor > > > 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) > > > > > > > > > > > > > > > > > > On Aug 21, 2009, at 19:15 , joy99 wrote: > > > > > > Dear Group, > > > > > >> I like to convert some simple strings of natural language to XML. May > > >> I use Python to do this? If any one can help me, on this. > > >> > > >> I am using primarily UTF-8 based strings, like Hindi or Bengali. Can I > > >> use Python to help me in this regard? > > >> > > >> How can I learn good XML aspects of Python. If any one can kindly name > > >> me a book or URL. > > >> > > >> I am using Python2.6 on Windows XP with IDLE as GUI. > > >> > > >> Best Regards, > > >> Subhabrata. > > >> -- > > >> http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list
Re: File Handling Problem
Dear Sir, Thank you for your kind reply. I would surely check your code. Meanwhile, I solved it using readlines() but not in your way. I will definitely have a look in your code. My solution came so smart that I felt I should not have posted this question. But I would like to know about, i) Fileinput. ii) Ast. And one small question does Python has any increment operator like ++ in C. Thank you for taking out the time to write answer for me. Wishing you a happy day ahead, Best Regards, Subhabrata. On Fri, Sep 4, 2009 at 8:43 PM, Lucas Prado Melo wrote: > On Fri, Sep 4, 2009 at 9:50 AM, joy99 wrote: > >> Dear Group, >> >> I have a file. The file has multiple lines. I want to get the line >> number of any one of the strings. >> Once I get that I like to increment the line number and see the string >> of the immediate next line or any following line as output. The >> problem as I see is nicely handled in list, >> > > You could just grab each line and associate that line's content with its > line numbers, like this: > f = open("my_file.txt", "r") > line2pos = {} > for line_num, line in enumerate(f.readlines()): > if line not in line2pos: > line2pos[line] = [] > line2pos[line].append(line_num) > (...) > > This approach would be nice when you don't know which string to look for > beforehand. > In the case you already know it, there's no need for keeping the line2pos > variable, the right approach should be to iterate through the lines of the > file and, when you see the string you need, just display the next line and > exit: > def lookNextLineAfterString(s) > f = open("my_file.txt", "r") > found = False > while True: > line = f.readline() > if line == '': > break > line = line[:-1] #stripping the newline > if line == s: > found = True > break > if found == True: > nextString = f.readline() > if nextString == '': > print "String found but there's no next string" > else: > print "Next string is ", nextString[:-1] > else: > print "String not found" > -- http://mail.python.org/mailman/listinfo/python-list