Python Comparing Two String Lists for Similarities

I am very new to Python, but I thought it would be interesting to make a program to sort all my downloads, but I have a little problem with it. It works great if my addressee has only one word, but if the addressee has two or more words, this happens incorrectly and the program gets stuck in a loop. Does anyone have an idea to compare lists than me.

>>>for i in dstdir:
>>>    print i.split()

['CALIFORNICATION']
['THAT', "'70S", 'SHOW']
['THE', 'BIG', 'BANG', 'THEORY']
['THE', 'OFFICE']
['DEXTER']
['SPAWN']
['SCRUBS']
['BETTER', 'OF', 'TED']

>>>for i in dstdir:
>>>    print i.split()
['Brooklyn.Nine-Nine.S01E16.REAL.HDTV.x264-EXCELLENCE.mp4']
['Revolution', '2012', 'S02E12', 'HDTV', 'x264-LOL[ettv]']]
['Inequality', 'for', 'All', '(2013)', '[1080p]']

This is an example of listing output.

I have a destination directory with only folders in it and a download directory. I want the program to automatically look up the name of the source file, and then look at the name of the recipient. if the recipient name is in the source name, then I have yes to continue and copy the downloaded file so that it is sorted in my collection.

destination = '/media/mediacenter/SAMSUNG/SERIES/'
source = '/home/mediacenter/Downloads/'
dstdir = os.listdir(destination)
srcdir = os.listdir(source)

for i in srcdir:
    source = list(i.split())
    for j in dstdir:
        count = 0
        succes = 0
        destination = list(j.split())
        if len(destination) == 1:
            while (count < len(source)):
                if destination[0].upper() == source[count].upper():
                    print 'succes ', destination, ' ', source
                count = count + 1
        elif len(destination) == 2:
            while(count < len(source)):
                if (destination[0].upper() == source[count].upper()):
                    succes = succes + 1
                    count = len(source)
            count = 0
            while(count < len(source)):
                if (destination[1].upper() == source[count].upper()):
                    succes = succes + 1
                    count = len(source)
            count = 0
            if succes == 2:
                print 'succes ', destination, ' ', source

"" ; , , .

+4
4

- , . ,

dstdir = ['The Big Bang Theory', 'Dexter', 'Spawn' ]

srcdir = ['the.big.bang.theory s1e1', 'the.big.bang.theory s1e2', 'dexter s2e01']

for source in srcdir:
    for destination in dstdir:
        destinationWords = destination.split()

        if all(word.lower() in source.lower() for word in destinationWords):
            print 'succes ', destination, ' ', source

:

succes  The Big Bang Theory   the.big.bang.theory s1e1
succes  The Big Bang Theory   the.big.bang.theory s1e2
succes  Dexter   dexter s2e01
+1

python fuzzywuzzy .

, :

> choices = ["Atlanta Falcons", "New York Jets", "New York Giants", "Dallas Cowboys"]
> process.extract("new york jets", choices, limit=2)
  [('New York Jets', 100), ('New York Giants', 78)]
> process.extractOne("cowboys", choices)
  ("Dallas Cowboys", 90)

token_sort_ratio .

> fuzz.ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")
  90
> fuzz.token_sort_ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")
  100
+2

script sugested , .

src = "/home/mediacenter/Downloads"
dst = "/media/mediacenter/SAMSUNG/SERIES"
source =  os.listdir(src)
destination = os.listdir(dst)

for filename in source:

    file_src = src +"/"+ str(filename)
    file_dst = dst +"/"+ str(filename)

    if filename not in destination and os.path.isdir(file_src) is False:
        #download file
        os.system("mv %s %s" %(file_src, file_dst))
    elif filename not in destination and os.path.isdir(file_src) is True:
        #download directory
        os.system("mv %s %s" %(file_src, dst))

, . , . ?

0

A re.subpossible solution to the problem was found from the previous answer . Replace this block:

# ...
import re

source =  os.listdir(src)
destination = os.listdir(dst)

By

source =  [re.sub(' ', '\\\\ ',w)for w in os.listdir(src)]
destination = [re.sub(' ', '\\\\ ', w) for w in os.listdir(dst)]

This is a trick for moving folders with spaces between names.

Instead of comparing strings to handle special characters, I think you should look for regular expressions . I tried to use something like this (in relation to the source and destination), but was not successful.

#snippet of code doesnt work, just to illustrate 

pattern = "[a-zA-Z0-9]"
for i,w in enumerate(source):
    for ch in w:

        if not re.match(pattern, ch) :
            print source , ch

            source[i] = re.sub( ch,r"\\" + ch, source[i])

This link is a matter of close concern.

0
source

Source: https://habr.com/ru/post/1527092/


All Articles