-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Howdy!
Suppose I have a list A containing elements that I want to put into
list B if they satisfy some property. The obvious way of doing it
would be,
B = []
for i in A:
if A.property():
B.append(i)
Now, list comprehensions, map() etc. can make a lot of list operations
easier, but I haven't found a shorter, more elegant way of doing this.
Have I missed something, or will I have to stick to my explicit loops?
Martin
- --
Homepage: http://www.cs.auc.dk/~factotum/
GPG public key: http://www.cs.auc.dk/~factotum/gpgkey.txt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>
iEYEARECAAYFAj9XtcEACgkQYu1fMmOQldVX0wCeJ7gxV82QVC qRZ3r44vbkFquf
M3QAnRGXnV3l/KslD7LNxT9roVQhGgJM
=aVpW
-----END PGP SIGNATURE----- 9 3333
Martin Christensen wrote: B = [] for i in A: if A.property(): B.append(i)
B = [ i for i in A if i.property() ]
Seems elegant, but then I'd like dictionary-comps if they came along,
Mike
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder http://members.rogers.com/mcfletch/
Martin Christensen wrote: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Howdy!
Suppose I have a list A containing elements that I want to put into list B if they satisfy some property. The obvious way of doing it would be,
B = [] for i in A: if A.property(): B.append(i)
Now, list comprehensions, map() etc. can make a lot of list operations easier, but I haven't found a shorter, more elegant way of doing this. Have I missed something, or will I have to stick to my explicit loops?
Martin
B = filter(lambda x: x.property(), A)
David
* Duncan Smith (bu*****@urubu.freeserve.co.uk) wrote: "Martin Christensen" <kn************************@gvdnet.dk> wrote in message news:87************@gvdnet.dk... -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Howdy!
Suppose I have a list A containing elements that I want to put into list B if they satisfy some property. The obvious way of doing it would be,
B = [] for i in A: if A.property(): B.append(i)
Now, list comprehensions, map() etc. can make a lot of list operations easier, but I haven't found a shorter, more elegant way of doing this. Have I missed something, or will I have to stick to my explicit loops?
Martin
Is this the sort of thing you mean?
A = [0, 1, 2, 'four', 'five', 6.0] [x for x in A if isinstance(x, str)] ['four', 'five']
filter() works well as well: foo
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] filter(lambda x: x > 4, foo)
[5, 6, 7, 8, 9]
Jeremy Jones
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 > "David" == David C Fox <da*******@post.harvard.edu> writes:
David> B = filter(lambda x: x.property(), A)
Now, if I'd paid better attention to the library documentation... :-)
Martin
- --
Homepage: http://www.cs.auc.dk/~factotum/
GPG public key: http://www.cs.auc.dk/~factotum/gpgkey.txt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>
iEYEARECAAYFAj9YJ98ACgkQYu1fMmOQldVcoACg4UwLuvBfdx 1aZV0qXJgomgQ4
5OkAnAgsf4fdEY0j9ekDPOrWytpKPqhJ
=NcyH
-----END PGP SIGNATURE-----
"Martin Christensen" <kn************************@gvdnet.dk> schrieb im
Newsbeitrag news:87************@gvdnet.dk... -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Howdy!
Suppose I have a list A containing elements that I want to put into list B if they satisfy some property. The obvious way of doing it would be,
B = [] for i in A: if A.property(): B.append(i)
Now, list comprehensions, map() etc. can make a lot of list operations easier, but I haven't found a shorter, more elegant way of doing this. Have I missed something, or will I have to stick to my explicit loops?
from random import random
rowA1 =[random()]*100000; rowA2=[random()]*100000
rowB1 = rowA1[:]; rowB2 =rowA2[:]
rowC1 = rowA1[:]; rowC2 =rowA2[:]
def A(row1,row2):
for x in row2:
if x<0.5:
row1.append(x)
def B(row1, row2):
row1 += filter(lambda x: x<0.5,row2)
def C(row1, row2):
row1 += [x for x in row2 if x<0.5]
def main():
A(rowA1,rowA2)
B(rowB1,rowB2)
C(rowC1,rowC2)
profile.run("main()")
----------------
Kindly
Michael P
Martin,
Computer Science at Aalborg I see. A Bayes net man perchance? I
hope Uffe, Kristian et al are well. :-)
Duncan
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 > "Duncan" == Duncan Smith <bu*****@urubu.freeserve.co.uk> writes:
Duncan> Computer Science at Aalborg I see. A Bayes net man perchance?
Duncan> I hope Uffe, Kristian et al are well. :-)
Haha! If you're talking about the guys I'm thinking of, I just came
home from a lovely Friday afternoon beer with them. :-) And yes, I
happen to be doing a project involving Bayesian networks, but I'm
originally a database guy. To keep it relevant, I did my masters
thesis implementation stuff in Python and am very happy I did.
Martin
- --
Homepage: http://www.cs.auc.dk/~factotum/
GPG public key: http://www.cs.auc.dk/~factotum/gpgkey.txt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>
iEYEARECAAYFAj9YwPIACgkQYu1fMmOQldWiCwCgr0YsGdB929 LnDvpivNI3C6U/
KG0AoOeVKunYa0Ud/9UI/C99JaihY9bM
=252w
-----END PGP SIGNATURE-----
"Martin Christensen" <kn************************@gvdnet.dk> wrote in message
news:87************@gvdnet.dk... -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
>> "Duncan" == Duncan Smith <bu*****@urubu.freeserve.co.uk> writes:
Duncan> Computer Science at Aalborg I see. A Bayes net man perchance? Duncan> I hope Uffe, Kristian et al are well. :-)
Haha! If you're talking about the guys I'm thinking of, I just came home from a lovely Friday afternoon beer with them. :-) And yes, I happen to be doing a project involving Bayesian networks, but I'm originally a database guy. To keep it relevant, I did my masters thesis implementation stuff in Python and am very happy I did.
Martin
Yes, messrs. Kjaerulff and Olesen. All my Bayes net stuff is in Python. I
must tidy it up (and rewrite my Digraph class) so I can make it available.
Duncan
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 > "Duncan" == Duncan Smith <bu*****@urubu.freeserve.co.uk> writes:
Duncan> Yes, messrs. Kjaerulff and Olesen.
Then I'm afraid that we were talking about different people, and these
people are probably less likely to run about getting wet on random
Fridays. :-) They're swell people, though, or at least Kristian is, in
my experience (only know Uffe by sight, hardly even voice). Oh well,
it was still amusing despite the Fates not playing _that_ much with
us.
Martin
- --
Homepage: http://www.cs.auc.dk/~factotum/
GPG public key: http://www.cs.auc.dk/~factotum/gpgkey.txt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>
iEYEARECAAYFAj9Y8AcACgkQYu1fMmOQldWpSACdEn8FUR9Oh3 CHya7SaEFpY+y5
CTQAoMd5w0JKmxiT8ULcv+RnD+lBQYSk
=+uoP
-----END PGP SIGNATURE----- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: booner |
last post by:
I have a form that when it loads I would like to highlight the values (from
a DB) that have been selected in a multiple selection list (<select
multiple="true">.
function onLoad()
{...
|
by: newcomer |
last post by:
I have a javascript index that is similar to the one in the Windows
help. It has a text field that allows the you to type text and it
finds the closest item in the list below the text field. The...
|
by: james.dixon |
last post by:
Hi
I was wondering if anyone else had had this problem before (can't find
anything on the web about it).
I have three select elements (list boxes - from here on I'll refer to
them as 'the...
|
by: Brian |
last post by:
How can I use javascript to select Items in a List/Menu? For example:
(value1, value2) will select value1 and value2 in a list menu....
|
by: Mike Wilson |
last post by:
Dear Group,
I have a heirarchical set of database tables, say - "order" and "order_type"
and want to display a series of orders in a grid control, and in place of
the order_type foreign key...
| |
by: jdhcards |
last post by:
Hello,
I've been banging my head against a problem all day without a solution,
and I'm hoping you all can help. I've got a piece of XML that defines a
set of elements in a flat list. Each of...
|
by: Timothy Grant |
last post by:
On Mon, Oct 13, 2008 at 2:29 PM, aditya shukla
<adityashukla1983@gmail.comwrote:
I would start here http://docs.python.org/library/random.html you are
likely interested in choice or sample.
...
|
by: swami |
last post by:
What is the query for selecting non duplicate elements
for eg:
no name age
1 siva 28
2 blair 32
3 mano 28
i want to select blair which hasn't got any duplicate...
|
by: libish |
last post by:
hi all,
can any one suggest me on selcting a list element?
i am displaying a list containing some items dynamically...
after displaying the content and the form holding this list, i need to...
|
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:
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: 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: 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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |