473,614 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help porting Perl function

kj


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.
Jun 27 '08 #1
10 1166
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
Jun 27 '08 #2
kj
>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.
Jun 27 '08 #3
On Jun 7, 2:42*pm, "Daniel Fetchinson" <fetchin...@goo glemail.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.c om/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.
Jun 27 '08 #4
kj 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).
The two solutions thus far use the .index() method, which itself runs in
O(n) time. This means that the provided functions run in O(n^2) time,
which can be a problem if the list is big. I'd go with this:

def partition(alist , criteria):
list1, list2 = [], []
for item in alist:
if criteria(item):
list1.append(it em)
else:
list2.append(it em)
return (list1, list2)

def mod(alist, criteria=lambda x: x % 2 == 0):
alist[:], blist = partition(alist , criteria)
return blist

>>partition(ran ge(10), lambda x: x % 2 == 0)
([0, 2, 4, 6, 8], [1, 3, 5, 7, 9])
>>l=range(10)
mod(l)
[1, 3, 5, 7, 9]
>>l
[0, 2, 4, 6, 8]
Jun 27 '08 #5
On Jun 7, 1:24*pm, kj <so...@987jk.co m.invalidwrote:
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.
If you want to avoid side-effects completely, return two lists, the
list of matches and the list of non-matches. In this example,
partition creates two lists, and stores all matches in the first list,
and mismatches in the second. (partition assigns to the associated
element of retlists based on False evaluating to 0 and True evaluating
to 1.)

def partition(lst, ifcond):
retlists = ([],[])
for i in lst:
retlists[ifcond(i)].append(i)
return retlists[True],retlists[False]

hasLeadingVowel = lambda x: x[0].upper() in "AEIOU"
matched,unmatch ed = partition("The quick brown fox jumps over the lazy
indolent dog".split(),
hasLeadingVowel )

print matched
print unmatched

prints:
['over', 'indolent']
['The', 'quick', 'brown', 'fox', 'jumps', 'the', 'lazy', 'dog']

-- Paul
Jun 27 '08 #6
On Jun 8, 6:05 am, eat...@gmail.co m wrote:
On Jun 7, 2:42 pm, "Daniel Fetchinson" <fetchin...@goo glemail.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.c om/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.
Not the same. The original responder was careful not to iterate over
the list which he was mutating.
>>def mod(alist):
.... return [alist.pop(alist .index(x)) for x in alist if x % 2 == 0]
....
>>a = range(10)
print mod(a), a
[0, 2, 4, 6, 8] [1, 3, 5, 7, 9]
>>a = [2,2,2,2,2,2,2,2]
print mod(a), a
[2, 2, 2, 2] [2, 2, 2, 2]
# should be [2, 2, 2, 2, 2, 2, 2, 2] []
Jun 27 '08 #7
On Jun 7, 5:56*pm, John Machin <sjmac...@lexic on.netwrote:
On Jun 8, 6:05 am, eat...@gmail.co m wrote:
On Jun 7, 2:42 pm, "Daniel Fetchinson" <fetchin...@goo glemail.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.c om/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.

Not the same. The original responder was careful not to iterate over
the list which he was mutating.
>def mod(alist):

... * *return [alist.pop(alist .index(x)) for x in alist if x % 2 == 0]
...>>a = range(10)
>print mod(a), a

[0, 2, 4, 6, 8] [1, 3, 5, 7, 9]>>a = [2,2,2,2,2,2,2,2]
>print mod(a), a

[2, 2, 2, 2] [2, 2, 2, 2]
# should be [2, 2, 2, 2, 2, 2, 2, 2] []
Alas, it appears my understanding of list comprehensions is
significantly less comprehensive than I thought =)
Jun 27 '08 #8
On Jun 8, 8:17 am, eat...@gmail.co m wrote:
On Jun 7, 5:56 pm, John Machin <sjmac...@lexic on.netwrote:
On Jun 8, 6:05 am, eat...@gmail.co m wrote:
On Jun 7, 2:42 pm, "Daniel Fetchinson" <fetchin...@goo glemail.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.c om/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.
Not the same. The original responder was careful not to iterate over
the list which he was mutating.
>>def mod(alist):
... return [alist.pop(alist .index(x)) for x in alist if x % 2 == 0]
...>>a = range(10)
>>print mod(a), a
[0, 2, 4, 6, 8] [1, 3, 5, 7, 9]>>a = [2,2,2,2,2,2,2,2]
>>print mod(a), a
[2, 2, 2, 2] [2, 2, 2, 2]
# should be [2, 2, 2, 2, 2, 2, 2, 2] []

Alas, it appears my understanding of list comprehensions is
significantly less comprehensive than I thought =)
It's nothing to do with list comprehensions, which are syntactical
sugar for traditional loops. You could rewrite your list comprehension
in the traditional manner:

def mod(alist):
ret = []
for x in alist:
if x % 2 == 0:
ret.append(alis t.pop(alist.ind ex(x))
return ret

and it would still fail for the same reason: mutating the list over
which you are iterating.

At the expense of even more time and memory you can do an easy fix:
change 'for x in alist' to 'for x in alist[:]' so that you are
iterating over a copy.

Alternatively, go back to basics:
>>def modr(alist):
.... ret = []
.... for i in xrange(len(alis t) - 1, -1, -1):
.... if alist[i] % 2 == 0:
.... ret.append(alis t[i])
.... del alist[i]
.... return ret
....
>>a = [2,2,2,2,2,2,2,2 ,2]
print modr(a), a
[2, 2, 2, 2, 2, 2, 2, 2, 2] []
>>>
HTH,
John
Jun 27 '08 #9
kj
In <72************ *************** *******@f24g200 0prh.googlegrou ps.comJohn Machin <sj******@lexic on.netwrites:
>It's nothing to do with list comprehensions, which are syntactical
sugar for traditional loops. You could rewrite your list comprehension
in the traditional manner...
and it would still fail for the same reason: mutating the list over
which you are iterating.
I normally deal with this problem by iterating backwards over the
indices. Here's how I coded the function (in "Python-greenhorn
style"):

def cull(list):
culled = []
for i in range(len(list) - 1, -1, -1):
if not_wanted(list[i]):
culled.append(l ist.pop(i))
return culled

....where not_wanted() is defined elsewhere. (For my purposes at the
moment, the greater generality provided by making not_wanted() a
parameter to cull() was not necessary.)

The specification of the indices at the beginning of the for-loop
looks pretty ignorant, but aside from that I'm happy with it.

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6343
by: MegaZone | last post by:
I'm having some issues with PHP DOMXML - in particular the get_elements_by_tagname method. Now, the PGP docs on this are, well, sparse, so maybe I'm just doing something stupid. I thought this method would behave like the 'findnodes' XML method in Perl. Namely that you can pass it an xpath statement and it will find nodes that match: $array = $node->get_elements_by_tagname($xpath); This is long so here's a pagebreak: And, indeed,...
0
2908
by: Dorthe Luebbert | last post by:
Hi, there is a new(er) version of John McNammara's Excel package for Perl: http://homepage.eircom.net/~jmcnamara/perl/prerel/Spreadsheet-WriteExcel-0.49.7.tar.gz The package writes Excel97 instead of Excel5, it has e.g. no longer the 255 char's limit for one cell etc. See:
5
2860
by: Ryan Liu | last post by:
Hi All, Now I am porting CC to GCC and I have some problems. Would you mind tell me some document which have some description how to port CC to GCC ?? Thank you very much. Ryan
5
1625
by: Kenton Groombridge | last post by:
Hi, I previous posted this in a gcc and g++ groups since that is the compiler I am working with, but I didn't get any response. Hopefully these are the right groups for this question. I am working (actually playing) on porting Alien vs Predator to Linux. I am using the code that is available via CVS from icculus.org. I recently upgraded my gcc to 3.4.1 and now a portion of the code doesn't compile. Nothing in the code has changed.
28
2462
by: Jed | last post by:
Hello to all! I have a couple of projects I intend starting on, and was wondering if someone here could make a suggestion for a good compiler and development environment. My goals are as follows: 1. Develop the project code on XP.
20
4053
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: li=;
4
2366
by: Chris Travers | last post by:
Hi all; A few years ago, I set about porting a PHP application from MySQL to PostgreSQL, after realizing that MySQL wasn't going to be able to handle it. In order to do this, I built a light, fast database abstraction layer which conforms to the behavior of the MySQL functions in PHP. This means that a large amount of porting work could be made simple using this porting layer if the application was originally used PHP's native MySQL...
4
1465
by: Stefko | last post by:
After a full day of attemping to port this PERL routine to PHP, I have given up on being able to do this myself, can anybody help? for($y=1;$y<=100;$y++){ $thename="NAME_$y"; if(length(${$thename})>3){ $num++; } } foreach($x=1; $x<=$num; $x++){ $qnt="QUANTITY_$x";
5
6681
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common requirement of programs. If you don't know the difference between a list and array don't worry about it. A list is just an array without a name. They both can hold the same type of data: scalars or strings. I will use the terms "list" and "array" to...
0
8198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8142
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8642
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8591
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8444
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5549
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.