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! 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.
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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)
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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: 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...
| |