我有一本字典,imageHashes
有keys=paths
和values = integers
,例如
imageHashes['/directorya/jim.txt'] = 7
imageHashes['/directorya/nigel.txt'] = 68
imageHashes['/directoryb/ralph.txt'] = 17
imageHashes['/directoryb/baba.txt'] = 43
使用组合,我可以循环使用:
for keypair in list(combinations(imageHashes,2)):
do something
问题是我只想在不同目录的对之间做一些事情,所以
jim
和ralph
nigel
和ralph
jim
和baba
nigel
和baba
jim
和nigel
ralph
和baba
如果您想要一种通用方法,即拥有任意数量的目录:
现在
comb
将拥有与可用路径中不同目录一样多的元素;也就是说,每个comb
都有一个目录路径无需过度思考:只需使用
continue
遍历组合并跳过您不想处理的组合。你需要知道的另一件事是如何检查两个文件是否在同一个目录中换句话说,它们的目录名是否匹配?这就是os.path.dirname
给我们的它给出:
注意,不需要将
combinations
返回的迭代器转换为列表:这只是浪费时间相关问题 更多 >
编程相关推荐