一个类似C语句的语句 [英] A C-like if statement

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

问题描述

我想念能用Python做这样的事情


1f(I = a.find(" 3")))!= -1:

打印它在这里:,我

其他:

打印No 3'在这里


我在其中分配了find()返回的索引,并且if语句获得了

来在同一行中完成它的工作。然后你不必拥有另一个像

那样专门得到3的索引。有没有办法在

Python中做到这一点?


谢谢!


Bob

I miss being able to do something like this in Python

1f (I = a.find("3")) != -1:
print "It''s here: ", I
else:
print "No 3''s here"

where I gets assigned the index returned by find() AND the if statement gets
to do its job in the same line. Then you don''t have to have another like
that specifically gets the index of the "3". Is there a way to do this in
Python?

Thanks!

Bob

推荐答案

Bob Greschke< bo*@passcal.nmt.edu>写道:
Bob Greschke <bo*@passcal.nmt.edu> wrote:
我错过了能够用Python做这样的事情

1f(I = a.find(" 3")))!= -1:
print"它在这里:,我
否则:
打印No 3'在这里

我被分配了返回的索引通过find()和if语句获得
在同一行中完成它的工作。那么你不必拥有另一个像
那样专门获得3的索引。有没有办法在Python中做到这一点?
I miss being able to do something like this in Python

1f (I = a.find("3")) != -1:
print "It''s here: ", I
else:
print "No 3''s here"

where I gets assigned the index returned by find() AND the if statement gets
to do its job in the same line. Then you don''t have to have another like
that specifically gets the index of the "3". Is there a way to do this in
Python?




这是Python中一个深思熟虑的基本设计决定

赋值是一种陈述,而不是具有副作用的表达。这个

意味着你经常需要一个额外的与经典的C"分配

和测试相比较如上所述的成语。我和你一样,经常会错过这个C成语的紧凑性,但它确实存在。


将魔法值恢复到魔法值也是unpythonic表示失败。

抛出异常将是更正常的做法。所以,

我希望以上内容可以转化为:


尝试:

i = a.find( 3)

打印它在这里:,我是

打印No 3 '在这里'



It is a deliberate and fundamental design decision in Python that
assignment is a statement, not an expression with side effects. This
means you often need an "extra" line compared to a classic C "assign
and test" idiom such as you have above. I, like you, often miss the
compactness of the C idiom, but there it is.

It''s also unpythonic to return magic values to indicate failure.
Throwing an exception would be the more normal way of doing it. So,
I''d expect the above to translate into something like:

try:
i = a.find("3")
print "It''s here: ", i
except NotFound:
print "No 3''s here"




" Roy Smith" < ro*@panix.com>在消息中写道

news:dt ********** @ reader2.panix.com ...

"Roy Smith" <ro*@panix.com> wrote in message
news:dt**********@reader2.panix.com...
Bob Greschke< bo * @ passcal。 nmt.edu>写道:
Bob Greschke <bo*@passcal.nmt.edu> wrote:
我错过了能够用Python做这样的事情

1f(I = a.find(" 3")))!= -1:
print"它在这里:,我
否则:
打印No 3'在这里

我被分配了返回的索引通过find()和if语句
来完成它在同一行中的工作。那么你不必拥有另一个像
那样专门获得3的索引。有没有办法在Python中执行此操作?
I miss being able to do something like this in Python

1f (I = a.find("3")) != -1:
print "It''s here: ", I
else:
print "No 3''s here"

where I gets assigned the index returned by find() AND the if statement
gets
to do its job in the same line. Then you don''t have to have another like
that specifically gets the index of the "3". Is there a way to do this in
Python?



这是Python中一个深思熟虑的基本设计决策,
赋值是一个语句,而不是一个表达式副作用。这意味着你经常需要一个额外的东西。线条与经典C分配
和测试相比较如上所述的成语。我和你一样,经常会错过C语言的紧凑性,但它确实存在。

返回神奇的值以表示失败也是单声道的。
投掷一个例外是更正常的做法。所以,
我希望以上内容能够转化为:

尝试:
i = a.find(" 3")
print" ;它在这里:,我是
除了NotFound:
打印No 3'在这里



It is a deliberate and fundamental design decision in Python that
assignment is a statement, not an expression with side effects. This
means you often need an "extra" line compared to a classic C "assign
and test" idiom such as you have above. I, like you, often miss the
compactness of the C idiom, but there it is.

It''s also unpythonic to return magic values to indicate failure.
Throwing an exception would be the more normal way of doing it. So,
I''d expect the above to translate into something like:

try:
i = a.find("3")
print "It''s here: ", i
except NotFound:
print "No 3''s here"




坚果。我想你是对的。这不合适。每天为Python添加的东西或者每天都会提出我不能发音的东西,但是一个简单的''如果

(I = a.find(" 3" 3) ;))!= -1''是不允许的。呵呵。可能是时候回到

到BASIC了。 :)


如果.find()被.index()替换,我认为你的方式会奏效。我是

只是想稍微清理一下if / elif树,所以使用try会使

更大的东西。


谢谢!


Bob



Nuts. I guess you''re right. It wouldn''t be proper. Things are added or
proposed every day for Python that I can''t even pronounce, but a simple ''if
(I = a.find("3")) != -1'' isn''t allowed. Huh. It might be time to go back
to BASIC. :)

I think your way would work if .find() were replaced with .index(). I''m
just trying to clean up an if/elif tree a bit, so using try would make
things bigger.

Thanks!

Bob


但也许我们正在讨论字符串方法以获得异常

我们要使用索引而不是找到。


Giles

But maybe we''re talking about string methods so to get an exception
we''d want to use "index" instead of "find".

Giles


这篇关于一个类似C语句的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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