我有一系列jpgs是网站的照片。对于某些网站,我有一张以上的照片。我为每个站点都有唯一的ID。每张照片都有一个名称,如'IDPhoto1','IDPhoto2'等。我想将一个列表中的唯一ID和另一个列表中的文件的完整路径隔离,然后从两个列表中生成一个字典,其中唯一的ID是密钥和文件名中包含相同ID的完整路径将是值。我可以生成列表,但不确定如何创建词典。
我正在更新这个以包含我到目前为止的代码,这并不多。我试过下面有人建议创建字典,但我没有得到与正确的键相关联的正确值,以及某些键应该有多个值的事实。如何生成匹配具有相同ID的值和键的字典?
更新:我想我越来越近了我编辑了下面的代码。现在我得到了与正确值相关联的正确键,但是每个键都有多个值:
import os, PyPDF2, re
pgs_dir = r'P:\Records\GIS\Projects\D04_OHS\OverheadSignStructurePics'
jpg_paths = []
jpg_ID = []
pattern = re.compile(r'Photo.*')
for dirpath, dirnames, filenames in os.walk(jpgs_dir):
for filename in filenames:
fullPath = dirpath+'\\'+filename
if re.search(pattern,filename):
jpg_paths.append(fullPath)
file_ID = re.sub(pattern,'',filename)
jpg_ID.append(file_ID)
jpg_ID_unique = set(jpg_ID)
for j in jpg_ID:
for jp in jpg_paths:
if j in jp:
if j not in pdf_dict.keys():
pdf_dict[j] = jp
else:
pdf_dict.update({j:jp})
print pdf_dict
最终编辑:
为了更好地关注这个问题,我编辑了原帖,以便更清楚地提出一个问题。我也能够生成一些对我有用的代码,它发布在下面:
import os, re
jpgs_dir = r'D:\Records\GIS\Projects\D04_OHS\OverheadSignStructurePics\jpgs_reduced'
jpg_paths = []
jpg_ID = []
pattern = re.compile(r'Photo.*')
pdf_dict = {}
pdf_file_path = r'D:\Records\GIS\Projects\D04_OHS\OverheadSignStructurePics\TIFS'
convert_txt_file = r'D:\ Records \ GIS \ Projects \ D04_OHS \ OverheadSignStructurePics \ Convert_JPGS_Batch.txt'
for dirpath, dirnames, filenames in os.walk(jpgs_dir):
for filename in filenames:
fullPath = dirpath+'\\'+filename
if re.search(pattern,filename):
jpg_paths.append(fullPath)
file_ID = re.sub(pattern,'',filename)
jpg_ID.append(file_ID)
jpg_ID_unique = set(jpg_ID)
print 'total number of unique IDs =',len(jpg_ID_unique)
for j in jpg_ID_unique:
for jp in jpg_paths:
if j in jp:
pdf_dict.setdefault(j, []).append(jp)
print pdf_dict
此处还有一个结果字典键和值的示例:
{'35101023189S1':['D:\ Records \ GIS \ Projects \ D04_OHS \ OverheadSignStructurePics \ jpgs_reduced \ 35101023189S1Photo1.jpg','D:\ Records \ GIS \ Projects \ D04_OHS \ OverheadSignStructurePics \ jpgs_reduced \ 35101023189S1Photo2.jpg' ,'D:\ Records \ GIS \ Projects \ D04_OHS \ OverheadSignStructurePics \ jpgs_reduced \ 35101023189S1Photo3.jpg','D:\ Records \ GIS \ Projects \ D04_OHS \ OverheadSignStructurePics \ jpgs_reduced \ 35101023189S1Photo4.jpg']
答案 0 :(得分:0)
>>> list_of_unique_ids = ['IDPhoto1', 'IDPhoto2', 'IDPhoto3']
>>> list_of_corresponding_paths = [r'Path/To/Photo1', r'Path/To/Photo2', r'Path/To/Photo3']
>>> dictionary = dict(zip(list_of_unique_ids, list_of_corresponding_paths))
>>> print dictionary
{'IDPhoto3': 'Path/To/Photo3', 'IDPhoto2': 'Path/To/Photo2', 'IDPhoto1': 'Path/To/Photo1'}