Rant(Re:x * x,如果x> 10 [英] Rant (was Re: x*x if x>10

查看:114
本文介绍了Rant(Re:x * x,如果x> 10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

On Sun,2008年7月27日05:24:36 -0700(PDT),alex23< wu ***** @ gmail.comwrote:


> 7月27日,10:13 * pm,ssecorp< circularf ... @ gmail.comwrote:


>我见过你可以写的地方类似于:


> x * x if x> 10
但确切这不起作用,我不能得到任何变化的工作。


>它被称为三元运算符。格式为:
< label =< true-valueif< conditionelse< false-value>



我看过PERL说/座右铭/吹嘘,有不止一种方法可以做到这一点

不止一次在这个群体上嘲笑那么这是什么原因让

这种额外的方式将if else语句放在一行上?是和和

或结构产生相同的效果不支持这种用途?


虽然我在高高在上,但我想提出列表连接。我最近需要
来连接5个列表,这对我来说并不是一个特别好的b
罕见的要求。我的第一次尝试是一个简单的循环

扩展一个空列表。这很好,但看起来像一个可怕的笨重的

解决方案。然后我尝试使用减少的各种公式。 ,落条犯规

的=抓住一次。现在我不是一个专业的程序员,所以

可能有充分的理由让一个对象在一个

程序中有多个名字,但这听起来像一个食谱为了灾难我。回到

列表连接,我终于找到了itertools.chain命令,它是最紧凑和最快的

(或者是一个微不足道的第二快,我可以' '$

记住哪个)。一路上,我必须尝试/使用了六种方法,

....这让我回到了我最初的PERL评论。在Python中也有不止一种方式




DaveM

解决方案

DaveM schrieb:


On Sun,2008年7月27日05:24:36 -0700(PDT),alex23< wu ** ***@gmail.com写道:


> 7月27日晚上10点13分,ssecorp< circularf ... @ gmail.comwrote:


>>我见过你可以写的东西:


>> x * x如果x> 10
但确切地说这不起作用,我无法获得任何变化。


>它被称为三元运算符。格式为:
< label =< true-valueif< conditionelse< false-value>



我看过PERL说/座右铭/吹嘘,有不止一种方法可以做到这一点

不止一次在这个群体上嘲笑那么这是什么原因让

这种额外的方式将if else语句放在一行上?是和和

或构造产生相同的效果不支持此用途?



你显然不知道有关误用或

以及此用途的缺陷。


试试这个:


>> a =真的
b = 1
c = 2
a和b或c



1


>> a = False
a和b或c



2


>> a = True
b =无
a和b或c



2


>>>



之后我尝试了使用reduce的各种公式。 ,落条犯规

的=抓住一次。现在我不是一个专业的程序员,所以

可能有充分的理由让一个对象在一个

程序中有多个名字,但这听起来像一个食谱为了灾难我。



你能告诉我们你的意思是一个对象的几个名字吗?你的意思是

这个?


a =范围(10)

b = a


id(a)== id(b)

?传递引用而不是值是一个非常重要的概念

多种语言,如果没有它,你最终会在大多数情况下复制。


回到

列表连接,我终于找到了itertools.chain命令,它是最紧凑和最快的

(或者是一个微不足道的第二快) ,我不能

记住哪个)。一路上,我必须尝试/使用了六种方法,

...这让我回到了我最初的PERL评论。使用Python也可以有多种方式来支持



任何非平凡的任务都有该属性。我不知道perl有足够的per / b
一个示例准备好了,表示python只有一种方式来做
做,而perl有几种。


但我*确实*知道蟒蛇禅字面上没有结果。

Diez


On Sun,27 2008年7月16:41:19 +0200,Diez B. Roggisch写道:


DaveM schrieb:


>回到
列表连接,我终于找到了itertools.chain命令,它是最紧凑和最快的(或者是一个微不足道的第二快,
我可以''记得哪个)。一路上,我必须尝试/使用半个方法,...这让我回到了我最初的PERL评论。在Python中也有不止一种方法可以做到这一点。



任何非平凡的任务都有该属性。我不知道perl有足够的per / b
一个示例准备好了,表示python只有一种方式来做
做,而perl有几种。


但我*确实*知道从字面上理解python zen是徒劳的。



我认为应该采取更多的字面意义而不是错误的减少

应该只有一种方式。人们往往会忘记明显的和

优选所有的时间。


Ciao,

Marc''BlackJack''Rintsch


DaveM写道:


On Sun,2008年7月27日05:24:36 -0700(PDT),alex23< wu ***** @ gmail.comwrote:


> 7月27日晚上10点13分,ssecorp< circularf ... @ gmail.comwrote:


>>我见过你可以写的东西:



>> x * x如果x> 10
但确切地说这不起作用,我无法获得任何变化。



>它被称为三元运算符。格式为:
< label =< true-valueif< conditionelse< false-value>



我看过PERL的说法/座右铭/夸耀,有不止一种方法可以做到这一点

不止一次嘲笑这个群体所以这是什么原因导致

这种在一行上放置if else语句的额外方法?是和和

或产生相同效果的结构不支持此用途?



和和或构造,它相当于三元运算符

非常复杂,看起来不像条件计算:

(C和[A]或[b])[0]

这是低效的,几乎不可读,这使得高度有用的三元运算符值得一个语法结构。

[A],[ b]和[0]部分在一般情况下绝对必要

其中A可以具有Python认为等于False的值。

如果你知道A将永远不会相当于False然后你可以使用这个:

C和A或B


加里赫伦


虽然我在高高在上,但我想提出列表连接。我最近需要
来连接5个列表,这对我来说并不是一个特别好的b
罕见的要求。我的第一次尝试是一个简单的循环

扩展一个空列表。这很好,但看起来像一个可怕的笨重的

解决方案。然后我尝试使用减少的各种公式。 ,落条犯规

的=抓住一次。现在我不是一个专业的程序员,所以

可能有充分的理由让一个对象在一个

程序中有多个名字,但这听起来像一个食谱为了灾难我。回到

列表连接,我终于找到了itertools.chain命令,它是最紧凑和最快的

(或者是一个微不足道的第二快,我可以' '$

记住哪个)。一路上,我必须尝试/使用了六种方法,

...这让我回到了我最初的PERL评论。在Python中也有不止一种方式




DaveM

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


On Sun, 27 Jul 2008 05:24:36 -0700 (PDT), alex23 <wu*****@gmail.comwrote:

>On Jul 27, 10:13*pm, ssecorp <circularf...@gmail.comwrote:

>I have seen somewhere that you can write something like:

>x*x if x>10
but exactly that doesn''t work and I can''t get any variation to work.

>It''s called a ternary operator. The format is:
<label= <true-valueif <conditionelse <false-value>

I''ve seen the PERL saying/motto/boast, "There''s more than one way to do it"
derided on more than one occasion on this group so what''s the reason for
this additional way to put an if else statement on one line? Are "and" and
"or" constructions to produce the same effect not supported for this use?

And while I''m on my high horse, I''d like to bring up list concatenations. I
recently needed to concatenate 5 lists, which doesn''t sound a particularly
rare requirement to me. My first attempt was a straightforward loop
extending an empty list. That worked fine but looked like an awful bulky
solution. Afterwards I tried various formulae using "reduce" , falling foul
of the "=" catch on one occasion. Now I''m not a professional programmer, so
there may be good reasons for a single object to have multiple names in a
program, but it sounds like a recipe for disaster to me. Getting back to the
list concatenation, I finally found the itertools.chain command which is the
most compact and fastest (or second fastest by a trivial amount, I can''t
remember which). Along the way, I must have tried/used half a dozen methods,
....which brings me back my initial PERL comment. There''s more than one way
to do it in Python, too.

DaveM

解决方案

DaveM schrieb:

On Sun, 27 Jul 2008 05:24:36 -0700 (PDT), alex23 <wu*****@gmail.comwrote:

>On Jul 27, 10:13 pm, ssecorp <circularf...@gmail.comwrote:

>>I have seen somewhere that you can write something like:

>>x*x if x>10
but exactly that doesn''t work and I can''t get any variation to work.

>It''s called a ternary operator. The format is:
<label= <true-valueif <conditionelse <false-value>


I''ve seen the PERL saying/motto/boast, "There''s more than one way to do it"
derided on more than one occasion on this group so what''s the reason for
this additional way to put an if else statement on one line? Are "and" and
"or" constructions to produce the same effect not supported for this use?

You obviously aren''t aware of the pitfalls regarding the mis-use of or
and and for this usage.

Try this:

>>a = True
b = 1
c = 2
a and b or c

1

>>a = False
a and b or c

2

>>a = True
b = None
a and b or c

2

>>>


Afterwards I tried various formulae using "reduce" , falling foul
of the "=" catch on one occasion. Now I''m not a professional programmer, so
there may be good reasons for a single object to have multiple names in a
program, but it sounds like a recipe for disaster to me.


Can you tell us what you mean by "several names of one object"? You mean
this?

a = range(10)
b = a

id(a) == id(b)
? Passing references instead of values is an extremely important concept
of many languages, without it you would end up copying most of the time.

Getting back to the
list concatenation, I finally found the itertools.chain command which is the
most compact and fastest (or second fastest by a trivial amount, I can''t
remember which). Along the way, I must have tried/used half a dozen methods,
...which brings me back my initial PERL comment. There''s more than one way
to do it in Python, too.

Any non-trivial task has that property. I don''t know enough perl to have
an example ready that shows something that python has only one way of
doing and perl has several.

But I *do* know that taking the python zen literally is fruitless.
Diez


On Sun, 27 Jul 2008 16:41:19 +0200, Diez B. Roggisch wrote:

DaveM schrieb:

>Getting back to the
list concatenation, I finally found the itertools.chain command which
is the most compact and fastest (or second fastest by a trivial amount,
I can''t remember which). Along the way, I must have tried/used half a
dozen methods, ...which brings me back my initial PERL comment. There''s
more than one way to do it in Python, too.


Any non-trivial task has that property. I don''t know enough perl to have
an example ready that shows something that python has only one way of
doing and perl has several.

But I *do* know that taking the python zen literally is fruitless.

I think it should be taken more literally than the wrong reduction to
"there should be only one way". People tend to forget "obvious" and
"preferably" all the time.

Ciao,
Marc ''BlackJack'' Rintsch


DaveM wrote:

On Sun, 27 Jul 2008 05:24:36 -0700 (PDT), alex23 <wu*****@gmail.comwrote:

>On Jul 27, 10:13 pm, ssecorp <circularf...@gmail.comwrote:

>>I have seen somewhere that you can write something like:


>>x*x if x>10
but exactly that doesn''t work and I can''t get any variation to work.


>It''s called a ternary operator. The format is:
<label= <true-valueif <conditionelse <false-value>


I''ve seen the PERL saying/motto/boast, "There''s more than one way to do it"
derided on more than one occasion on this group so what''s the reason for
this additional way to put an if else statement on one line? Are "and" and
"or" constructions to produce the same effect not supported for this use?

The "and" and "or" construct which is equivalent to the ternary operator
is quite convoluted and looks nothing like a conditional computation:
(C and [A] or [b])[0]
This is inefficient, and nearly unreadable, which makes the highly
useful ternary operator worthy of a syntactical construct.
The [A], [b] and [0] parts are absolutely necessary in the general case
where A can have values that Python would consider equivalent to False.
If you know A will never be equivalent to False then you can use just this:
C and A or B

Gary Herron

And while I''m on my high horse, I''d like to bring up list concatenations. I
recently needed to concatenate 5 lists, which doesn''t sound a particularly
rare requirement to me. My first attempt was a straightforward loop
extending an empty list. That worked fine but looked like an awful bulky
solution. Afterwards I tried various formulae using "reduce" , falling foul
of the "=" catch on one occasion. Now I''m not a professional programmer, so
there may be good reasons for a single object to have multiple names in a
program, but it sounds like a recipe for disaster to me. Getting back to the
list concatenation, I finally found the itertools.chain command which is the
most compact and fastest (or second fastest by a trivial amount, I can''t
remember which). Along the way, I must have tried/used half a dozen methods,
...which brings me back my initial PERL comment. There''s more than one way
to do it in Python, too.

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


这篇关于Rant(Re:x * x,如果x> 10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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