[Tutor] finding digit in string

2012-10-08 Thread Benjamin Fishbein
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 digit in string

2012-10-08 Thread Mark Lawrence

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

2012-10-08 Thread Alan Gauld

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

2012-10-08 Thread Dave Angel
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

2012-10-08 Thread bob gailer

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 digit in string

2012-10-08 Thread Prasad, Ramit
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 digit in string

2012-10-08 Thread eryksun
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