I have printed the contents of 2 folders to separate txt files. It is in the form of a python list How could I loop through the files looking for matching list elements? My first problem is how to extract the words from out of the quotes and the second should come easily since I would just have to find that same pattern in the 2nd txt file. Any ideas?

You can use eval

I've heard only bad things about eval + strings.
Maybe I'm better off not printing them to files?

I need to be able to search out of order in the lists to compare them

for item in List1:
search List2 for item
if match is found
next item
else print item from list1 in file.txt

So, if anyone could help me with this pseudocode.. I haven't found any operators that let me easily compare contents of lists or a search list function

To check if an item exists in a list, just do:

if item in List1:
   # item found, add code here
else:
   # item not found, add code here

I've heard only bad things about eval + strings.
Maybe I'm better off not printing them to files?

I need to be able to search out of order in the lists to compare them

for item in List1:
search List2 for item
if match is found
next item
else print item from list1 in file.txt

So, if anyone could help me with this pseudocode.. I haven't found any operators that let me easily compare contents of lists or a search list function

The only 'bad' thing about eval is that you must not eval a string received from the internet, because it could contain arbitrary code which you don't want to execute on your computer.

But there is nothing wrong in evaluating a string written by your own program. It's like running your own script.

Edited 6 Years Ago by Gribouillis: n/a

Comments
straighted my knowledge of eval out

Do I need to convert the 2 lists to strings before I can search each? Because I used list.count('the') and it returned 0.. Which there should be almost 300 cases of 'the' in this.

I have this at the moment:

import os

# Define variables for the 2 lists
mu1List = os.listdir('M:\Music')
mu2List = os.listdir('H:\Music')
mu1List.sort()
mu2List.sort()

def compare():    # cycle through items in list
                  # find each item from list1 in list2
                  # if item isn't found, print item to screen

    for item in mu1List:
        mu2List.find(item)
    else:
        print item
        
compare()

Edited 6 Years Ago by planetPlosion: added the code

You can use set

x = set(mu1List)
y = set(mu2List)

a = x - y # set of items in x but not in y (set difference)
b = x & y # set of items in both sets (set intersection)
c = y - x # set of items in y but not in x

example

>>> x = set(range(10))
>>> y = set(range(4, 12))
>>> x
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> y
set([4, 5, 6, 7, 8, 9, 10, 11])
>>> x - y
set([0, 1, 2, 3])
>>> x & y
set([4, 5, 6, 7, 8, 9])
>>> y - x
set([10, 11])
>>> x | y
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

Edited 6 Years Ago by Gribouillis: n/a

Comments
perfect
This question has already been answered. Start a new discussion instead.