Python中的算术序列 [英] Arithmetic sequences in Python

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

问题描述

请访问 http://www.python.org/peps /pep-0204.html 首先。


如你所见,PEP 204遭到拒绝,主要是因为不那么明显

句法。但IMO背后的想法是非常好的。那么,也许

有理由采用略微修改的Haskell的语法?东西

喜欢


[1,3..10] - > [1,3,5,7,9]

(1,3..10) - >与上面相同的值,但返回生成器而不是

list

[1..10] - > [1,2,3,4,5,6,7,8,9,10]

(1 ..) - > ''无限''发电机产生1,2,3等等

(-3,-5 ..) - >产生-3,-5,-7等的''无限''发电机


所以,

1)" []"表示清单,()意味着发电机

2)开始是必需的,步骤和结束是可选的。


此外,这可以很好地与枚举集成(如果他们将在$ py $中出现
)。 Haskell也是这种集成的一个例子。

Please visit http://www.python.org/peps/pep-0204.html first.

As you can see, PEP 204 was rejected, mostly because of not-so-obvious
syntax. But IMO the idea behind this pep is very nice. So, maybe
there''s a reason to adopt slightly modified Haskell''s syntax? Something
like

[1,3..10] --> [1,3,5,7,9]
(1,3..10) --> same values as above, but return generator instead of
list
[1..10] --> [1,2,3,4,5,6,7,8,9,10]
(1 ..) --> ''infinite'' generator that yield 1,2,3 and so on
(-3,-5 ..) --> ''infinite'' generator that yield -3,-5,-7 and so on

So,
1) "[]" means list, "()" means generator
2) the "start" is required, "step" and "end" are optional.

Also, this can be nicely integrated with enumerations (if they will
appear in python). Haskell is also example of such integration.

推荐答案

我更喜欢在PEP中使用冒号:它与

切片表示法以及matlab中的冒号操作符。


我喜欢一般的想法,如果可用的话,我可能会使用它很多,

但是功能已经有了范围和irange。


Bas

I like the use of the colon as in the PEP better: it is consistant with
the slice notation and also with the colon operator in Matlab.

I like the general idea and I would probably use it a lot if available,
but the functionality is already there with range and irange.

Bas


" ; Gregory Petrosyan < GR *************** @ gmail.com>写道:
"Gregory Petrosyan" <gr***************@gmail.com> writes:
正如你所看到的,PEP 204被拒绝,主要是因为语法不那么明显。但IMO背后的想法是非常好的。所以,也许
有理由采用稍微修改过的Haskell的语法?
As you can see, PEP 204 was rejected, mostly because of not-so-obvious
syntax. But IMO the idea behind this pep is very nice. So, maybe
there''s a reason to adopt slightly modified Haskell''s syntax?




我喜欢这个有一些问题:Python循环往往是基于0,所以

虽然表达顺序[1..n]很方便,你通常想要的是b $ b想要的是[0 ..(n- 1)]哪个更丑。


如果你想从f(n)倒数到零,在Haskell中你可能会说


[b,b-1 .. 0]其中b = f(n)


没有在哪里在Python中,你怎么办?


[f(n)-1,f(n)-2 .. 0]


评估f两次(可能两次都不会得到相同的结果),

而传统的


xrange(f(n)-1, - 1,-1)


只评估一次,但IMO令人厌恶。


无论如何我从来不喜欢使用范围或xrange来控制

Python循环。这只是愚蠢和混乱。所以我认为你的想法

有潜力,但有一些问题需要解决。



I like this with some issues: Python loops tend to be 0-based, so
while it''s convenient to express the sequence [1..n], what you usually
want is [0..(n-1)] which is uglier.

If you want to count down from f(n) to zero, in Haskell you might say

[b, b-1 .. 0] where b=f(n)

There''s no "where" in Python, so what do you do?

[f(n)-1, f(n)-2 .. 0]

evaluates f twice (and might not even get the same result both times),
while the traditional

xrange(f(n)-1, -1, -1)

only evaluates it once but is IMO repulsive.

Anyway I''ve never liked having to use range or xrange to control
Python loops. It''s just kludgy and confusing. So I think your idea
has potential but there''s issues that have to be worked out.


_Consistentsy_是BDFL拒绝的,如果我明白了。至于

我,对于真正不同的任务,使用类似的语法并不太神。

和[1:10]真的不明显,而[1] ..10]是。

_Consistentsy_ is what BDFL rejects, if I understand pep right. As for
me, it''s not too god to have similar syntax for really different tasks.
And [1:10] is really not obvious, while [1..10] is.


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

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