473,406 Members | 2,439 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,406 software developers and data experts.

fast way to filter a set?

I know I can do things like

s=Set(range(1,11))
s=Set(filter(lambda x:x%2==0,s))

But this seems a bit slow since filter returns a list which then must
be converted back to a set. Any tips? Thanks!
Jul 18 '05 #1
4 7346
fo*************@yahoo.com.tw (Fortepianissimo) writes:
I know I can do things like

s=Set(range(1,11))
s=Set(filter(lambda x:x%2==0,s))

But this seems a bit slow since filter returns a list which then must
be converted back to a set. Any tips? Thanks!


Can you ?
I tried python2.3 and 2.2 and it replies
: "NameError: name 'Set' is not defined" .

There is a PEP but it seems it's steel 'under consideration'.
http://www.python.org/peps/pep-0218.html

May be , if this proposal is accepted you
one day could write :
{ x for x in S if x%2 == 0 } # S be a set .

bye.
Jul 18 '05 #2

fortepianissimo> I know I can do things like
fortepianissimo> s=Set(range(1,11))
fortepianissimo> s=Set(filter(lambda x:x%2==0,s))

fortepianissimo> But this seems a bit slow since filter returns a list
fortepianissimo> which then must be converted back to a set. Any tips?

The only thing which comes to mind is:
s = sets.Set(range(1,11))
s Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) keys = list(s)
dummy = [s.discard(x) for x in keys if x%2]
s

Set([2, 4, 6, 8, 10])

You still create a list, but don't create a second set.

Skip

Jul 18 '05 #3
Nicola Mingotti wrote:
fo*************@yahoo.com.tw (Fortepianissimo) writes:
I know I can do things like

s=Set(range(1,11))
s=Set(filter(lambda x:x%2==0,s))

But this seems a bit slow since filter returns a list which then must
be converted back to a set. Any tips? Thanks!


Can you ?
I tried python2.3 and 2.2 and it replies
: "NameError: name 'Set' is not defined" .


In 2.3 it works if you do

from sets import Set

first.

Peter
Jul 18 '05 #4
Fortepianissimo wrote:
I know I can do things like

s=Set(range(1,11))
s=Set(filter(lambda x:x%2==0,s))

But this seems a bit slow since filter returns a list which then must
be converted back to a set. Any tips? Thanks!


The Set constructor accepts any iterable, so you can do it all with
iterators instead of temporary lists:

from sets import Set
from itertools import ifilter

s = Set(range(1, 11))
print Set(ifilter(lambda x: x % 2 == 0, s))

Peter

Jul 18 '05 #5

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

Similar topics

9
by: Robin Cull | last post by:
Imagine I have a dict looking something like this: myDict = {"key 1": , "key 2": , "key 3": , "key 4": } That is, a set of keys which have a variable length list of associated values after...
8
by: Neil | last post by:
I have a very puzzling situation with a database. It's an Access 2000 mdb with a SQL 7 back end, with forms bound using ODBC linked tables. At our remote location (accessed via a T1 line) the time...
14
by: WindAndWaves | last post by:
Hi Gurus What is the fatest way to select all the items in a list box? At the moment, I do this for each itm in ctl..... type of method, but with more than 2,000 items, this can be a little...
6
by: DC | last post by:
Hi, I am programming a search catalogue with 200000 items (and growing). I am currently using the SQL Server 2000 fulltext engine for this task but it does not fit the requirements anymore. ...
4
by: Harry | last post by:
Hello, I am using a 2-dimensional matrix for image manipulation and recognition. The x-axes is image.width pixels long and the y-axes image.height. All fields have a RGB integer value. To store...
2
by: DraguVaso | last post by:
Hi, I need a FAST way to put the content of a file in a datatable (one record for each line in the file). I have a routine for it, but it takes me too much time (2-5 seconds for each file) to...
9
by: lars_woetmann | last post by:
I have a list I filter using another list and I would like this to be as fast as possible right now I do like this: i tried using the method filter: filter(lambda x: x not in list2, list1)
3
by: cartoper | last post by:
I am working on writing an Apache Module (in C/C++) that needs to resize (down) large images (4MB to 16MB) VERY quickly. The objective is not to make the images look great for printing, just look...
2
by: Mathieu Prevot | last post by:
Hi, I use in a bourne shell script the following filter: sed '/watch?v=/! d;s/.*v=//;s/\(.\{11\}\).*/\1/' \ | sort | uniq | awk 'ORS=" "{print $1}' that give me all sets of 11 characters...
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: 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
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...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.