Re: [Tutor] Dictionary get method
On 20/03/13 14:54, Amit Saha wrote: Hello Phil, On Wed, Mar 20, 2013 at 12:54 PM, Phil wrote: Thank you for reading this. I'm working my way through a series of exercises where the author only provides a few solutions. The reader is asked to modify the histogram example so that it uses the get method thereby eliminating the if and else statements. Histogram2 is my effort. The resulting dictionary only contains the default value provided by "get" and I cannot see how the value can be incremented without an if statement. You are almost there. Note that all you have to do is increment 1 to the current 'value' for the key denoted by c. If you change the line with get() to the following, it works as you want it to: d[c]= 1 + d.get(c, 0) Output: {'a': 1, 'b': 1, 'o': 2, 'n': 1, 's': 2, 'r': 2, 'u': 1, 't': 1} histogram2 {'a': 1, 'b': 1, 'o': 2, 'n': 1, 's': 2, 'r': 2, 'u': 1, 't': 1} You were almost there. Good Luck. -Amit. Thanks Amit and Mitya, I thought I must have been close. I've played with C++ since the mid 90s and I'm finding Python very refreshing. -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionary get method
On 20/03/13 15:09, Mitya Sirenef wrote: By the way, you can further simplify it by doing: def histogram2(s): return {c: d.get(c,0)+1 for c in s} That will work in python 3, in python 2 you need: return dict((c: d.get(c,0)+1) for c in s) Thanks again Mitya, although I'm not sure it's a simplification at my present level. -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionary get method
Phil wrote: > On 20/03/13 15:09, Mitya Sirenef wrote: > > >> >> By the way, you can further simplify it by doing: >> >> def histogram2(s): >> return {c: d.get(c,0)+1 for c in s} >> >> >> That will work in python 3, in python 2 you need: >> >> return dict((c: d.get(c,0)+1) for c in s) >> > > Thanks again Mitya, although I'm not sure it's a simplification at my > present level. Especially as Mitya's code doesn't work. >>> {k: v for k, v in [(1, "a"), (2, "b")]} {1: 'a', 2: 'b'} is called "dict comprehension", it builds a dict from a list of key-value pairs. However, there is no way to reference the resulting dict while it is being built, and that is necessary for a histogram. It is possible to use dict.update() with a generator expression >>> d = {} >>> d.update((c, d.get(c, 0)+1) for c in "abba") >>> d {'a': 2, 'b': 2} but frankly, I don't see how that is better than the for loop. So as your experience with Python grows you may continue to use a loop or switch to the standard library's collections.Counter (Python3 only). ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] help related to unicode using python
Hi all i'm working with unicode using python i have some txt files in telugu i want to split all the lines of that text files in to words of telugu and i need to classify all of them using some identifiers.can any one send solution for that thank u ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help related to unicode using python
On 20/03/2013 11:38, nishitha reddy wrote: Hi all i'm working with unicode using python i have some txt files in telugu i want to split all the lines of that text files in to words of telugu and i need to classify all of them using some identifiers.can any one send solution for that thank u ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Sorry but we don't work like that. You write some code and when you get problems ask for help. I'd strongly suggest using Python 3.3 if you can for your processing. It's vastly superior to earlier, buggy unicode implementations in Python. -- Cheers. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionary get method
On 03/20/2013 04:21 AM, Peter Otten wrote: Phil wrote: On 20/03/13 15:09, Mitya Sirenef wrote: By the way, you can further simplify it by doing: def histogram2(s): return {c: d.get(c,0)+1 for c in s} That will work in python 3, in python 2 you need: return dict((c: d.get(c,0)+1) for c in s) Thanks again Mitya, although I'm not sure it's a simplification at my present level. Especially as Mitya's code doesn't work. Ah, yes - I messed up here.. I agree the loop is the best option here, vs. the example below. -m d = {} d.update((c, d.get(c, 0)+1) for c in "abba") d {'a': 2, 'b': 2} but frankly, I don't see how that is better than the for loop. So as your experience with Python grows you may continue to use a loop or switch to the standard library's collections.Counter (Python3 only). ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help related to unicode using python
On 20/03/13 22:38, nishitha reddy wrote: Hi all i'm working with unicode using python i have some txt files in telugu i want to split all the lines of that text files in to words of telugu and i need to classify all of them using some identifiers.can any one send solution for that Probably not. I would be surprised if anyone here knows what Telugu is, or the rules for splitting Telugu text into words. The Natural Language Toolkit (NLTK) may be able to handle it. You could try doing the splitting and classifying yourself. If Telugu uses space-delimited words like English, you can do it easily: data = u"ఏఐఒ ఓఔక ఞతణథ" words = data.split() As for classifying the words, I have no idea, sorry. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
> Hello, I am a beginning python student and I am having trouble with a > program I am writing. Hello, and welcome. Since you don't say if this is an assignment or not, I will just point you in the right direction, and point out a few things you might make use of. > def B1(): > period = "." You don't actually need to store a period anywhere, because when your while loop initiates, you can just do: while next1 != "." A word of advice: next is a keyword in python, so 'next1' might not be a great variable name. > # I need store the value so when while overwrites next1 with the next input > the previous input is stored and will print output when I call it later > along with last one > # I believe the solution is some how implenting this expression x = x+ > variable > while next1 != (period) : > > next1 = input("Enter the next word in you sentence or enter period:") > > if next1 == (period): > next1 = next1 + period > print ("Your sentence is:",first,next1,period) Your code will never terminate, because: program initiates. while loop begins. Enter a few words, then decide to end with a period. if-branch executes, because next1 is a period. next1 becomes next1 + period #which is .. while loop checks to see if next1 is a period, which it isn't, and runs again. Furthermore, you are continually overwriting next1 until you type a period. Consider the following output (I have added some spaces to the important parts, so as to make them stand out): >>> Enter the first word in your sentence I Enter the next word in you sentence or enter period:am Entering while loop. next1 is: am Enter the next word in you sentence or enter period:a next1 is: a Entering while loop. next1 is: a Enter the next word in you sentence or enter period:novice next1 is: novice Entering while loop. next1 is: novice Enter the next word in you sentence or enter period:. next1 is: . next1 is a period, now running next1 + period line: .. Entering while loop. next1 is: .. If you don't have to use strings for this program, I would suggest you check out lists, and especially list.append(). It is possible to write a program that does what you want, but it'd be a convoluted solution. -- best regards, Robert S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
On 03/20/2013 03:57 PM, travis jeanfrancois wrote: Hello, I am a beginning python student and I am having trouble with a program I am writing . The problem requires me to use "while" and that I create a function that allows the user to a create sentence by inputing a string and to end the sentence with a period meaning inputing "." .The problem is while keeps overwriting the previuos input and it still asks for a string even after it prints out . Here is the output I am getting Enter the first word in your sentence: I Enter the next word in your sentence: am Enter the next word in your sentence: a Enter the next word in your sentence: novice Enter the next word in your sentence: . I . . Enter the next word in your sentence: Here is the desired output: : Enter the first word in your sentence: I Enter the next word in your sentence: am Enter the next word in your sentence: a Enter the next word in your sentence: novice Enter the next word in your sentence: . I am a novice. Here is my code: def B1(): #Creates a function called B1 period = "." # The variable period is assigned first = input("Enter the first word in your sentence ") #The variable first is assigned next1 = input("Enter the next word in you sentence or enter period:") #The variable next 1 is assigned # I need store the value so when while overwrites next1 with the next input the previous input is stored and will print output when I call it later along with last one # I believe the solution is some how implenting this expression x = x+ variable You need a new variable that accumulates the whole sentence. I'd use a list, but many people would use a string. Since I don't know what concepts you know yet, I'll stick to string here. Anyway, you have to initialize it before the loop, and then you can print it after the loop. sentence = "" while next1 != (period) : next1 = input("Enter the next word in you sentence or enter period:") At this point, add the word to the sentence. if next1 == (period): next1 = next1 + period print ("Your sentence is:",first,next1,period) No need for these three lines inside the loop, since the loop will end when next1 is equal to the period. So put the print after the end of the loop, and I'll let you figure out what it should print. PS : The" #" is I just type so I can understand what each line does -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
On Wednesday 2013 March 20 13:39, Robert Sjoblom wrote: > A word of advice: next is a keyword in python ~/Packages/Python/Notable-0.1.5b> python Python 2.5 (r25:51908, May 25 2007, 16:14:04) [GCC 4.1.2 20061115 (prerelease) (SUSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import keyword >>> keyword.iskeyword('next') False >>> 14:44 Wed 2013 Mar 20 ~/Packages/Python/Notable-0.1.5b> python2.7 Python 2.7.2 (default, Oct 10 2011, 10:47:36) [GCC 4.1.2 20061115 (prerelease) (SUSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import keyword >>> keyword.iskeyword('next') False >>> 14:44 Wed 2013 Mar 20 ~/Packages/Python/Notable-0.1.5b> python3.3 Python 3.3.0 (default, Sep 30 2012, 09:02:56) [GCC 4.1.2 20061115 (prerelease) (SUSE Linux)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import keyword >>> keyword.iskeyword('next') False >>> -- Yonder nor sorghum stenches shut ladle gulls stopper torque wet strainers. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
On Mar 20, 2013 10:49 p.m., "xDog Walker" wrote: > > On Wednesday 2013 March 20 13:39, Robert Sjoblom wrote: > > A word of advice: next is a keyword in python > > ~/Packages/Python/Notable-0.1.5b> python > Python 2.5 (r25:51908, May 25 2007, 16:14:04) > [GCC 4.1.2 20061115 (prerelease) (SUSE Linux)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import keyword > >>> keyword.iskeyword('next') > False > >>> > 14:44 Wed 2013 Mar 20 > ~/Packages/Python/Notable-0.1.5b> python2.7 > Python 2.7.2 (default, Oct 10 2011, 10:47:36) > [GCC 4.1.2 20061115 (prerelease) (SUSE Linux)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import keyword > >>> keyword.iskeyword('next') > False > >>> > 14:44 Wed 2013 Mar 20 > ~/Packages/Python/Notable-0.1.5b> python3.3 > Python 3.3.0 (default, Sep 30 2012, 09:02:56) > [GCC 4.1.2 20061115 (prerelease) (SUSE Linux)] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import keyword > >>> keyword.iskeyword('next') > False > >>> > > -- > Yonder nor sorghum stenches shut ladle gulls stopper torque wet > strainers. Fine, it's a method, my bad. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Scripting Calligra sheets with Python
On 03/19/2013 09:16 PM, Jim Byrnes wrote: On 03/18/2013 11:25 PM, Dave Angel wrote: On 03/18/2013 09:56 PM, Jim Byrnes wrote: On 03/18/2013 07:54 PM, Dave Angel wrote: On 03/18/2013 12:18 PM, Jim Byrnes wrote: I am trying to script Calligra Sheets (formerly KSpread) with python. I have gotten some of the included example scripts to run so I know python scripting is running. I found the following snippet on their website: import KSpread sheet = KSpread.view().sheet() # swap text of B5 and C6 t1 = sheet.text("B5") t2 = sheet.text(6,3) sheet.setText("B5", t2) sheet.setText(6, 3, t1) # swap value of D7 and E8 v1 = sheet.value("D7") v2 = sheet.value(8,5) sheet.setValue("D7", v2) sheet.setValue(8, 5, v1) Error: 'str' object has no attribute 'text' File "file:///usr/share/kde4/apps/sheets/scripts/extensions/myswap.py", line 4, in This error message appeared in a dialog box. I've taught myself some Python but I don't understand what is happening here. Shouldn't t1 be a sheet object? What would cause it to be a str instead? Since somebody has censored the rest of the error traceback, we can't even tell what line is giving the error. Assuming you know it's t1 = sheet.text("85") I think line wrapping obscured that it is line 4 t1 = sheet.text("B5"). I saw the line 4, but didn't see anyplace where it showed me line 4. So i had to guess. And for all I knew, the error isn't happening on that line, but on some line called indirectly by that one. The full stack trace would be reassuring. But apparently your Calligra environment is censoring it. then it has nothing to do with the type of t1, but with the type of sheet. If that's the case, then print out type(sheet) and see what it actually is. Then consult the documentation for the KSpread.view().sheet() function and see what it's documented to return. The docs say: Returns the KSpread::ViewAdaptor object in which the document is displayed. I would like to follow your advice and print out type(sheet) but right now I don't know how. This is all running inside Calligra sheets and so far the only thing I have gotten out if it is the error message. Are you permitted to edit this myswap.py file? If so, add a line that prints the useful information immediately before the line which causes the exception. And if print is also swallowed by your helpful environment, then write to a file. Yes I can and it does swallow it, so I redirected to a file and it says . Same as the error message. So now I need to figure out why it's not returning the object expected. I can run the sample scripts that were installed but not something I originate. I guess I must be missing something procedural that is keeping them from running properly. Thanks for you help. Regards, Jim Just a follow up for completeness in case some else finds this and has the same problem. Replace line 2 sheet = KSpread.view().sheet() with sheetname = KSpread.currentSheet().sheetName() sheet = KSpread.sheetByName(sheetname) Then it will run with no errors. Regards, Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
On 20/03/13 19:57, travis jeanfrancois wrote: I create a function that allows the user to a create sentence by inputing a string and to end the sentence with a period meaning inputing "." .The problem is while keeps overwriting the previuos input 'While' does not do any such thing. Your code is doing that all by itself. What while does is repeat your code until a condition becomes false or you explicitly break out of the loop. Here is my code: def B1(): Try to give your functions names that describe what they do. B1() is meaningless, readSentence() would be better. period = "." # The variable period is assigned Its normal programming practice to put the comment above the code not after it. Also comments should indicate why you are doing something not what you are doing - we can see that from the code. first = input("Enter the first word in your sentence ") next1 = input("Enter the next word in you sentence or enter period:") # I need store the value so when while overwrites next1 with the next input the previous input is stored and will print output when I call it later along with last one # I believe the solution is some how implenting this expression x = x+ variable You could be right. Addition works for strings as well as numbers. Although there are other (better) options but you may not have covered them in your class yet. while next1 != (period) : You don;t need the parentheses around period. Also nextWord might be a better name than next1. Saving 3 characters of typing is not usually worthwhile. next1 = input("Enter the next word in you sentence or enter period:") Right, here you are overwriting next1. It's not the while's fault - it is just repeating your code. It is you who are overwriting the variable. Notice that you are not using the first that you captured? Maybe you should add next1 to first at some point? Then you can safely overwrite next1 as much as you like? if next1 == (period): Again you don;t need the parentheses around period next1 = next1 + period Here, you add the period to next1 which the 'if' has already established is now a period. print ("Your sentence is:",first,next1,period) And now you print out the first word plus next1 (= 2 periods) plus a period = 3 periods in total... preceded by the phrase "Your sentence is:" This tells us that the sample output you posted is not from this program... Always match the program and the output when debugging or you will be led seriously astray! PS : The" #" is I just type so I can understand what each line does The # is a comment marker. Comments are a very powerful tool that programmers use to explain to themselves and other programmers why they have done what they have. When trying to debug faults like this it is often worthwhile grabbing a pen and drawing a chart of your variables and their values after each time round the loop. In this case it would have looked like iteration period first next1 0 . I am 1 . I a 2 . I novice 3 . I .. If you aren't sure of the values insert a print statement and get the program to tell you, but working it out in your head is more likely to show you the error. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help related to unicode using python
Reply inline. On 21/03/13 12:18 AM, Steven D'Aprano wrote: > On 20/03/13 22:38, nishitha reddy wrote: >> Hi all >> i'm working with unicode using python >> i have some txt files in telugu i want to split all the lines of that >> text files in to words of telugu >> and i need to classify all of them using some identifiers.can any one >> send solution for that > > > Probably not. I would be surprised if anyone here knows what Telugu is, > or the rules for splitting Telugu text into words. The Natural Language > Toolkit (NLTK) may be able to handle it. > > You could try doing the splitting and classifying yourself. If Telugu > uses > space-delimited words like English, you can do it easily: > > data = u"ఏఐఒ ఓఔక ఞతణథ" > words = data.split() Unicode characters for telugu: http://en.wikipedia.org/wiki/Telugu_alphabet#Unicode On python 3.x, >>> import re >>> a='ఏఐఒ ఓఔక ఞతణథ' >>> print(a) ఏఐఒ ఓఔక ఞతణథ >>> re.split('[^\u0c01-\u0c7f]', a) ['ఏఐఒ', 'ఓఔక', 'ఞతణథ'] Similar logic can be used for any other Indic script. HTH. -- शंतनू ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor