Noob有关Python的问题 [英] Noob questions about Python

查看:47
本文介绍了Noob有关Python的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近(今天)刚开始学习/玩Python。所以

远我感到兴奋和印象深刻(来自PHP背景)。


我对Python行为有几个问题......


val =''string''

li = list(val)

print li.reverse()


什么都不返回,但是,


val =''string''

li = list(val)

li.reverse()

print li


返回我想要的内容。为什么Python会这样做?


我也一直在玩二进制数学,并注意到了

Python对待:


val = 00110

为整数72而不是返回00110,为什么Python会这样做?

(我怎样才能解决它? )


感谢任何回复!

解决方案

10月17日,3:37下午,Ixiaus< parnel ... @ comcast.netwrote:


我最近(今天)刚开始学习/玩Python。所以

远我感到兴奋和印象深刻(来自PHP背景)。


我对Python行为有几个问题......


val =''string''

li = list(val)

print li.reverse()


什么都不返回,但是,


val =''string''

li = list(val)

li.reverse()

print li


返回我想要的内容。为什么Python会这样做?



因为list.reverse()修改了一个列表,所以它不会创建并返回一个

的新列表。


返回列表的反向副本的常用习语是:

li [:: - 1]


你可以也使用内置的反转 - 它没有返回列表,

而是反向迭代器。你可以通过将它传递给list()初始值设定项来创建一个迭代器列表

,如list(reverse(li))。

Ixiausaécrit:


我最近(今天)刚开始学习/玩Python。所以

远我兴奋并留下深刻印象



欢迎登机! - )


(来自PHP背景)。


我有一些关于Python行为的问题...


val =''string ''

li = list(val)

print li.reverse()


什么也不返回,但是,


val =''string''

li = list(val)

li.reverse()

打印li


返回我想要的内容。为什么Python会这样做?



list.reverse(如list.sort)是一种破坏性的就地操作。不返回
返回对象提醒

操作的破坏性。这是一个设计选择,无论你是否同意

(FWIW,我不是,但我和它一起生活! - )


请注意,还有reverse()函数返回任何序列的反向

迭代器,所以你也可以这样做:


li = list(''allo'')

print''''。join(reverse(li))


另外我一直在玩周围的二进制数学并注意到

Python对待:

val = 00110


为整数72而不是返回00110,为什么Python会这样做?



以0(零)开头的字面整数被视为八进制。它是一个非常常见的约定(如Hexa的0x)。 FWIW,PHP只做

同样的事情。


(我怎么能解决它?)


你不能。到目前为止,Python没有二进制整数的字面符号。

二进制整数的字面符号无论如何都不是常见的特征。


HTH


10月17日晚上8:37,Ixiaus< parnel ... @ comcast.netwrote:


我有几个关于Python行为的问题...

为整数72而不是返回00110,为什么Python会这样做?

(我怎么能绕过它呢?)



你可以这样做:


def bin(x):

return int(x, 2)


val = bin(''00110'')


-

Paul Hankin


I have recently (today) just started learning/playing with Python. So
far I am excited and impressed (coming from PHP background).

I have a few questions regarding Python behavior...

val = ''string''
li = list(val)
print li.reverse()

returns nothing, but,

val = ''string''
li = list(val)
li.reverse()
print li

returns what I want. Why does Python do that?

Also I have been playing around with Binary math and noticed that
Python treats:

val = 00110

as the integer 72 instead of returning 00110, why does Python do that?
(and how can I get around it?)

Grateful for any replies!

解决方案

On Oct 17, 3:37 pm, Ixiaus <parnel...@comcast.netwrote:

I have recently (today) just started learning/playing with Python. So
far I am excited and impressed (coming from PHP background).

I have a few questions regarding Python behavior...

val = ''string''
li = list(val)
print li.reverse()

returns nothing, but,

val = ''string''
li = list(val)
li.reverse()
print li

returns what I want. Why does Python do that?

Because list.reverse() modifies a list, it doesn''t create and return a
new one.

A common idiom for returning a reversed copy of a list is:
li[::-1]

You can also use the builtin "reversed" -- it doesn''t return a list,
but rather a reverse iterator. You can create a list from an iterator
by passing it to the list() initializer, like list(reversed(li)).


Ixiaus a écrit :

I have recently (today) just started learning/playing with Python. So
far I am excited and impressed

Welcome onboard then !-)

(coming from PHP background).

I have a few questions regarding Python behavior...

val = ''string''
li = list(val)
print li.reverse()

returns nothing, but,

val = ''string''
li = list(val)
li.reverse()
print li

returns what I want. Why does Python do that?

list.reverse (like list.sort) is a destructive in-place operation. Not
returning the object is reminder of the destructive nature of the
operation. That''s a design choice, whether you agree with it or not
(FWIW, I don''t, but I live with it !-)

Note that there''s also the reverse() function that returns a reverse
iterator over any sequence, so you could also do:

li = list(''allo'')
print ''''.join(reverse(li))

Also I have been playing around with Binary math and noticed that
Python treats:

val = 00110

as the integer 72 instead of returning 00110, why does Python do that?

Literal integers starting with ''0'' (zero) are treated as octal. It''s a
pretty common convention (like 0x for hexa). FWIW, PHP does just the
same thing.

(and how can I get around it?)

You can''t. Python has no literal notation for binary integers so far.
Literal notation for binary ints is not a common feature anyway.

HTH


On Oct 17, 8:37 pm, Ixiaus <parnel...@comcast.netwrote:

I have a few questions regarding Python behavior...
as the integer 72 instead of returning 00110, why does Python do that?
(and how can I get around it?)

You can do this:

def bin(x):
return int(x, 2)

val = bin(''00110'')

--
Paul Hankin


这篇关于Noob有关Python的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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