[英]Python Comparing two lists of strings for similarities
我是Python的新手,但我认为制作一个程序来排序我的所有下载会很有趣,但我遇到了一些麻烦。 如果我的目的地只有一个单词,但如果目的地有两个或更多单词,那么它就会完美地运行,这就是它出错的地方,程序陷入了循环。 有没有人比我更好地比较列表
>>>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]']
这是列表输出的示例。
我有一个目标目录,其中只包含文件夹和下载目录。 我想创建一个程序来自动查看源文件名,然后查看目标名称。 如果目标名称在源名称中,那么我将继续并复制下载的文件,以便在我的集合中对其进行排序。
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
现在我很高兴只有“成功”作为输出; 我将弄清楚如何复制文件,因为在不久的将来,这将是一个完全不同的问题
也许这样的事情。 检查目标文件夹中的每个单词是否都存在于文件名中
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
我个人最喜欢的python模糊字符串比较是模糊的。它有很多很好的例子和非常自由的许可证。
一些可能与您相关的示例:
> 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
通过这个简单的脚本,你可以将文件从源码移动到命运。
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))
看来你正在寻找什么。 您只需要检查文件名是否不在目的地列表中并移动它。 它对你有用吗?
从之前的回答中发现, re.sub
是解决问题的可能方法。 替换此块:
# ...
import re
source = os.listdir(src)
destination = os.listdir(dst)
通过
source = [re.sub(' ', '\\\\ ',w)for w in os.listdir(src)]
destination = [re.sub(' ', '\\\\ ', w) for w in os.listdir(dst)]
它可以移动名称之间带空格的文件夹。
我认为你应该寻找正则表达式,而不是比较字符串来处理特殊字符。 我试图使用这样的东西(应用于源和目的地),但没有减少。
#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])
在这个链接上有一个类似问题的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.