"数在基" ``oneliner”” [英] "number-in-base" ``oneliner''

查看:86
本文介绍了"数在基" ``oneliner””的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点犹豫发布这个,但是......在意大利的Python NG上,有人

问是否有办法将整数x转换为

a字符串,表示它在任意基数N(给定一个序列

,其len为N,给出要使用的数字)为单个表达式。


我还没有找到一个非常普通的方式,更不用说清楚了,但是,到目前为止我最好的是

...:

def number_in_base(x,N,数字,maxlen = 99):

返回'' - ''[x> = 0:] +(

(x和''''。join([数字[k%N]为i在范围内(maxlen)

为k in [abs(x)// N ** i]如果k> 0])[:: - 1]

)或数字[0])


除了缺乏清晰度外,这种方法的明显缺陷是

darned''maxlen''参数 - 但是之后,因为我只能有一个''for'',

而不是''while'',列表理解或生成器表达式,我和/或
不要认为递归有资格作为''单一表达''...... :-(


无论如何,改进和建议欢迎,谢谢!

Alex

解决方案

2004年10月29日星期五23:58:47 +0200, al ***** @ yahoo.com (Alex Martelli)写道:

我犹豫有点发布这个,但是...在意大利Python NG上,有人
询问是否有任何方法将整数x转换为
一个字符串,代表它在任意基数N(给出一个带有len的N的序列,使得数字可以使用)作为单个表达式。

我还没有找到一种非常通用的方式,更不用说清楚了一,但是,我到目前为止最好的是...:

def number_in_base(x,N,digits,maxlen = 99):
返回'' - ' '[x> = 0:] +(
(x和''''。join([数字[k%N]为i在范围内(maxlen)
对于k在[abs(x)// N ** i]如果k> 0])[:: - 1]
)或数字[0])

除此之外由于缺乏清晰度,这种方法的明显缺陷是达到''maxlen''参数 - 然后,因为我只能''为'',
而不是'' '',在列表理解或生成器表达式中,我不认为递归符合''单个表达式''...... :-(

无论如何,改进和建议欢迎,谢谢!



也许这个有用的东西?没有经过严格测试(也不是非常清楚;-)

def number_in_base(x,N,digits):
...返回x == 0和数字[0]或'' - ''[:x< 0] +''''。join([d for it in iter(
$ b) $ b ... lambda qr = [abs(x),0]:qr [0]和(

... qr .__ setslice __(0,2,divmod(qr [0],N ))或数字[qr [1]])

...,0)] [:: - 1])

... number_in_base(126,2, ''0123456789ABCDEF'')
''1111110''number_in_base(126,8,''0123456789ABCDEF'')
''176''number_in_base(126,16,''0123456789ABCDEF'')
''7E''number_in_base(1,16,''0123456789ABCDEF'')
''1''number_in_base(0,16,''0123456789ABCDEF'')
''0''number_in_base (-126,16,''0123456789ABCDEF'')
''-7E''number_in_base(-126,2,''0123456789ABCDEF' )
'' - 1111110''


更少测试,并使用下一个覆盖的列表子类型做同样的事情:

def number_in_base (x,N,数字):
...返回x == 0和数字[0]或'' - ''[:x< 0] +''''。join([d代表d in type('''',(list,),{

...''__iter __'':lambda s:s,''next'':lambda s :(

... s [0]为0且iter([])。next()或

... s .__ setslice __(0,2,divmod(s [0],N ))或数字[s [1]])

...})([abs(x),0])] [:: - 1])

... number_in_base(-126,8,''01234567'')
'' - 176''number_in_base(-126,2,''01'')
'' - 1111110''number_in_base (126,2,''01'')
''1111110''number_in_base(0,2,''01'')
''0''number_in_base(1,2,''01 '')



''1''


; - )


问候,

Bengt Richter


2004年10月29日星期五23:58:47 +0200, al*****@yahoo.com (Alex Martelli)写道:

我有点犹豫发布这个,但是...在意大利的Python NG上,有人
询问是否有任何方法可以将整数x转换为
一个字符串,表示它是任意的base N(给定一个带有len的N的序列,给出要使用的数字)作为单个表达式。

我还没有找到一个非常通用的方法,很多不太清楚,但是,我到目前为止最好的是......:

def number_in_base(x,N,digits,maxlen = 99):
返回' ' - ''[x> = 0:] +(
(x和''''。join([数字[k%N]为i在范围内(maxlen)
对于k在[abs] (x)// N ** i]如果k> 0])[:: - 1]
)或数字[0])

除了缺乏清晰度,明显的缺陷这种方法的原因是:''maxlen''参数 - 然后,因为我只能在列表理解中列出''for'',
而不是''while''生成器表达式,我不认为递归符合''单个表达式''...... :-(

无论如何,欢迎改进和建议,谢谢!


也许这个有用吗?没有经过严格测试(也不是非常清楚;-)

def number_in_base(x,N,digits):
...返回x == 0和数字[0]或'' - ''[:x< 0] +''''。join([d for it in iter(
$ b) $ b ... lambda qr = [abs(x),0]:qr [0]和(

... qr .__ setslice __(0,2,divmod(qr [0],N ))或数字[qr [1]])

...,0)] [:: - 1])

... number_in_base(126,2, ''0123456789ABCDEF'')
''1111110''number_in_base(126,8,''0123456789ABCDEF'')
''176''number_in_base(126,16,''0123456789ABCDEF'')
''7E''number_in_base(1,16,''0123456789ABCDEF'')
''1''number_in_base(0,16,''0123456789ABCDEF'')
''0''number_in_base (-126,16,''0123456789ABCDEF'')
''-7E''number_in_base(-126,2,''0123456789ABCDEF' )
'' - 1111110''


更少测试,并使用下一个覆盖的列表子类型做同样的事情:

def number_in_base (x,N,数字):
...返回x == 0和数字[0]或'' - ''[:x< 0] +''''。join([d代表d in type('''',(list,),{

...''__iter __'':lambda s:s,''next'':lambda s :(

... s [0]为0且iter([])。next()或

... s .__ setslice __(0,2,divmod(s [0],N ))或数字[s [1]])

...})([abs(x),0])] [:: - 1])

... number_in_base(-126,8,''01234567'')
'' - 176''number_in_base(-126,2,''01'')
'' - 1111110''number_in_base (126,2,''01'')
''1111110''number_in_base(0,2,''01'')
''0''number_in_base(1,2,''01 '')



''1''


; - )


问候,

Bengt Richter


2004年10月30日星期六06:07:23 GMT,< a href =mailto:bo ** @ oz.net> bo ** @ oz.net (Be​​ngt Richter)写道:

[...]

也许这个有用的东西?测试不是很严格(也不是非常清楚;-)

def number_in_base(x,N,digits):... return x == 0和数字[0]或'' - ''[:x< 0] +''''。join([d for it in iter(
... lambda qr = [abs(x),0] :qr [0]和(
... qr .__ setslice __(0,2,divmod(qr [0],N))或数字[qr [1]])
...,0 )] [:: - 1])
...




或者,更紧凑一点(尽管我对-1感到内疚;-) :

def number_in_base(x,N = 10,digits =''0123456789ABCDEF''):
... return'' - ''[:x< 0] +''''。join(list(iter(lambda qr = [abs(x), - 1] :( qr [0]或qr [1]< 0)和(

。 .. qr .__ setslice __(0,2,divmod(qr [0],N))或数字[qr [1]]),False))[:: - 1])

.. .nu​​mber_in_base(126,2,''01'')
''1111110''number_in_base(126,8,''01234567'')
''176''number_in_base(-126,8,''01234567'')
'' - 176''number_in_base(-1,2,''01234567'')
' '-1''number_in_base(1,2,''01234567'')
''1''number_in_base(0,2,''01234567'')



''0''


问候,

Bengt Richter


I hesitate a bit to post this, but... on the Italian Python NG, somebody
was asking whether there was any way to convert an integer number x into
a string which represents it in an arbitrary base N (given a sequence
with a len of N that gives the digits to use) as "a single expression".

I haven''t found a really general way, much less a clear one, but, the
best I have so far is...:

def number_in_base(x, N, digits, maxlen=99):
return ''-''[x>=0:] + (
(x and ''''.join([digits[k%N] for i in range(maxlen)
for k in [abs(x)//N**i] if k>0])[::-1]
) or digits[0])

Besides the lack of clarity, the obvious defect of this approach is that
darned ''maxlen'' parameter -- but then, since I can have only a ''for'',
not a ''while'', in a list comprehension or generator expression, and I
don''t think recursion qualifies as ''a single expression''...:-(

Anyway, improvements and suggestions welcome, thanks!
Alex

解决方案

On Fri, 29 Oct 2004 23:58:47 +0200, al*****@yahoo.com (Alex Martelli) wrote:

I hesitate a bit to post this, but... on the Italian Python NG, somebody
was asking whether there was any way to convert an integer number x into
a string which represents it in an arbitrary base N (given a sequence
with a len of N that gives the digits to use) as "a single expression".

I haven''t found a really general way, much less a clear one, but, the
best I have so far is...:

def number_in_base(x, N, digits, maxlen=99):
return ''-''[x>=0:] + (
(x and ''''.join([digits[k%N] for i in range(maxlen)
for k in [abs(x)//N**i] if k>0])[::-1]
) or digits[0])

Besides the lack of clarity, the obvious defect of this approach is that
darned ''maxlen'' parameter -- but then, since I can have only a ''for'',
not a ''while'', in a list comprehension or generator expression, and I
don''t think recursion qualifies as ''a single expression''...:-(

Anyway, improvements and suggestions welcome, thanks!


Maybe something useful in this? Not very tested (and not terribly clear either ;-)

def number_in_base(x, N, digits): ... return x==0 and digits[0] or ''-''[:x<0] + ''''.join([d for d in iter(
... lambda qr=[abs(x),0]:qr[0] and (
... qr.__setslice__(0,2,divmod(qr[0],N)) or digits[qr[1]])
... , 0)][::-1])
... number_in_base( 126 ,2,''0123456789ABCDEF'') ''1111110'' number_in_base( 126 ,8,''0123456789ABCDEF'') ''176'' number_in_base( 126 ,16,''0123456789ABCDEF'') ''7E'' number_in_base( 1 ,16,''0123456789ABCDEF'') ''1'' number_in_base( 0 ,16,''0123456789ABCDEF'') ''0'' number_in_base(-126 ,16,''0123456789ABCDEF'') ''-7E'' number_in_base(-126 ,2,''0123456789ABCDEF'') ''-1111110''

Even less tested, and using a list subtype with overridden next to do the same:
def number_in_base(x, N, digits): ... return x==0 and digits[0] or ''-''[:x<0] + ''''.join([d for d in type('''',(list,),{
... ''__iter__'':lambda s:s, ''next'':lambda s:(
... s[0] is 0 and iter([]).next() or
... s.__setslice__(0,2,divmod(s[0],N)) or digits[s[1]])
... })([abs(x),0])][::-1])
... number_in_base(-126, 8, ''01234567'') ''-176'' number_in_base(-126, 2, ''01'') ''-1111110'' number_in_base(126, 2, ''01'') ''1111110'' number_in_base( 0 , 2, ''01'') ''0'' number_in_base( 1 , 2, ''01'')


''1''

;-)

Regards,
Bengt Richter


On Fri, 29 Oct 2004 23:58:47 +0200, al*****@yahoo.com (Alex Martelli) wrote:

I hesitate a bit to post this, but... on the Italian Python NG, somebody
was asking whether there was any way to convert an integer number x into
a string which represents it in an arbitrary base N (given a sequence
with a len of N that gives the digits to use) as "a single expression".

I haven''t found a really general way, much less a clear one, but, the
best I have so far is...:

def number_in_base(x, N, digits, maxlen=99):
return ''-''[x>=0:] + (
(x and ''''.join([digits[k%N] for i in range(maxlen)
for k in [abs(x)//N**i] if k>0])[::-1]
) or digits[0])

Besides the lack of clarity, the obvious defect of this approach is that
darned ''maxlen'' parameter -- but then, since I can have only a ''for'',
not a ''while'', in a list comprehension or generator expression, and I
don''t think recursion qualifies as ''a single expression''...:-(

Anyway, improvements and suggestions welcome, thanks!


Maybe something useful in this? Not very tested (and not terribly clear either ;-)

def number_in_base(x, N, digits): ... return x==0 and digits[0] or ''-''[:x<0] + ''''.join([d for d in iter(
... lambda qr=[abs(x),0]:qr[0] and (
... qr.__setslice__(0,2,divmod(qr[0],N)) or digits[qr[1]])
... , 0)][::-1])
... number_in_base( 126 ,2,''0123456789ABCDEF'') ''1111110'' number_in_base( 126 ,8,''0123456789ABCDEF'') ''176'' number_in_base( 126 ,16,''0123456789ABCDEF'') ''7E'' number_in_base( 1 ,16,''0123456789ABCDEF'') ''1'' number_in_base( 0 ,16,''0123456789ABCDEF'') ''0'' number_in_base(-126 ,16,''0123456789ABCDEF'') ''-7E'' number_in_base(-126 ,2,''0123456789ABCDEF'') ''-1111110''

Even less tested, and using a list subtype with overridden next to do the same:
def number_in_base(x, N, digits): ... return x==0 and digits[0] or ''-''[:x<0] + ''''.join([d for d in type('''',(list,),{
... ''__iter__'':lambda s:s, ''next'':lambda s:(
... s[0] is 0 and iter([]).next() or
... s.__setslice__(0,2,divmod(s[0],N)) or digits[s[1]])
... })([abs(x),0])][::-1])
... number_in_base(-126, 8, ''01234567'') ''-176'' number_in_base(-126, 2, ''01'') ''-1111110'' number_in_base(126, 2, ''01'') ''1111110'' number_in_base( 0 , 2, ''01'') ''0'' number_in_base( 1 , 2, ''01'')


''1''

;-)

Regards,
Bengt Richter


On Sat, 30 Oct 2004 06:07:23 GMT, bo**@oz.net (Bengt Richter) wrote:
[...]

Maybe something useful in this? Not very tested (and not terribly clear either ;-)

def number_in_base(x, N, digits): ... return x==0 and digits[0] or ''-''[:x<0] + ''''.join([d for d in iter(
... lambda qr=[abs(x),0]:qr[0] and (
... qr.__setslice__(0,2,divmod(qr[0],N)) or digits[qr[1]])
... , 0)][::-1])
...



Alternatively, a little more compactly (though I feel guilty about the -1 ;-):

def number_in_base(x, N=10, digits=''0123456789ABCDEF''): ... return ''-''[:x<0] + ''''.join(list(iter(lambda qr=[abs(x),-1]: (qr[0] or qr[1]<0) and (
... qr.__setslice__(0,2,divmod(qr[0],N)) or digits[qr[1]]), False))[::-1])
... number_in_base(126,2,''01'') ''1111110'' number_in_base( 126, 8,''01234567'') ''176'' number_in_base(-126, 8,''01234567'') ''-176'' number_in_base( -1, 2,''01234567'') ''-1'' number_in_base( 1, 2,''01234567'') ''1'' number_in_base( 0, 2,''01234567'')


''0''

Regards,
Bengt Richter


这篇关于&QUOT;数在基&QUOT; ``oneliner””的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆