需要帮助移植Perl功能 [英] Need help porting Perl function

查看:58
本文介绍了需要帮助移植Perl功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




嗨。我想移植一个Perl函数来做一些我不知道如何用Python知道怎么做的事情。 (事实上​​,它甚至可能是
明显不是Pythonic!)


原始的Perl函数接受一个数组的引用,删除

来自此数组的满足特定条件的所有元素,

并返回包含已删除元素的列表。因此

这个函数返回一个值*和*有一个主要的副作用,即

原始参数的目标数组将被修改(这个

是我怀疑可能是非Pythonic的部分。


Python函数能达到同样的效果吗?如果没有,那么如何在Python中使用类似的功能代码?b
基本上问题

是根据一些标准将一个列表分成两个。


TIA!


Kynn


-

注意:在我的地址中,第一期之前的所有内容都是倒退的;

和最后一期,以及所有内容在它之后,应该被丢弃。

解决方案

嗨。我想移植一个Perl函数,它做了一件我不想要的东西.blockquote class =post_quotes>
知道怎么做Python。 (事实上​​,它甚至可能是
明显不是Pythonic!)


原始的Perl函数接受一个数组的引用,删除

来自此数组的满足特定条件的所有元素,

并返回包含已删除元素的列表。因此

这个函数返回一个值*和*有一个主要的副作用,即

原始参数的目标数组将被修改(这个

是我怀疑可能是非Pythonic的部分。


Python函数能达到同样的效果吗?如果没有,那么如何在Python中使用类似的功能代码?b
基本上问题

是根据某些标准将一个列表拆分为两个。



这个函数将获取一个整数列表并对其进行修改,例如它将删除甚至是整数的
。删除的整数将作为

新列表返回(免责声明:我100%确定它可以做得更好,更多

优化等等):< br $>

def mod(alist):

old = alist [:]

ret = []

for i in old:

if i%2 == 0:

ret.append(alist.pop(alist.index(i)))


返回


x =范围(10,20)


打印x

r = mod(x)

打印r

打印x


HTH,

Daniel

-

Psss,psss,爱不释手! - http://www.cafepress.com/putitdown


>这个函数将获取一个整数列表并对其进行修改,例如


>它甚至会删除整数。已移除的整数将作为
新列表返回



< snip>


太棒了!


谢谢!


kynn


-

注意:在我的在第一个时期倒退之前解决所有问题;

和最后一个时期以及之后的一切都应该被丢弃。


6月7日, 2:42 * pm,Daniel Fetchinson < fetchin ... @ googlemail.com>

写道:


嗨。 *我想要移植一个Perl函数来做一些我不知道如何用Python知道怎么做的事情。 *(事实上,它甚至可能是
显然不是Pythonic!)


原始的Perl函数引用一个数组,从这个数组中删除满足特定条件的所有元素,并且返回包含已删除元素的列表。 *因此

这个函数返回一个值*和*有一个主要的副作用,即

原始参数的目标数组将被修改(这个

是我怀疑可能是非Pythonic的部分。


Python函数能达到同样的效果吗? *如果没有,如何将一个代码用于Python中的类似功能? *基本上问题

是根据某些标准将一个列表拆分为两个。



这个函数将获取一个整数列表并对其进行修改,例如它将删除甚至整数的
。删除的整数将作为

新列表返回(免责声明:我100%确定它可以做得更好,更多

优化等等):


def mod(alist):

* * old = alist [:]

* * ret = []

* * for old in old:

* * * * if i%2 == 0:

* * * * * * ret.append( alist.pop(alist.index(i)))


* *返回ret


x =范围(10,20)

打印x

r = mod(x)

打印r

打印x


HTH,

Daniel

-

Psss,psss,爱不释手! -http://www.cafepress.com/putitdown



def mod(alist):

return [alist.pop(alist。 index(x))对于x in alist如果x%2 ==

0]


alist = range(10,20)

blist = mod(alist)


打印alist

打印blist


列表同样的东西理解。




Hi. I''d like to port a Perl function that does something I don''t
know how to do in Python. (In fact, it may even be something that
is distinctly un-Pythonic!)

The original Perl function takes a reference to an array, removes
from this array all the elements that satisfy a particular criterion,
and returns the list consisting of the removed elements. Hence
this function returns a value *and* has a major side effect, namely
the target array of the original argument will be modified (this
is the part I suspect may be un-Pythonic).

Can a Python function achieve the same effect? If not, how would
one code a similar functionality in Python? Basically the problem
is to split one list into two according to some criterion.

TIA!

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.

解决方案

Hi. I''d like to port a Perl function that does something I don''t

know how to do in Python. (In fact, it may even be something that
is distinctly un-Pythonic!)

The original Perl function takes a reference to an array, removes
from this array all the elements that satisfy a particular criterion,
and returns the list consisting of the removed elements. Hence
this function returns a value *and* has a major side effect, namely
the target array of the original argument will be modified (this
is the part I suspect may be un-Pythonic).

Can a Python function achieve the same effect? If not, how would
one code a similar functionality in Python? Basically the problem
is to split one list into two according to some criterion.

This function will take a list of integers and modify it in place such
that it removes even integers. The removed integers are returned as a
new list (disclaimer: I''m 100% sure it can be done better, more
optimized, etc, etc):

def mod( alist ):
old = alist[:]
ret = [ ]
for i in old:
if i % 2 == 0:
ret.append( alist.pop( alist.index( i ) ) )

return ret

x = range(10,20)

print x
r = mod( x )
print r
print x

HTH,
Daniel
--
Psss, psss, put it down! - http://www.cafepress.com/putitdown


>This function will take a list of integers and modify it in place such

>that it removes even integers. The removed integers are returned as a
new list

<snip>

Great!

Thanks!

kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.


On Jun 7, 2:42*pm, "Daniel Fetchinson" <fetchin...@googlemail.com>
wrote:

Hi. *I''d like to port a Perl function that does something I don''t
know how to do in Python. *(In fact, it may even be something that
is distinctly un-Pythonic!)

The original Perl function takes a reference to an array, removes
from this array all the elements that satisfy a particular criterion,
and returns the list consisting of the removed elements. *Hence
this function returns a value *and* has a major side effect, namely
the target array of the original argument will be modified (this
is the part I suspect may be un-Pythonic).

Can a Python function achieve the same effect? *If not, how would
one code a similar functionality in Python? *Basically the problem
is to split one list into two according to some criterion.


This function will take a list of integers and modify it in place such
that it removes even integers. The removed integers are returned as a
new list (disclaimer: I''m 100% sure it can be done better, more
optimized, etc, etc):

def mod( alist ):
* * old = alist[:]
* * ret = [ ]
* * for i in old:
* * * * if i % 2 == 0:
* * * * * * ret.append( alist.pop( alist.index( i ) ) )

* * return ret

x = range(10,20)

print x
r = mod( x )
print r
print x

HTH,
Daniel
--
Psss, psss, put it down! -http://www.cafepress.com/putitdown

def mod( alist ):
return [ alist.pop( alist.index( x ) ) for x in alist if x % 2 ==
0 ]

alist = range(10,20)
blist = mod( alist )

print alist
print blist

The same thing with list comprehensions.


这篇关于需要帮助移植Perl功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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