Python如果那么链等价 [英] Python IF THEN chain equivalence

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

问题描述

我正在翻译一个用Python编写的程序然后链接

IF x1<限制:---做一个---

IF x2<限制:---做b ---

IF x3<限制:---做c ---

.-----

------

IF x10< limt:---做j ---

那么

那么

-----

那么

那么

那么


换句话说,只要''xi''小于''limit''继续下去

下行链路,当''xi''不小于''限制''跳转到结束时

链继续。


这是Python中的等价吗?

IF x1<限制:

---做一个---

elif x2<限制:

---做b ---

----

----

elif x10<限制:

---做j ---

I''m translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN

In other words, as long as ''xi'' is less than ''limit'' keep going
down the chain, and when ''xi'' isn''t less than ''limit'' jump to end of
chain a continue.

Is this the equivalence in Python?

IF x1 < limit:
--- do a ---
elif x2 < limit:
--- do b ---
----
----
elif x10 < limit:
--- do j ---

推荐答案

jzakiya schrieb:
jzakiya schrieb:

我正在用Python翻译一个有IF的程序然后链接


IF x1<限制:---做一个---

IF x2<限制:---做b ---

IF x3<限制:---做c ---

.-----

------

IF x10< limt:---做j ---

那么

那么

-----

那么

那么

那么


换句话说,只要''xi''小于''limit''继续下去

下行链路,当''xi''不小于''限制''跳转到结束时

链继续。


这是Python中的等价吗?

IF x1<限制:

---做一个---

elif x2<限制:

---做b ---

----

----

elif x10<限制:

---做j ---
I''m translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN

In other words, as long as ''xi'' is less than ''limit'' keep going
down the chain, and when ''xi'' isn''t less than ''limit'' jump to end of
chain a continue.

Is this the equivalence in Python?

IF x1 < limit:
--- do a ---
elif x2 < limit:
--- do b ---
----
----
elif x10 < limit:
--- do j ---



当然不是。在做一个之后,它会停止。


你需要使用


如果x1<限制:

做一个

如果x2<限制:

做b

...

或者,根据做X的性质,你可以做到/>

代表x,todo in((x1,do_a),(x2,do_b),...):

如果x<限制:

待办事项()

否则:

休息


这意味着尽管如此; DOS"纯函数没有(可变)

参数。


Diez

Of course not. After "do a", it would stop.

You need to use

if x1 < limit:
do a
if x2 < limit:
do b
...
Alternatively, and depending on the nature of "do X", you can do

for x, todo in ((x1, do_a), (x2, do_b), ...):
if x < limit:
todo()
else:
break

This implies though that the "dos" are pure functions without (variable)
arguments.

Diez


我想你应该重新思考你的帖子。您发布的第一个案例对我所知的任何语言都没有意义。而且,很多嵌套的IF'在任何语言中都是一个坏主意。在Python中,如果继续使用,最终会有代码缩进40+个字符。


-----原始消息----

来自:jzakiya< jz ***** @ mail.com>

要: py * ********@python.org

发送时间:2008年11月13日星期四下午5:06:53

主题:Python IF那么链等价


我正在用Python翻译一个有IF的程序然后链

IF x1<限制:---做一个---

IF x2<限制:---做b ---

IF x3<限制:---做c ---

.-----

------

IF x10< limt:---做j ---

那么

那么

-----

那么

那么

那么


换句话说,只要''xi''小于''limit''继续下去

下行链路,当''xi''不小于''限制''跳转到结束时

链继续。


这是Python中的等价吗?

IF x1<限制:

---做一个---

elif x2<限制:

---做b ---

----

----

elif x10<限制:

---做j ---

-
http://mail.python.org/mailman/listinfo/python-list


就任何主题提出问题并从真实的人那里获得答案。转到Yahoo!在 http://ca.answers.yahoo.com 上回答并分享您的知识
I think you should rethink your post. The first case you posted makes no sense in any language I know. Also, a whole lot of nested IF''s is a bad idea in any language. In Python, you will end up with code indented 40+ characters if you keep going.

----- Original Message ----
From: jzakiya <jz*****@mail.com>
To: py*********@python.org
Sent: Thursday, November 13, 2008 5:06:53 PM
Subject: Python IF THEN chain equivalence

I''m translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN

In other words, as long as ''xi'' is less than ''limit'' keep going
down the chain, and when ''xi'' isn''t less than ''limit'' jump to end of
chain a continue.

Is this the equivalence in Python?

IF x1 < limit:
--- do a ---
elif x2 < limit:
--- do b ---
----
----
elif x10 < limit:
--- do j ---
--
http://mail.python.org/mailman/listinfo/python-list

__________________________________________________ ________________
Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what you know at http://ca.answers.yahoo.com


11月13日,5:21 * pm,Alan Baljeu< alanbal ... @ yahoo.comwrote:
On Nov 13, 5:21*pm, Alan Baljeu <alanbal...@yahoo.comwrote:

我认为你应该重新考虑你的帖子。您发布的第一个案例对我所知的任何语言都没有意义。 *此外,很多嵌套的IF'在任何语言中都是个坏主意。 *在Python中,如果继续使用,最终会有代码缩进40+个字符。


-----原始消息----

来自:jzakiya< jzak ... @ mail.com>

收件人:python-l ... @ python.org

发送时间:2008年11月13日,星期四5:06:53 PM

主题:Python如果那么链等价


我正在用Python翻译一个有IF的程序然后链


IF * x1<限制:* ---做一个---

* * IF * x2<限制:* ---做b ---

* * * * IF x3<限制:* ---做c ---

* * * * * * * * * * * * .-----

* * * * * * * * * * * * ------

* * * * * * * * * * IF * x10<限制:---做j ---

* * * * * * * * * *那么

* * * * * * * * *那么
* * * * * * * -----

* * * * *那么

* * *那么

那么


换句话说,只要* *''xi''小于''限制''继续下去

下行,当''xi''不小于''限制''跳到结束时

链继续。


这是等价的用Python?


IF * x1<限制:

* * * * ---做一个* ---

elif x2<限制:

* * * * ---做b ---

----

----

elif x10<限制:

* * * * ---做j ---


--http://mail.python.org/mailman/listinfo/ python-list


就任何主题提出问题并从真实的人那里获得答案。转到Yahoo!回答并分享你所知道的http://ca.answers.yahoo.com

I think you should rethink your post. The first case you posted makes no sense in any language I know. *Also, a whole lot of nested IF''s is a bad idea in any language. *In Python, you will end up with code indented 40+ characters if you keep going.

----- Original Message ----
From: jzakiya <jzak...@mail.com>
To: python-l...@python.org
Sent: Thursday, November 13, 2008 5:06:53 PM
Subject: Python IF THEN chain equivalence

I''m translating a program in Python that has this IF Then chain

IF *x1 < limit: * --- do a ---
* * IF *x2 < limit: *--- do b ---
* * * * IF x3 < limit: *--- do c ---
* * * * * * * * * * * *.-----
* * * * * * * * * * * * ------
* * * * * * * * * * IF *x10 < limt: --- do j ---
* * * * * * * * * * THEN
* * * * * * * * *THEN
* * * * * * * -----
* * * * * THEN
* * *THEN
THEN

In other words, as long as * *''xi'' is less than ''limit'' keep going
down the chain, and when ''xi'' isn''t less than ''limit'' jump to end of
chain a continue.

Is this the equivalence in Python?

IF *x1 < limit:
* * * * --- do a *---
elif x2 < limit:
* * * * --- do b ---
----
----
elif x10 < limit:
* * * *--- do j ---

--http://mail.python.org/mailman/listinfo/python-list

* * * __________________________________________________ ________________
Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what you know athttp://ca.answers.yahoo.com



在代码中''xi'和''限制''是变量和---做字母

---

短语只是写入任何数组:an_array [xi] = 0


实际上,代码非常有意义,并且是我正在实现的算法的必要性,并且在Forth中工作得非常好,并且

可以在正常的页面宽度内写得非常好。


我只是希望能在Python中执行等效的链接

没有

必须严格缩进源代码超过

打印页面的正常宽度。

但是如果那个''是用Python做的唯一方法,那就这样吧。

In the code the ''xi''s and ''limit'' are variables and the --- do letters
---
phrases are simply writes to any array: an_array[xi]=0

Actually, the code makes perfectly good sense, and is a necessity of
the algorithm I''m implementing, and works perfectly good in Forth, and
can be
written quite nicely within a normal page width.

I was just hoping I could perform the equivalent chain in Python
without
having to grossly indent the source code past the normal width of a
printed page.
But if that''s the only way to do it in Python, then so be it.


这篇关于Python如果那么链等价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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