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

List comparison help please

I am trying to compare a list of items to the list of files generated
by os.listdir. I am having trouble getting this to work and think I
may be going down the wrong path. Please let me know if hter is a
better way to do this. THis is what I have for my class so far:

import os, sys

class MatchList:
def __init__(self, dir, file):
self.dir = os.listdir(dir)
self.flist = open(file, 'r').readlines()
self.matches = []
def matcher(self):
#~ matches = []
for fname in self.dir:
#~ print fname
if fname[-4] == '.':
for item in self.flist:
if item == fname[:-4]:
pass
else:
self.matches.append(fname)
#~ else:
#~ break
#~ if self.matches == '':
#~ print "Sorry, there was no match."
#~ else:
#~ for item in matches:
#~ print item
def matchedFiles(self):
for item in self.matches: print item

if __name__ == '__main__':
dir = sys.argv[1]
file = sys.argv[2]
list = open(file, 'r').readlines()
test = MatchList(dir, file)
test.matcher()
test.matchedFiles()
Thanks in advance for your help.

:)
SA

Aug 20 '06 #1
4 2703
20 Aug 2006 14:47:14 -0700, Bucco <ph*******@gmail.com>:
I am trying to compare a list of items to the list of files generated
by os.listdir. I am having trouble getting this to work and think I
may be going down the wrong path. Please let me know if hter is a
better way to do this. THis is what I have for my class so far:
Have you tried using sets?
>>import os
os.listdir('/')
['lost+found', 'var', 'etc', 'media', 'cdrom', 'bin', 'boot', 'dev',
'home', 'initrd', 'lib', 'mnt', 'opt', 'proc', 'root', 'sbin', 'srv',
'sys', 'tmp', 'usr', 'initrd.img', 'vmlinuz', 'windows',
'initrd.img.old', 'vmlinuz.old']
>>s = set(os.listdir('/'))
p = set(['opt', 'mnt', 'initrd', 'home', 'tmp', 'lib', 'media',
'boot', 'usr', 'var', 'proc', 'bin', 'sys', 'initrd.img.old', 'cdrom',
'lost+found', 'sbin', 'vmlinuz.old', 'windows'])
>>s - p
set(['dev', 'etc', 'vmlinuz', 'srv', 'root', 'initrd.img'])

--
Felipe.
Aug 20 '06 #2
Bucco wrote:
I am trying to compare a list of items to the list of files generated
by os.listdir. I am having trouble getting this to work and think I
may be going down the wrong path. Please let me know if hter is a
better way to do this. THis is what I have for my class so far:

import os, sys

class MatchList:
def __init__(self, dir, file):
self.dir = os.listdir(dir)
self.flist = open(file, 'r').readlines()
self.matches = []
def matcher(self):
#~ matches = []
for fname in self.dir:
#~ print fname
if fname[-4] == '.':
for item in self.flist:
if item == fname[:-4]:
pass
else:
self.matches.append(fname)
#~ else:
#~ break
#~ if self.matches == '':
#~ print "Sorry, there was no match."
#~ else:
#~ for item in matches:
#~ print item
def matchedFiles(self):
for item in self.matches: print item

if __name__ == '__main__':
dir = sys.argv[1]
file = sys.argv[2]
list = open(file, 'r').readlines()
test = MatchList(dir, file)
test.matcher()
test.matchedFiles()
Thanks in advance for your help.

:)
SA
0) What kind of trouble?

1) Don't use "dir", "file", and "list" as variable names, those are
already python built in objects (the dir() function, list type, and
file type, respectively.)

2) 'r' is the default for open(), omit it. "self.flist =
open(file).readlines()"

3) The output of file.readlines() includes the newline at the end of
each line. Use the rstrip() method of strings if this is a problem for
you:

lines = [line.rstrip() for line in open(file).readlines()]

4) This is bizarre:
if item == fname[:-4]:
pass
else:
self.matches.append(fname)
why not "if item != fname[:-4]: self.matches.append(fname)"? But,
note, you're appending fname to self.matches once for every item in
self.flist that it does not match. You might want to put a break
statement in there:

if item != fname[:-4]:
self.matches.append(fname)
break

But see the next point:

5) Since you have a list of things you're matching (excluding actually)
this part:
for item in self.flist:
if item == fname[:-4]:
pass
else:
self.matches.append(fname)
could become:

if fname[:-4] not in self.flist: self.matches.append(fname)

6) Why are you using #~ for comments?

Also, check out os.path.splitext()
http://docs.python.org/lib/module-os.path.html#l2h-1761

HTH,
~Simon

Aug 20 '06 #3

Simon Forman wrote:
1) Don't use "dir", "file", and "list" as variable names, those are
already python built in objects (the dir() function, list type, and
file type, respectively.)
Thanks. My own stupidity on this one.

2) 'r' is the default for open(), omit it. "self.flist =
open(file).readlines()"
Could you clarify? Show an example. Sorry if I sound newbyish, but I
am.

5) Since you have a list of things you're matching (excluding actually)
this part:
for item in self.flist:
if item == fname[:-4]:
pass
else:
self.matches.append(fname)

could become:

if fname[:-4] not in self.flist: self.matches.append(fname)
Thanks. This was what I was looking for.

6) Why are you using #~ for comments?
This is just the automatic commenting on scite. I put my cursor on the
line I want to comment and press ctrl-q ans scite comments out the
whole line or selection. As for why the lines are commented; I
commented these lines so I could test out some of the code prior to
running the next bit.
Also, check out os.path.splitext()
http://docs.python.org/lib/module-os.path.html#l2h-1761
I will check this out also.

Thank You for your help. You have answered some questions for me.

Thanks:)

SA

Aug 20 '06 #4
Bucco wrote:
Simon Forman wrote:
1) Don't use "dir", "file", and "list" as variable names, those are
already python built in objects (the dir() function, list type, and
file type, respectively.)

Thanks. My own stupidity on this one.

2) 'r' is the default for open(), omit it. "self.flist =
open(file).readlines()"

Could you clarify? Show an example. Sorry if I sound newbyish, but I
am.
Sure, no problem. "open(filename, 'r')" is exactly the same as
"open(filename)". The flag argument is optional and when you leave it
off it defaults to 'r'.
>
5) Since you have a list of things you're matching (excluding actually)
this part:
for item in self.flist:
if item == fname[:-4]:
pass
else:
self.matches.append(fname)
could become:

if fname[:-4] not in self.flist: self.matches.append(fname)

Thanks. This was what I was looking for.
Cool! You're welcome. :-)
>
6) Why are you using #~ for comments?

This is just the automatic commenting on scite. I put my cursor on the
line I want to comment and press ctrl-q ans scite comments out the
whole line or selection. As for why the lines are commented; I
commented these lines so I could test out some of the code prior to
running the next bit.
Ah, right on. I was just wondering.
>
Also, check out os.path.splitext()
http://docs.python.org/lib/module-os.path.html#l2h-1761
I will check this out also.

Thank You for your help. You have answered some questions for me.

Thanks:)

SA
You're very welcome. It's a pleasure.

Peace,
~Simon

Aug 25 '06 #5

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

Similar topics

0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS...
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.,...
0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS...
8
by: Rajesh | last post by:
Question :An element in a sorted list can be found in O(log n) time via binary search. But suppose I rotate the sorted list at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might...
3
by: ASP Developer | last post by:
I have a generic list of objects. These objects have a text field that contains a letter for the first character and numbers for the next three. For example, "A001". I need to sort these by...
1
by: anandatkfri | last post by:
Hi, i just want to compare 2 listboxes. The comparison should be like this..that if the item present in the first list box is not present in the second list box, the item must be removed from...
7
by: Abhishek | last post by:
Hi I am using a generic list in my program to have a list of objects stored. I want to compare all the objects in this list with object of another class and find all the objects which meet the...
3
by: tshad | last post by:
I have dataGrid that I am filling from a List Collection and need to sort it by the various columns. So I need to be able to sort the Collection and found that you have to set up your own...
8
by: Mike Copeland | last post by:
I am trying to learn/use the STL <listto implement a small application. I didn't get very far before I got a compile error that befuddles me. Here's the code: struct GENCHECK ...
3
by: Mike Copeland | last post by:
I am trying to learn and use STL lists, and I'm getting a compile error with initial declarations. Here;s the code I have so far: struct GENCHECK // Gender Check...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.