0

First I'll show my code and then I'll explain:

for eachfile in matches:
    if eachfile in set1:
        print 'find one match'
        shutil.copy2(eachfile, '/Users/r2-d2/Desktop/folder')
        print 'copy done'

matches is a list like this:

[fooFILE_Aspam.txt, barFILE_B.CSV, codeFILE_Cdog.vcf]

set1 is a set like which looks like this:

(FILE_A, FILE_B, FILE_F...)

I'm iterating over the list to check if inside each entry there's any of the elements of the set. If it does I copy the entry away. In the above example scenario I expect a match for FILE_A and FILE_B. I know that the keyword in would work the other way around but I can't find an elegant solution without using another loop.

1
  • Are these strings inside the list/set? Commented May 12, 2016 at 14:08

6 Answers 6

1

Set1 is not a set (like you stated) but a tuple, so you need to cast and then you can use a set intersection to filter duplicates across both

 matching_files = list(set(matches).intersection(set(set1)))

 for file in matching_files:
     # Do your copy
     pass
Sign up to request clarification or add additional context in comments.

Comments

0

what about intersection between sets?and then you copy the result?Not really sure though if you are looking for a math in the string of if the elements in the list are the "same" as the one in the set

Comments

0

There are many ways to do this.

import re
pat = re.compile('|'.join(set1))
for match in matches:
    if pat.search(match):
        # do stuff

But I would probably introduce the filter with the help of regex (or possibly fnmatch) while calculating matches

Comments

0

use any to check if any matches eachfile.

matches = ['fooFILE_Aspam.txt', 'barFILE_B.CSV', 'codeFILE_Cdog.vcf']
set1 = ('FILE_A', 'FILE_B', 'FILE_F')

for eachfile in matches:
    if any([x in eachfile for x in set1]):
        print eachfile

Comments

0

First, in operator applies to both set and str. In the former case, you are checking for exact string match; in the second, for contiguous substring inclusion. I assume you are after the latter, so this should work.

matches = ['fooFILE_Aspam.txt', 'barFILE_B.CSV', 'codeFILE_Cdog.vcf']
set1 = {'FILE_A', 'FILE_B', 'FILE_F'}

to_move = [fname for fname in matches if any(t in fname for t in set1)]

# In [39]: q.to_move
# Out[39]: ['fooFILE_Aspam.txt', 'barFILE_B.CSV']

Comments

0

You can use this:

for eachfile in [x for x in matches if x in set1]:
    print 'find one match'
    shutil.copy2(eachfile, '/Users/r2-d2/Desktop/folder')
    print 'copy done'

Or, do it inside the for loop - list:

def shCopy(file,dest='/Users/r2-d2/Desktop/folder'):
    print 'find one match'
    shutil.copy2(file, dest)
    print 'copy done'

[shCopy(x) for x in matches if x in set1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.