列举改进建议 [英] enumerate improvement proposal

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

问题描述

我认为列举这样做会很方便:


def enumerate(itrbl,start = 0,step = 1):

i = start

for itrbl:

yield(i,it)

i + = step


这允许比当前枚举更多的灵活性,

在许多情况下收紧代码,并且似乎它不会打破现有代码的b / b
。是的,我需要枚举的这种行为,比如今晚的

和当前的例子。我把步骤放在参数用于

概念对称与切片。


这是一个案例用途(或用例?):

#与提议的枚举对象

导入运算符

def in_interval(test,bounds,first = 1,reverse = False):

op = operator .gt if reverse else operator.lt #python 2.5

bounds = sorted(bounds,reverse = reverse)

for i,绑定枚举(bounds,first):

如果op(测试,绑定):

返回i

返回i + 1

#使用现有的枚举

导入运算符

def in_interval(test,bounds,first = 1,reverse = False):

op = operator.gt if reverse else operator.lt #python 2.5

bounds = sorted(bounds,reverse = reverse)

for i,以枚举(bounds)绑定:

如果op(测试,绑定):

返回i +第一个

返回i +第一个+ 1

py#eg

....

pyin_interval(8,界限)

2

pyin_i nterval(1,bounds)

1

pyin_interval(1,bounds,reverse = True)

5

pyin_interval(8,bounds,reverse = True)

4

pyin_interval(20,bounds,reverse = True)

2


当然,我还没有用过这里的步骤。即使在这个简单的例子中,

提出的枚举也会清除代码和逻辑,从而消除了几个
加号。对于这个真实世界的例子,实际要求

反转垃圾箱会使提议的枚举提供的解混混淆。但是我认为可能很明显,

提出的枚举可能会比那个更复杂的情况有所帮助。


有什么想法吗?


James

I think that it would be handy for enumerate to behave as such:

def enumerate(itrbl, start=0, step=1):
i = start
for it in itrbl:
yield (i, it)
i += step

This allows much more flexibility than in the current enumerate,
tightens up code in many cases, and seems that it would break no
existing code. Yes, I have needed this behavior with enumerate, like
tonight and the current example. I put the "step" parameter in for
conceptual symmetry with slicing.

Here is a case use (or is it use case?):
# with the proposed enumerate
import operator
def in_interval(test, bounds, first=1, reverse=False):
op = operator.gt if reverse else operator.lt # python 2.5
bounds = sorted(bounds, reverse=reverse)
for i, bound in enumerate(bounds, first):
if op(test, bound):
return i
return i + 1
# with the existing enumerate
import operator
def in_interval(test, bounds, first=1, reverse=False):
op = operator.gt if reverse else operator.lt # python 2.5
bounds = sorted(bounds, reverse=reverse)
for i, bound in enumerate(bounds):
if op(test, bound):
return i + first
return i + first + 1
py# eg
....
pyin_interval(8, bounds)
2
pyin_interval(1, bounds)
1
pyin_interval(1, bounds, reverse=True)
5
pyin_interval(8, bounds, reverse=True)
4
pyin_interval(20, bounds, reverse=True)
2

Of course, I haven''t used step here. Even in this trivial example the
proposed enumerate cleans the code and logic, eliminating a couple of
plus signs. For this real-world example, the practical requirement for
reversing the bins obfuscates somewhat the de-obfuscation provided by
the proposed enumerate. But I think that it might be obvious that the
proposed enumerate could help significantly in cases a bit more
complicated than this one.

Any thoughts?

James

推荐答案

James Stroud写道:
James Stroud wrote:

我认为枚举行为是很方便的:


def enumerate(itrbl,start = 0,step = 1):

i =开始

for itrbl:

yield(i,it)

i + = step


这比现在的枚举允许更多的灵活性,

在很多情况下收紧代码,似乎它不会破坏

现有代码。是的,我需要枚举的这种行为,比如今晚的

和当前的例子。我把步骤放在参数用于

概念对称与切片。


这是一个案例用途(或用例?):


#与建议的枚举

导入运算符

def in_interval(test,bounds,first = 1,reverse = False):

op = operator.gt if reverse else operator.lt #python 2.5

bounds = sorted(bounds,reverse = reverse)

for i,绑定枚举(bounds,第一个):

如果操作(测试,绑定):

返回i

返回i + 1


#使用现有的枚举

导入运算符

def in_interval(test,bounds,first = 1,reverse = False):

op = operator.gt if reverse else operator.lt #python 2.5

bounds = sorted(bounds,reverse = reverse)

for i,以枚举(bounds)绑定:

如果操作(测试,绑定):

返回i +第一次

返回i +第一个+ 1


py#eg

...

pyi n_interval(8,bounds)

2

pyin_interval(1,bounds)

1

pyin_interval(1, bounds,reverse = True)

5

pyin_interval(8,bounds,reverse = True)

4

pyin_interval(20,bounds,reverse = True)

2


当然,我还没有用过这里的步骤。即使在这个简单的例子中,

提出的枚举也会清除代码和逻辑,从而消除了几个
加号。对于这个真实世界的例子,实际要求

反转垃圾箱会使提议的枚举提供的解混混淆。但是我认为可能很明显,

提出的枚举可能会比那个更复杂的情况有所帮助。


有什么想法吗?


詹姆斯
I think that it would be handy for enumerate to behave as such:

def enumerate(itrbl, start=0, step=1):
i = start
for it in itrbl:
yield (i, it)
i += step

This allows much more flexibility than in the current enumerate,
tightens up code in many cases, and seems that it would break no
existing code. Yes, I have needed this behavior with enumerate, like
tonight and the current example. I put the "step" parameter in for
conceptual symmetry with slicing.

Here is a case use (or is it use case?):
# with the proposed enumerate
import operator
def in_interval(test, bounds, first=1, reverse=False):
op = operator.gt if reverse else operator.lt # python 2.5
bounds = sorted(bounds, reverse=reverse)
for i, bound in enumerate(bounds, first):
if op(test, bound):
return i
return i + 1
# with the existing enumerate
import operator
def in_interval(test, bounds, first=1, reverse=False):
op = operator.gt if reverse else operator.lt # python 2.5
bounds = sorted(bounds, reverse=reverse)
for i, bound in enumerate(bounds):
if op(test, bound):
return i + first
return i + first + 1
py# eg
...
pyin_interval(8, bounds)
2
pyin_interval(1, bounds)
1
pyin_interval(1, bounds, reverse=True)
5
pyin_interval(8, bounds, reverse=True)
4
pyin_interval(20, bounds, reverse=True)
2

Of course, I haven''t used step here. Even in this trivial example the
proposed enumerate cleans the code and logic, eliminating a couple of
plus signs. For this real-world example, the practical requirement for
reversing the bins obfuscates somewhat the de-obfuscation provided by
the proposed enumerate. But I think that it might be obvious that the
proposed enumerate could help significantly in cases a bit more
complicated than this one.

Any thoughts?

James



经过短暂的反思,我意识到我刚刚描述了一个

for-to-step-do风格循环可以在许多其他语言中找到,大多数

特别是BASIC。


James

After a brief reflection, I realized that I just described a
"for-to-step-do" style loop one might find in many other languages, most
notably BASIC.

James


James Stroud写道:
James Stroud wrote:

def enumerate(itrbl,start = 0,step = 1):

i = start
$ b在itrbl中它是$ b:

收益率(我,它)

i + =步骤
def enumerate(itrbl, start=0, step=1):
i = start
for it in itrbl:
yield (i, it)
i += step



'拼写了


izip(计数(开始),序列)


今天的Python。

that''s spelled

izip(count(start), sequence)

in today''s Python.


def in_interval(test,bounds,first = 1,reverse = False):
def in_interval(test, bounds, first=1, reverse=False):



为什么这个函数'的工作为实际序列索引添加偏移量?


< / F>

why is it this function''s job to add an offset to the actual sequence index?

</F>


Fredrik Lundh写道:
Fredrik Lundh wrote:

为什么这个函数的工作是为实际序列添加一个偏移量

index?


< / F>
why is it this function''s job to add an offset to the actual sequence
index?

</F>



代码适用于经济学家。她坚持用

第一个bin开始为1.我猜,实际上,binning linerizes数据和

bin数可能会成为一个除数或者也许操作数

的对数。


James

The code is for an economist. She is insistent on starting with the
first bin as 1. I''m guessing, practically, binning linerizes data and
the bin number may potentially become a divisor or perhaps the operand
in a logarithm.

James


这篇关于列举改进建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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