473,396 Members | 1,755 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.

is there such a built-in funciton, similar to filter

wcc
Hello,

Beginner learning Python here. I know filter(lambda x: x > 3, [1, 2,
5, -3, 4, 8]) will give me a list [5, 4, 8]. What if I only need to
find the first item in the list that returns Ture when applying the
filter function. In this case, I only want to get the 5, and its index
2. Is there a built-in function, or function from a module for that?
I think it is not hard to write a function for this. But knowing
Python is "battery included", I thought I'd like to ask. Thanks for
your help.

- wcc

Mar 5 '06 #1
3 1247
wcc wrote:
Beginner learning Python here. I know filter(lambda x: x > 3, [1, 2,
5, -3, 4, 8]) will give me a list [5, 4, 8]. What if I only need to
find the first item in the list that returns Ture when applying the
filter function. In this case, I only want to get the 5, and its index
2. Is there a built-in function, or function from a module for that?
I think it is not hard to write a function for this. But knowing
Python is "battery included", I thought I'd like to ask. Thanks for
your help.


You can use a generator expression like this:
items = [1, 2, 5, -3, 4, 8]
((index, item) for index, item in enumerate(items) if item > 3).next()

(2, 5)
--
Michael Hoffman
Mar 5 '06 #2
wcc
Thanks a lot Michael.

Mar 5 '06 #3

"Michael Hoffman" <ca*******@mh391.invalid> wrote in message
news:du**********@gemini.csx.cam.ac.uk...
wcc wrote:
Beginner learning Python here. I know filter(lambda x: x > 3, [1, 2,
5, -3, 4, 8]) will give me a list [5, 4, 8]. What if I only need to
find the first item in the list that returns Ture when applying the
filter function. In this case, I only want to get the 5, and its index
You have made two important changes to the function return value.
Filter returns a list of items. You want an item and index.
What if there is no first item?
2. Is there a built-in function, or function from a module for that?
I think it is not hard to write a function for this. But knowing
Python is "battery included", I thought I'd like to ask.

A 'battery' is something like the email, html, xml modules that assist
major application areas. Not every easily written 3-line function ;-)
You can use a generator expression like this:
items = [1, 2, 5, -3, 4, 8]
((index, item) for index, item in enumerate(items) if item >
3).next() (2, 5)

items = [0,1,2,3]
((index, item) for index, item in enumerate(items) if item > 3).next()


Traceback (most recent call last):
File "<pyshell#82>", line 1, in -toplevel-
((index, item) for index, item in enumerate(items) if item > 3).next()
StopIteration

I believe ifilter from the itertools module will act the same.

Terry Jan Reedy


Mar 5 '06 #4

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

Similar topics

2
by: Steve Doody | last post by:
When I add #include <Python.h> to a header file, GCC tells me, No such file or directory. Am trying to learn how to combine C++ and Python. Using SuSE 9.0 Linux, 2.4.21-231-default GCC 3.3.1...
1
by: JimmyT | last post by:
I just configured and installed 2.3.4 and noticed there is no datetime module. I noticed there is a datetimemodule.c file that did not get built (ie no object file). Is there something I need to...
1
by: Alex Elbert | last post by:
Hi I have built dynamic HTMLTable. Now I want to attach it directly to the Email Body - it is already built, so why not to use a ready table. However, I cannot find the way of getting plain HTML...
0
by: Andrew Crook | last post by:
does MYSQL have a quota built into it! I need it limit the size of each database AndiC
1
by: Mark | last post by:
Is there a way to execute a statement that is built dynamically by a .NET application. For example I have a loop that is reading values from a database and I want to do something like the...
4
by: Yasutaka Ito | last post by:
Hi, Is there a way to determine which version of .NET Framework any given assembly is built with? thanks! -Yasutaka
1
by: William | last post by:
Looking for a pre built dotnet corporate or small business website template.
48
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
14
by: Joseph Geretz | last post by:
Basically, I just need an efficient way to store and retrieve keys from a list. I don't need key/value pairs. I've implemented this with the standard HashTable, but the value column is a waste...
3
by: drewj840 | last post by:
I built a Windows service that sweeps a set of folders every 60 seconds and puts the files into a SQL Server database. I am creating a second service that will delete this set of folders and recreate...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.