Re: [Tutor] string rules for 'number'
On 08/10/12 11:51, Arnej Duranovic wrote: Alright guys, I appreciate all your help SO much. I know understand, as the gentleman above said A string is a string is a string doesn't matter what is in it and they are ordered the same way...BUT this is what was going through my head. Since letters are ordered in such a way that A is less than B, for example, I thought the same applied to numbers, It does. 1 comes before 2, just like A comes before B. And 12345 comes before 2, just like Apple comes before B. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Through a glass, darkly: the datetime module
Steven D'Aprano wrote: It is a little-known fact that Unix sys admins, and C programmers, can only type a fixed number of keys before their brains explode. Sad but true. Since nobody knows how many keys that will be, but only that it is fixed at birth, they have a horror of typing four characters when two would do. Hence the standard Unix directories /usr and /mnt instead of /user and /mount, and the Unix commands: Odd then that they eschew the usage of the mouse, a device which would prolong their lives. 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 at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] finding a number with str.find
Is there a way to find the next character that is a digit (0-9) in a string? It seems that the str.find method can only find one particular character, rather than any character from among many. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] finding digit in string
I figured out a solution for the question I asked on here. To find the next digit (0-9) in a string, I use: text.find(0 or 1 or 2, etc...) But is there a more elegant way to do this? The way I found uses a lot of typing. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding a number with str.find
On 08/10/12 17:33, Benjamin Fishbein wrote: Is there a way to find the next character that is a digit (0-9) in a string? it seems that the str.find method can only find one particular character, When looking for patterns rather than literal values you need to use regular expressions. These can get very complex very quickly but searching for a number is not too bad. One simple way (although not optimal) is to put the characters you want inside square brackets import re s = a string with 7 words in it res = re.search([0-9], s) res.group() '7' This returns something called a match object which can tell you what was found. You can then use that to find the index in the string. s.index( res.group() ) 14 Having digits in a search pattern is such a common thing that there is a special syntax for that - '\d' : s.index( re.search(\d, s).group() ) 14 Finally you can find all occurrences of a pattern in a string using re.findall() which returns a list of the found matches: re.findall(\d,s) # only one occurrence in this case... ['7'] you can then loop through the results to locate each item. Regular expressions are way more powerful than this and there can be a tendency to overuse them. This should be resisted since they can introduce very hard to find bugs due to subtle errors in the patter specification. You can find a very gentle introduction to their other features in the re topic of my tutorial. 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] finding digit in string
On 08/10/2012 17:43, Benjamin Fishbein wrote: I figured out a solution for the question I asked on here. To find the next digit (0-9) in a string, I use: text.find(0 or 1 or 2, etc...) But is there a more elegant way to do this? The way I found uses a lot of typing. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor How about something like:- for ch in text: if ch.isdigit(): doSomething(ch) or:- for ch in text: if '0' = ch = '9': doSomething(ch) If you want to use the offset for any reason use enumerate:- for i, ch in enumerate(text): etc. -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding digit in string
On 08/10/12 17:43, Benjamin Fishbein wrote: I figured out a solution for the question I asked on here. To find the next digit (0-9) in a string, I use: text.find(0 or 1 or 2, etc...) Are you sure that worked? It doesn't for me on Python 2.7... s 'a string with 7 words in it' s.find('4' or '5' or '7') -1 s.find('7') 14 -- 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] finding digit in string
On 10/08/2012 12:43 PM, Benjamin Fishbein wrote: I figured out a solution for the question I asked on here. Why then did you start a new thread, instead of responding on the same one? You didn't even use the same subject string. To find the next digit (0-9) in a string, I use: text.find(0 or 1 or 2, etc...) That won't work. 0 or 1 is just 0 So you're searching for the character zero. But is there a more elegant way to do this? The way I found uses a lot of typing. See the other thread you started, with subject [Tutor] finding a number with str.find -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding digit in string
On 10/8/2012 12:43 PM, Benjamin Fishbein wrote: I figured out a solution for the question I asked on here. To find the next digit (0-9) in a string, I use: text.find(0 or 1 or 2, etc...) But is there a more elegant way to do this? The way I found uses a lot of typing. My way is: import string tt = string.maketrans('0123456789','00') ts = asdf3456'.translate(t) # yields 'asdf' ts.find(0) # finds the next 0 which is in the same position as corresponding digit -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding a number with str.find
On 09/10/12 03:33, Benjamin Fishbein wrote: Is there a way to find the next character that is a digit (0-9) in a string? It seems that the str.find method can only find one particular character, rather than any character from among many. Correct. For more complicated searching needs, either use a proper parser to parse the string as needed, or use a regular expression as a kind of super-find. import re re.search(r'\d', 'I 8 sandwiches').start() # returns 2 Beware though: there is a saying about regular expressions, and in particular about the sort of people who reach for a reg ex to solve nearly every problem: Some people, when faced with a problem, think 'I know, I'll use a regular expression'. Now they have two problems. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Through a glass, darkly: the datetime module
Sander Sweers schreef: Roel Schroeven schreef op zo 07-10-2012 om 21:19 [+0200]: Sander Sweers schreef: Op 7 okt. 2012 04:29 schreef aklei...@sonic.net mailto:aklei...@sonic.net het volgende: I'm also not sure but I seem to remember that it is (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) which I think is extremely clever because it gets around the problem created by the fact that some people (misguided in my view) begin the week with Sunday instead of ending with it. This has religious reasons, see http://en.wikipedia.org/wiki/Week#Systems_derived_from_the_seven-day_week. Notable exception to the rule that Sunday is the first day of the week in europe is Italy, they consider Monday the first day of the week. As far as I know, monday is generally considered the first day of the week here in Belgium. I never knew it was different in other European countries; I thought it was America that starts the week on sunday. Was I wrong then? As far as I know also in Belgium Sunday is officially the first day of the week. Look at the calendar and check what is the leftmost day. My guess this is the same as your northern neighbor, Sunday ;-). On the contrary, I can't remember ever having seen a non-foreign calendar with weeks starting on Sunday. For example: http://www.antwerpen.be/docs/Stad/Bedrijven/Sociale_zaken/SZ_Milieu/afval/ophaalkalender_3_m.pdf I know one example doesn't proof much, but that calendar certainly looks very familiar to me, while Sunday-based calendars always feel weird. A Google images search for kalender also results mostly in Monday-based calendars for me. All of that doesn't say very much about which one is officially sanctioned as the first day of the week. I don't have the faintest idea where that piece of information should be available. Maybe it's not defined anywhere. Belgium, like European countries, has largely christian roots, which does suggest Sunday as the first day of the week. But I think the christian rules on such details have had very little impact on civilian life for many decades. Regards, Roel -- Too often we hold fast to the cliches of our forebears. We subject all facts to a prefabricated set of interpretations. Too often we enjoy the comfort of opinion without the discomfort of thought. -- John F Kennedy r...@roelschroeven.net ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string rules for 'number'
On 8 October 2012 03:19, eryksun eryk...@gmail.com wrote: As a supplement to what's already been stated about string comparisons, here's a possible solution if you need a more 'natural' sort order such as '1', '5', '10', '50', '100'. You can use a regular expression to split the string into a list of (digits, nondigits) tuples (mutually exclusive) using re.findall. For example: import re dndre = re.compile('([0-9]+)|([^0-9]+)') re.findall(dndre, 'a1b10') [('', 'a'), ('1', ''), ('', 'b'), ('10', '')] Use a list comprehension to choose either int(digits) if digits is non-empty else nondigits for each item. For example: [int(d) if d else nd for d, nd in re.findall(dndre, 'a1b10')] ['a', 1, 'b', 10] Now you have a list of strings and integers that will sort 'naturally' compared to other such lists, since they compare corresponding items starting at index 0. All that's left to do is to define this operation as a key function for use as the key argument of sort/sorted. For example: import re def natural(item, dndre=re.compile('([0-9]+)|([^0-9]+)')): if isinstance(item, str): item = [int(d) if d else nd for d, nd in re.findall(dndre, item.lower())] return item The above transforms all strings into a list of integers and lowercase strings (if you don't want letter case to affect sort order). In Python 2.x, use basestring instead of str. If you're working with bytes in Python 3.x, make sure to first decode() the items before sorting since the regular expression is only defined for strings. Regular sort: sorted(['s1x', 's10x', 's5x', 's50x', 's100x']) ['s100x', 's10x', 's1x', 's50x', 's5x'] Natural sort: sorted(['s1x', 's10x', 's5x', 's50x', 's100x'], key=natural) ['s1x', 's5x', 's10x', 's50x', 's100x'] For simple cases like this example I tend to use: natural = lambda x: (len(x), x) sorted(['s1x', 's10x', 's5x', 's50x', 's100x'], key=natural) ['s1x', 's5x', 's10x', 's50x', 's100x'] Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding digit in string
Mark Lawrence wrote: On 08/10/2012 17:43, Benjamin Fishbein wrote: I figured out a solution for the question I asked on here. To find the next digit (0-9) in a string, I use: text.find(0 or 1 or 2, etc...) But is there a more elegant way to do this? The way I found uses a lot of typing. How about something like:- for ch in text: if ch.isdigit(): doSomething(ch) or:- for ch in text: if '0' = ch = '9': doSomething(ch) I am not sure that will work very well with Unicode numbers. I would assume (you know what they say about assuming) that str.isdigit() works better with international characters/numbers. '1'.isdigit() True '23'.isdigit() True '23a'.isdigit() False for ch in text: if ch.isdigit(): # do something If you want to use the offset for any reason use enumerate:- for i, ch in enumerate(text): etc. -- Cheers. Mark Lawrence. 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 at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding a number with str.find
On 08/10/12 19:31, Steven D'Aprano wrote: re.search(r'\d', 'I 8 sandwiches').start() # returns 2 I knew there was a better way that using index and group but I couldn't think what it was... start() so obvious once you see it :-) -- 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] finding digit in string
On Mon, Oct 8, 2012 at 4:11 PM, Prasad, Ramit ramit.pra...@jpmorgan.com wrote: for ch in text: if '0' = ch = '9': doSomething(ch) I am not sure that will work very well with Unicode numbers. I would assume (you know what they say about assuming) that str.isdigit() works better with international characters/numbers. In my tests below, isdigit() matches both decimal digits ('Nd') and other digits ('No'). None of the 'No' category digits works with int(). Python 2.7.3 chars = [unichr(i) for i in xrange(sys.maxunicode + 1)] digits = [c for c in chars if c.isdigit()] digits_d = [d for d in digits if category(d) == 'Nd'] digits_o = [d for d in digits if category(d) == 'No'] len(digits), len(digits_d), len(digits_o) (529, 411, 118) Decimal nums = [int(d) for d in digits_d] [nums.count(i) for i in range(10)] [41, 42, 41, 41, 41, 41, 41, 41, 41, 41] Other print u''.join(digits_o[:3] + digits_o[12:56]) ²³¹⁰⁴⁵⁶⁷⁸⁹₀₁₂₃₄₅₆₇₈₉①②③④⑤⑥⑦⑧⑨⑴⑵⑶⑷⑸⑹⑺⑻⑼⒈⒉⒊⒋⒌⒍⒎⒏⒐ print u''.join(digits_o[67:94]) ❶❷❸❹❺❻❼❽❾➀➁➂➃➄➅➆➇➈➊➋➌➍➎➏➐➑➒ print u''.join(digits_o[3:12]) ፩፪፫፬፭፮፯፰፱ int(digits_o[67]) Traceback (most recent call last): File stdin, line 1, in module UnicodeEncodeError: 'decimal' codec can't encode character u'\u2776' in position 0: invalid decimal Unicode string Python 3.2.3 chars = [chr(i) for i in range(sys.maxunicode + 1)] digits = [c for c in chars if c.isdigit()] digits_d = [d for d in digits if category(d) == 'Nd'] digits_o = [d for d in digits if category(d) == 'No'] len(digits), len(digits_d), len(digits_o) (548, 420, 128) Decimal nums = [int(d) for d in digits_d] [nums.count(i) for i in range(10)] [42, 42, 42, 42, 42, 42, 42, 42, 42, 42] Other print(*(digits_o[:3] + digits_o[13:57]), sep='') ²³¹⁰⁴⁵⁶⁷⁸⁹₀₁₂₃₄₅₆₇₈₉①②③④⑤⑥⑦⑧⑨⑴⑵⑶⑷⑸⑹⑺⑻⑼⒈⒉⒊⒋⒌⒍⒎⒏⒐ print(*digits_o[68:95], sep='') ❶❷❸❹❺❻❼❽❾➀➁➂➃➄➅➆➇➈➊➋➌➍➎➏➐➑➒ print(*digits_o[3:12], sep='') ፩፪፫፬፭፮፯፰፱ int(digits_o[68]) Traceback (most recent call last): File stdin, line 1, in module ValueError: invalid literal for int() with base 10: '❶' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Through a glass, darkly: the datetime module
On Mon, Oct 08, 2012 at 08:50:14PM +0200, Roel Schroeven wrote: Sander Sweers schreef: As far as I know also in Belgium Sunday is officially the first day of the week. Look at the calendar and check what is the leftmost day. My guess this is the same as your northern neighbor, Sunday ;-). On the contrary, I can't remember ever having seen a non-foreign calendar with weeks starting on Sunday. Depends on what you mean by foreign :) Here in Australia, we sometimes use Monday and sometimes Sunday as the first day of the week. My calendar applet in Xfce under Debian squeeze gives Sunday as the first day of the week. According to Wikipedia, the first day of the week can be Sunday, Monday or Saturday. Some examples picked at random: Sunday: ancient Roman, Hebrew, Greek, Arabic, Malay, Persian, etc. Monday: Polish, Russia, Serbian, Mongolian, Hungarian, Turkish, etc. Saturday: Swahili, Georgian. http://en.wikipedia.org/wiki/Names_of_the_days_of_the_week -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why difference between printing string typing its object reference at the prompt?
Steve, On Thu, Oct 4, 2012 at 6:28 AM, Steven D'Aprano st...@pearwood.info wrote: snip Now, ask me about *raw strings*, and the difference between Unicode and byte strings :) How can I resist asking! I am not in chapter 2 of my study text yet, but looking ahead raw strings seem to be a method of declaring everything within the quotes to be a literal string character including the backslash escape character. Apparently this is designated by using an r before the very first quote. Can this quote be single, double or triple? I am not up (yet) on the details of Unicode that Python 3 defaults to for strings, but I believe I comprehend the general concept. Looking at the string escape table of chapter 2 it appears that Unicode characters can be either 16-bit or 32-bit. That must be a lot of potential characters! It will be interesting to look up the full Unicode tables. Quickly scanning the comparing strings section, I wonder if I should have been so quick to jump in with a couple of responses to the other thread going on recently! I don't see a mention of byte strings mentioned in the index of my text. Are these just the ASCII character set? Since I have not made it formally into this chapter yet, I don't really have specific questions, but I would be interested in anything you are willing to relate on these topics to complete my introduction to strings in Python. Or we can wait until I do get into the data types chapter that looks at these topics in detail and have specific questions. -- Cheers! boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor