功能提案:序列.join方法 [英] Feature Proposal: Sequence .join method

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

问题描述

大家好!


我无法确定之前是否已经提出过这个问题(有很多关于加入的讨论作为序列方法不同的

语义)。所以,我提出了所有序列的广义.join方法

这些语义:


def join(self,seq):

T =类型(个体)

结果= T()

如果len(seq):

result = T(seq [0 ])

for seq [1:]中的项目:

结果=结果+自我+ T(项目)

返回结果


这将允许以下代码:


[0] .join([[5],[42,5],[1,2] ,3],[23]])


导致:

[5,0,42,5,0,1,2,3,0 ,23]


您可能已经注意到它实际上包含两个道具,所以如果

你不喜欢str.join应用str()序列中的每个项目

替换行

result = result + self + T(item)

with

结果=结果+自我+项目

我的版本在过去已被拒绝,但是,我发现第一个版本更有用新的背景...你可以通过

列表或元组的序列或方法的任何序列和它

做你的想法(至少我认为:)。


欢迎任何评论,

David。

Hi all!

I could not find out whether this has been proposed before (there are
too many discussion on join as a sequence method with different
semantics). So, i propose a generalized .join method on all sequences
with these semantics:

def join(self, seq):
T = type(self)
result = T()
if len(seq):
result = T(seq[0])
for item in seq[1:]:
result = result + self + T(item)
return result

This would allow code like the following:

[0].join([[5], [42, 5], [1, 2, 3], [23]])

resulting in:
[5, 0, 42, 5, 0, 1, 2, 3, 0, 23]

You might have noticed that this contains actually two propsals, so if
you don''t like str.join applying str() on each item in the sequence
replace the line
result = result + self + T(item)
with
result = result + self + item
My version has been turned down in the past as far as i read, yet, i
find the first version more useful in the new context... you can pass a
sequence of lists or tuples or really any sequence to the method and it
does what you think (at least what i think :).

Any comments welcome,
David.

推荐答案

David Murmann写道:
David Murmann wrote:
replace线
结果=结果+自我+ T(项目)

结果=结果+自我+项目
replace the line
result = result + self + T(item)
with
result = result + self + item



当然还有

result = T(seq [0])

with

result = seq [0]


and of course the line
result = T(seq[0])
with
result = seq[0]


David Murmann写道:
David Murmann wrote:
大家好!

我不知道之前是否提出过这个问题(关于加入的讨论太多了)作为具有不同语义的序列方法)。所以,我提出了一个关于所有序列的广义.join方法
这些语义:

def join(self,seq):
T = type(self)
result = T()
如果len(seq):
结果= T(seq [0])
对于seq [1:]中的项目:
结果=结果+自我+ T(项目)
返回结果

这将允许以下代码:

[0] .join([[5],[42,5] ],[1,2,3],[23]])
Hi all!

I could not find out whether this has been proposed before (there are
too many discussion on join as a sequence method with different
semantics). So, i propose a generalized .join method on all sequences
with these semantics:

def join(self, seq):
T = type(self)
result = T()
if len(seq):
result = T(seq[0])
for item in seq[1:]:
result = result + self + T(item)
return result

This would allow code like the following:

[0].join([[5], [42, 5], [1, 2, 3], [23]])




我不喜欢把它放在所有序列上。如果你想要这个,我会把它作为一个函数提出(也许是内置的,

也许在其他模块中)。


此外,这个特定的实现是一个坏主意。重复的+ =到

结果可能导致O(N ** 2)行为。


STeVe



I don''t like the idea of having to put this on all sequences. If you
want this, I''d instead propose it as a function (perhaps builtin,
perhaps in some other module).

Also, this particular implementation is a bad idea. The repeated += to
result is likely to result in O(N**2) behavior.

STeVe


Steven Bethard写道:
Steven Bethard wrote:
David Murmann写道:
David Murmann wrote:
大家好!

我不知道这是不是之前提出过(关于连接的讨论太多,作为具有不同语义的序列方法)。所以,我提出了一个关于所有序列的广义.join方法
这些语义:

def join(self,seq):
T = type(self)
result = T()
如果len(seq):
结果= T(seq [0])
对于seq [1:]中的项目:
结果=结果+自我+ T(项目)
返回结果

这将允许以下代码:

[0] .join([[5],[42,5] ],[1,2,3],[23]])
Hi all!

I could not find out whether this has been proposed before (there are
too many discussion on join as a sequence method with different
semantics). So, i propose a generalized .join method on all sequences
with these semantics:

def join(self, seq):
T = type(self)
result = T()
if len(seq):
result = T(seq[0])
for item in seq[1:]:
result = result + self + T(item)
return result

This would allow code like the following:

[0].join([[5], [42, 5], [1, 2, 3], [23]])



我不喜欢把它放在所有序列上。如果你想要这个,我会把它作为一个函数提出(也许是内置的,也许是在其他模块中)。

另外,这个特定的实现很糟糕理念。重复的+ =到
结果可能会导致O(N ** 2)行为。

STeVe



I don''t like the idea of having to put this on all sequences. If you
want this, I''d instead propose it as a function (perhaps builtin,
perhaps in some other module).

Also, this particular implementation is a bad idea. The repeated += to
result is likely to result in O(N**2) behavior.

STeVe




嗨并且感谢您的快速回复,


i刚刚发现以下实施可能更快,并且b $ b更快,并且足够短,可以用于每个我的用例:


def join(sep,seq):

return reduce(lambda x,y:x + sep + y,seq,type( sep)())


所以,我正在撤回我的提议,而是建议在py3k中保持减少和

lambda;)。 />

再次感谢,

David。



Hi and thanks for the fast reply,

i just figured out that the following implementation is probably much
faster, and short enough to be used in place for every of my use cases:

def join(sep, seq):
return reduce(lambda x, y: x + sep + y, seq, type(sep)())

so, i''m withdrawing my proposal, and instead propose to keep reduce and
lambda in py3k ;).

thanks again,
David.


这篇关于功能提案:序列.join方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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