three argument filtering

Lets assume I have defined function:

def threeArgs(x,y,z):
  return ... # Returns True or False

I also have a list of elements:

a = [elem1, elem2, ..., elemN]

I would like to construct another list b containing first and last element form list a and those elements Xn in between for which threeArgs(Xn-1,Xn, Xn+1) returns true.

How to do it in elegant pythonic way?

Answers


Use slicing to create three shifted lists, then zip them together:

[a[0]] + [y for x, y, z in zip(a, a[1:], a[2:]) if threeArgs(x, y, z)] + [a[-1]]

Need Your Help

$(document).ready(function() is not working after page refresh in ie6

jquery jquery-post

$(document).ready(function() not working after page refresh in ie 6.Is there any problem to use?Whether it will work in all condition when page load happens...In other browser also?

Trying to JSON.parse a file

javascript json node.js

I'm downloading a file and trying to use JSON.parse, which should return { dateTime: "2012-04-07T17:15:00.000-05:00", value: "1065.91" } Am I passing the correct object through JSON.parse and struc...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.