473,372 Members | 1,116 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,372 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 1041
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.