Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53889 > unrolled thread
| Started by | Mohsen Pahlevanzadeh <mohsen@pahlevanzadeh.org> |
|---|---|
| First post | 2013-09-10 00:40 +0430 |
| Last post | 2013-09-10 15:11 +0000 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
a gift function and a question Mohsen Pahlevanzadeh <mohsen@pahlevanzadeh.org> - 2013-09-10 00:40 +0430
Re: a gift function and a question Steven D'Aprano <steve@pearwood.info> - 2013-09-10 07:01 +0000
Re: a gift function and a question random832@fastmail.us - 2013-09-10 09:50 -0400
Re: a gift function and a question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-10 15:11 +0000
| From | Mohsen Pahlevanzadeh <mohsen@pahlevanzadeh.org> |
|---|---|
| Date | 2013-09-10 00:40 +0430 |
| Subject | a gift function and a question |
| Message-ID | <mailman.193.1378757469.5461.python-list@python.org> |
Dear all,
I have a gift for mailing list:
////////////////////////////////
def integerToPersian(number):
listedPersian = ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹']
listedEnglish = ['0','1','2','3','4','5','6','7','8','9']
returnList = list()
listedTmpString = list(str(number))
for i in listedTmpString:
returnList.append(listedPersian[listedEnglish.index(i)])
return ''.join(returnList)
////////////////////////////////////
When you call it such as : "integerToPersian(3455)" , it return ۳۴۵۵,
۳۴۵۵ is equivalent to 3455 in Persian and Arabic language.When you read
a number such as reading from databae, and want to show in widget, this
function is very useful.
My question is , do you have reverse of this function? persianToInteger?
Yours,
Mohsen
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2013-09-10 07:01 +0000 |
| Message-ID | <522ec3c0$0$29999$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #53889 |
On Tue, 10 Sep 2013 00:40:59 +0430, Mohsen Pahlevanzadeh wrote:
> Dear all,
>
> I have a gift for mailing list:
>
> ////////////////////////////////
> def integerToPersian(number):
> listedPersian = ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹']
> listedEnglish = ['0','1','2','3','4','5','6','7','8','9']
> returnList = list()
> listedTmpString = list(str(number))
> for i in listedTmpString:
> returnList.append(listedPersian[listedEnglish.index(i)])
> return ''.join(returnList)
> ////////////////////////////////////
> When you call it such as : "integerToPersian(3455)" , it return ۳۴۵۵,
> ۳۴۵۵ is equivalent to 3455 in Persian and Arabic language.When you read
> a number such as reading from databae, and want to show in widget, this
> function is very useful.
Thank you Mohsen!
Here is a slightly more idiomatic version of the same function. This is
written for Python 3:
def integerToPersian(number):
"""Convert positive integers to Persian.
>>> integerToPersian(3455)
'۳۴۵۵'
Does not support negative numbers.
"""
digit_map = dict(zip('0123456789', '۰۱۲۳۴۵۶۷۸۹'))
digits = [digit_map[c] for c in str(number)]
return ''.join(digits)
Python 2 version will be nearly the same except it needs to use the u
prefix on the strings.
> My question is , do you have reverse of this function? persianToInteger?
The Python built-in int function already supports that:
py> int('۳۴۵۵')
3455
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | random832@fastmail.us |
|---|---|
| Date | 2013-09-10 09:50 -0400 |
| Message-ID | <mailman.210.1378821028.5461.python-list@python.org> |
| In reply to | #53906 |
On Tue, Sep 10, 2013, at 3:01, Steven D'Aprano wrote:
> def integerToPersian(number):
> """Convert positive integers to Persian.
>
> >>> integerToPersian(3455)
> '۳۴۵۵'
>
> Does not support negative numbers.
> """
> digit_map = dict(zip('0123456789', '۰۱۲۳۴۵۶۷۸۹'))
> digits = [digit_map[c] for c in str(number)]
> return ''.join(digits)
You can support negative numbers if you use digit_map.get(c,c).
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-09-10 15:11 +0000 |
| Message-ID | <522f3699$0$29988$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #53906 |
On Tue, 10 Sep 2013 07:01:20 +0000, Steven D'Aprano wrote:
> On Tue, 10 Sep 2013 00:40:59 +0430, Mohsen Pahlevanzadeh wrote:
>> My question is , do you have reverse of this function?
>> persianToInteger?
>
> The Python built-in int function already supports that:
>
>
> py> int('۳۴۵۵')
> 3455
Oh, I forgot to mention, this works back to at least Python 2.4, provided
you remember to use Unicode strings rather than bytes:
py> b = '۳۴۵۵' # This fails.
py> int(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '\xdb\xb3\xdb\xb4\xdb
\xb5\xdb\xb5'
py>
py> s = u'۳۴۵۵' # This works.
py> int(s)
3455
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web