繁体   English   中英

如何从某些路径的最后一部分的组合中获取字符串?

[英]How I get the string from the combination of the last part of some paths?

首先,我有这个元组列表:

paths = [('/User/folder/fileA.png', '/User/folder/fileB.png'), ('/User/folder/fileC.png', '/User/folder/fileD.png'), ('/User/folder/fileE.png', '/User/folder/fileF.png') ]

我的目标是得到一个 function 可以给我组合文件的字符串:

print(combinations)

fileA_fileB
fileC_fileD
fileE_fileF

我试图用 pathlib 模块修复它,但我无法遍历列表。 有任何想法吗? 非常感谢您提前

您可以将它们转换为Path对象,然后使用Path.stem获取名称。 您当然可以按照自己的意愿连接它们,在您的情况下添加下划线。 类似f'{Path(i).stem}_{Path(j).stem}'

from pathlib import Path

paths = [('/User/folder/fileA.png', '/User/folder/fileB.png'), ('/User/folder/fileC.png', '/User/folder/fileD.png'), ('/User/folder/fileE.png', '/User/folder/fileF.png') ]
res = [Path(i).stem + Path(j).stem for i, j in paths]

for i in res:
    print(i)

Output

fileAfileB
fileCfileD
fileEfileF

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM