473,387 Members | 1,388 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,387 software developers and data experts.

List search

I'm having a problem with searching a list. Here's my code:

mylist = ['x11', 'x11-wm', 'x11-system']

for line in mylist:
if 'x11' in line:
print line

This results in the following output:

x11
x11-wm
x11-system

I'm looking to return the list item that just has 'x11'. How can I
structure my search term so that this output is returned?
Sep 28 '07 #1
5 1043
On Fri, 28 Sep, Kevin Walzer wrote:
I'm having a problem with searching a list. Here's my code:

mylist = ['x11', 'x11-wm', 'x11-system']

for line in mylist:
if 'x11' in line:
print line
Just compare for equality:

if line == 'x11':

or

print "\n".join(x for x in mylist if x == 'x11')

--
Stefan Bellon
Sep 28 '07 #2
Kevin Walzer wrote:
I'm having a problem with searching a list. Here's my code:

mylist = ['x11', 'x11-wm', 'x11-system']

for line in mylist:
if 'x11' in line:
print line

This results in the following output:

x11
x11-wm
x11-system

I'm looking to return the list item that just has 'x11'. How can I
structure my search term so that this output is returned?
line == 'x11'

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Sep 28 '07 #3
On 28/09/2007, Kevin Walzer <kw@codebykevin.comwrote:
I'm having a problem with searching a list. Here's my code:

mylist = ['x11', 'x11-wm', 'x11-system']

for line in mylist:
if 'x11' in line:
print line

This results in the following output:

x11
x11-wm
x11-system
That output is correct, you are asking your script to print any list
item containing x11 when what you actually wanted was a list item that
is the string 'x11'

mylist = ['x11', 'x11-wm', 'x11-system']
for item in mylist:
if item == 'x11':
print line

If there is only ever one 'x11' in the list you could also consider

print mylist.index('x11')

and

print mylist[mylist.index('x11')]

Also, before iterating the whole list check that 'x11' exists

if 'x11' in mylist:
do stuff

and list comprehesions

print [x for x in mylist if x == 'x11']

HTH :)

Tim Williams
Sep 28 '07 #4
Robert Kern wrote:
line == 'x11'
D'oh! That was simple, wasn't it? *smacks head*

That did the trick. Thanks!

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Sep 28 '07 #5
Kevin Walzer wrote:
I'm having a problem with searching a list. Here's my code:

mylist = ['x11', 'x11-wm', 'x11-system']

for line in mylist:
if 'x11' in line:
print line

This results in the following output:

x11
x11-wm
x11-system

I'm looking to return the list item that just has 'x11'.
Why do you want to have an item you already know?

If you just wanted to check *if* the string "x11" is in the list,
you'd use

if "x11" in mylist:

Regards,
Björn

--
BOFH excuse #221:

The mainframe needs to rest. It's getting old, you know.

Sep 29 '07 #6

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

Similar topics

19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
2
by: Mark Drummond | last post by:
Hi all. I've been using Perl for many years now, but I am a "use it and learn it as you need it" type. I having some trouble passing a list to the "search" subroutine from Net::LDAP. I am trying...
12
by: ljungers | last post by:
I'm on the home streach of my project and found that my "Reset for New Search" command button not working as desired. What should happen is that when the button is clicked a Event Procedure is run....
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
6
by: Henrik Goldman | last post by:
Hello, I have a dataset which consist of a string username and string hostname as a key and then an integer representing a count as the matching "second" value in a pair. So far I've used...
1
by: ebernedo | last post by:
Hey guys, I have two main questions First off (pictures are kind of blurry) I have this table http://i197.photobucket.com/albums/aa109/ebernedo/DiscTable.jpg And thats my database I use my...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
2
by: hollinshead | last post by:
hi there, i have been having this issue for quite some time now and i cant seem to get my head around it. I am trying to create a database for candidates CV's and covering letters. basically the data...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.