473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Selecting elements from a list

-----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>

iEYEARECAAYFAj9 XtcEACgkQYu1fMm OQldVX0wCeJ7gxV 82QVCqRZ3r44vbk Fquf
M3QAnRGXnV3l/KslD7LNxT9roVQh GgJM
=aVpW
-----END PGP SIGNATURE-----
Jul 18 '05 #1
9 3338
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/


Jul 18 '05 #2
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

Jul 18 '05 #3
* Duncan Smith (bu*****@urubu. freeserve.co.uk ) wrote:

"Martin Christensen" <kn************ ************@gv dnet.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

Jul 18 '05 #4
-----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>

iEYEARECAAYFAj9 YJ98ACgkQYu1fMm OQldVcoACg4UwLu vBfdx1aZV0qXJgo mgQ4
5OkAnAgsf4fdEY0 j9ekDPOrWytpKPq hJ
=NcyH
-----END PGP SIGNATURE-----
Jul 18 '05 #5

"Martin Christensen" <kn************ ************@gv dnet.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("ma in()")
----------------
Kindly
Michael P
Jul 18 '05 #6

Martin,
Computer Science at Aalborg I see. A Bayes net man perchance? I
hope Uffe, Kristian et al are well. :-)

Duncan
Jul 18 '05 #7
-----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>

iEYEARECAAYFAj9 YwPIACgkQYu1fMm OQldWiCwCgr0YsG dB929LnDvpivNI3 C6U/
KG0AoOeVKunYa0U d/9UI/C99JaihY9bM
=252w
-----END PGP SIGNATURE-----
Jul 18 '05 #8

"Martin Christensen" <kn************ ************@gv dnet.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
Jul 18 '05 #9
-----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>

iEYEARECAAYFAj9 Y8AcACgkQYu1fMm OQldWpSACdEn8FU R9Oh3CHya7SaEFp Y+y5
CTQAoMd5w0JKmxi T8ULcv+RnD+lBQY Sk
=+uoP
-----END PGP SIGNATURE-----
Jul 18 '05 #10

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

Similar topics

18
5679
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() { document.forms.elements.value = "<value from DB>"; }
1
1518
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 list is simply a select element with options that looks like this: <SELECT> <OPTION VALUE="1">A <OPTION VALUE="2">B <OPTION VALUE="3">C...
3
2400
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 list boxes'). Users can add and remove items from the list boxes. When the users are adding and removing items, the list boxes are single
4
1601
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....
6
3128
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 identifier, I would like a dropdown combo box (lookup from the "order_type" table) to change the type of the order. I also need an update command...
1
1539
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 these elements has an attribute "Id" which has a unique value. Some of these elements have a parent-child relationship, specified with a "ResultId"...
0
954
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. -- Stand Fast, tjg.
7
4647
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 elements in age
1
2623
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 select elements by pressing up/down button on the mobile phone... at times by default i'm able to select items by clicking on the up/down buttons......
0
7478
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7923
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5984
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4960
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1901
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 we have to send another system
0
722
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.