Manuel T
Manuel T

Reputation: 17

Match elements of lists by partly similar strings| Python 3.x

I've got two lists that contain filenames from different directories.

The goal is to sync the filenames from the target directory to the filenames in the source directory. Since the filenames contain partly the same string, it should be possible to reference them.


Directory 1: "random-original.ext" | Directory 2: "original.ext"

Sync with skript:

Directory 1: "random-original.ext" | Directory 2: "random-original.ext"


The "S03-ST02..." in here is just an example of random strings. The only thing that will be the same in both files is the the last part before and including the file-extension. The filenames below are just examples - it should work with all kind of names afterwards.

Input:

sourceFiles  = [S03-ST02-T04-VID004.mov, S01-ST01-T01-AV0203.abc, S05-ST02-T01-MOV0014.mp4]
targetFiles  = [AV0203.abc, MOV0014.mp4, VID0004.mov]

Output Var 1

sourceFiles = [S01-ST01-T01-AV0203.abc, S05-ST02-T01-MOV0014.mp4, S03-ST02-T04-VID0004.mov]
targetFiles  = [AV0203.abc, MOV0014.mp4, VID0004.mov]

So I can reference the same file (with different names) for example with a for loop:

for i in len(sourceFiles):
   sourceFiles[i]
   targetFiles[i]

Output Var 2

combFileList = [(S01-ST01-T01-AV0203.abc, AV0203.abc), (S05-ST02-T01-MOV0014.mp4, MOV0014.mp4), (S03-ST02-T04-VID0004.mov, VID0004.mov)]

And reference the same file for example like this:

for i in len(combFileList):
   combFileList[i][0]
   combFileList[i][1]
   ...

How would I approach this?

Upvotes: 0

Views: 51

Answers (2)

gmoshkin
gmoshkin

Reputation: 1135

Something like this

for targetFile in targetFiles:
    for sourceFile in sourceFiles:
        if sourceFile.endswith(targetFile):
            do_something(sourceFile, targetFile)

Upvotes: 0

Ma0
Ma0

Reputation: 15204

You could use next here inside a list-comprehension

sourceFiles  = ['S03-ST02-T04-VID0004.mov', 'S01-ST01-T01-AV0203.abc', 'S05-ST02-T01-MOV0014.mp4']
targetFiles  = ['AV0203.abc', 'MOV0014.mp4', 'VID0004.mov']

res = [next((x for x in targetFiles if y.endswith(x)), 'N/A') for y in sourceFiles]

which produces

print(res)  # -> ['VID0004.mov', 'AV0203.abc', 'MOV0014.mp4']

Upvotes: 1

Related Questions