473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

split a list based on a predicate

Hi!

Is there a functional way to do this?

I have an array [0,1,2,3,0,1,2,2,3] and I want the first chunk of
non-decreasing values from this array (eg: In this case I want
[0,1,2,3])

Thanks,

Rajanikanth
Oct 8 '08 #1
3 1354
Rajanikanth Jammalamadaka:
Is there a functional way to do this?
I have an array [0,1,2,3,0,1,2,2,3] and I want the first chunk of
non-decreasing values from this array (eg: In this case I want
[0,1,2,3])
In Python sometimes the best way to write the code isn't functional,
this is readable code:

s = [3,1,2,3,0,1,2,2,3,3,2]

it = iter(s)
prec = it.next() #fix this if it can be empty
groups = [[prec]]
for x in it:
if x < prec:
groups.append([])
groups[-1].append(x)
prec = x
print groups

Output:
[[3], [1, 2, 3], [0, 1, 2, 2, 3, 3], [2]]

If you want more fancy code you may use something like this:

class Infinite:
def __cmp__(self, other):
return 0 if isinstance(other, Infinite) else 1
infinite = Infinite()

it = iter(s)
prec = infinite
groups = []
for x in it:
if x < prec:
groups.append([])
groups[-1].append(x)
prec = x
print groups

If you really want functional code you may try to use groupby().

bye,
bearophile
Oct 9 '08 #2
Hi,

On Oct 8, 6:36*pm, "Rajanikanth Jammalamadaka" <rajanika...@gmail.com>
wrote:
Hi!

Is there a functional way to do this?

I have an array [0,1,2,3,0,1,2,2,3] and I want the first chunk of
non-decreasing values from this array (eg: In this case I want
[0,1,2,3])

Thanks,

Rajanikanth
Here is an idea. It is not the most efficient code!

def combine(l, a):
if not l or l[-1]<a: l.append(a)
return l

reduce(combine, [0,1,2,3,0,1,2,2,3], [])

best regards,
beginner

Oct 9 '08 #3
Thanks for all of your replies.

Rajanikanth

On Wed, Oct 8, 2008 at 11:59 PM, beginner <zy*******@gmail.comwrote:
Hi,

On Oct 8, 6:36 pm, "Rajanikanth Jammalamadaka" <rajanika...@gmail.com>
wrote:
>Hi!

Is there a functional way to do this?

I have an array [0,1,2,3,0,1,2,2,3] and I want the first chunk of
non-decreasing values from this array (eg: In this case I want
[0,1,2,3])

Thanks,

Rajanikanth

Here is an idea. It is not the most efficient code!

def combine(l, a):
if not l or l[-1]<a: l.append(a)
return l

reduce(combine, [0,1,2,3,0,1,2,2,3], [])

best regards,
beginner

--
http://mail.python.org/mailman/listinfo/python-list


--
Rajanikanth
Oct 9 '08 #4

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

Similar topics

5
by: oliver | last post by:
hi there i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a...
5
by: matthias_k | last post by:
Hi, I need to sort elements of a std::list using a function predicate, something like: bool predicate( const& M m1, const& M m2 ) { return m1.somedata < m2.somedata; } I tried to call...
3
by: Abhi | last post by:
In the following hypothetical example I want to build a generic list of unique string items. How should I implement the pred function so that it returns true/false if target string exists in the...
5
by: SKumar | last post by:
Hi All, I have a list which contains my class objects. My class is having two variables. I want to sort the list based on these two variables. Ex : class Foo { ... ...
2
by: sianan | last post by:
I am having a problem doing the following in generics. I have two list of a custom item type. I need to iterate through the first list and match each item against another list to see if there is...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
1
by: maxtoroq | last post by:
I know how to work the .FindAll method of the List class, but is there a way to split a list into 2 lists, one containing all the items that match a certain criteria and one that doesn't?
8
by: Flyzone | last post by:
Hi, i have a problem with the split function and regexp. I have a file that i want to split using the date as token. Here a sample: ----- Mon Apr 9 22:30:18 2007 text text Mon Apr 9...
0
by: Tim Chase | last post by:
Is there a functional way to do this? Sounds like a use for a generator wrapper: def monotonic(iterator): i = iter(iterator) prev = i.next() yield prev while True:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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 project—planning, coding, testing,...

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.