Python 在我的案例中,我应该使用什么词典或列表?

Python 在我的案例中,我应该使用什么词典或列表?,python,list,dictionary,Python,List,Dictionary,我有一个文本文件,它存储图像路径及其标签路径,例如 /JPEGImages/1.jpg /Class/1_label.png /JPEGImages/2.jpg /Class/2_label.png /JPEGImages/3.jpg /Class/3_label.png /JPEGImages/4.jpg /Class/4_label.png ... /JPEGImages/10000.jpg /Class/10000_label.png 在我的任务中,我将读取文本文件并存储在字典/列表/数

我有一个文本文件,它存储图像路径及其标签路径,例如

/JPEGImages/1.jpg /Class/1_label.png
/JPEGImages/2.jpg /Class/2_label.png
/JPEGImages/3.jpg /Class/3_label.png
/JPEGImages/4.jpg /Class/4_label.png
...
/JPEGImages/10000.jpg /Class/10000_label.png
在我的任务中,我将读取文本文件并存储在字典/列表/数组中,以便在单个循环中轻松访问

for i in range 10001
   print ('image path', ...[i])
   print ('label path', ...[i])
我应该使用什么类型的数据?这是我的当前代码,但它只保存最后一条路径

with open('files.txt', 'r') as f:
    for line in f:
        line = line.strip()
        img, gt = line.split()
        img_path = data_dir + img
        gt_path =  data_dir + gt
        img_gt_path_dict["img_path"]=img_path
        img_gt_path_dict["gt_path"] = gt_path
for i in range (10001):
  print (img_gt_path_dict["img_path"][i])
  print (img_gt_path_dict["gt_path"][i])

您可以使用元组列表来存储数据

Ex:

res = []
with open('files.txt', 'r') as f:
    for line in f:
        line = line.strip()
        img, gt = line.split()
        img_path = data_dir + img
        gt_path =  data_dir + gt
        res.append((img_path, gt_path))

for i,v in res:
  print("Image Path {0}".format(i))
  print("label  Path {0}".format(v))
使用字典(根据评论中的要求)


您可以使用元组列表来存储数据

Ex:

res = []
with open('files.txt', 'r') as f:
    for line in f:
        line = line.strip()
        img, gt = line.split()
        img_path = data_dir + img
        gt_path =  data_dir + gt
        res.append((img_path, gt_path))

for i,v in res:
  print("Image Path {0}".format(i))
  print("label  Path {0}".format(v))
使用字典(根据评论中的要求)

而不是:

img_gt_path_dict["img_path"]=img_path
img_gt_path_dict["gt_path"] = gt_path
这样使用:

img_gt_path_dict[img_path]=img_path
img_gt_path_dict[gt_path] = gt_path
因为在dict中不能复制密钥,所以它只存储最后一个值。 因此,您可以使用list代替dict,或者将完整的image_path和gt_path存储在一个列表中,然后输入dict。 或 像这样

img_path_list = []
gt_path_list = []
with open('files.txt', 'r') as f:
for line in f:
    line = line.strip()
    img, gt = line.split()
    img_path = data_dir + img
    gt_path =  data_dir + gt
    img_path_list.append(img_path)
    gt_path_list.append(gt_path)
img_gt_path_dict["img_path"]=img_path_list
img_gt_path_dict["gt_path"] = gt_path_list
print (img_gt_path_dict["img_path"])
print (img_gt_path_dict["gt_path"])
而不是:

img_gt_path_dict["img_path"]=img_path
img_gt_path_dict["gt_path"] = gt_path
这样使用:

img_gt_path_dict[img_path]=img_path
img_gt_path_dict[gt_path] = gt_path
因为在dict中不能复制密钥,所以它只存储最后一个值。 因此,您可以使用list代替dict,或者将完整的image_path和gt_path存储在一个列表中,然后输入dict。 或 像这样

img_path_list = []
gt_path_list = []
with open('files.txt', 'r') as f:
for line in f:
    line = line.strip()
    img, gt = line.split()
    img_path = data_dir + img
    gt_path =  data_dir + gt
    img_path_list.append(img_path)
    gt_path_list.append(gt_path)
img_gt_path_dict["img_path"]=img_path_list
img_gt_path_dict["gt_path"] = gt_path_list
print (img_gt_path_dict["img_path"])
print (img_gt_path_dict["gt_path"])

简单的解决方案是创建一个列表字典,即:

paths = {}
paths['img_path'] = []
paths['gt_path'] = []
现在按如下方式修改代码

paths = {}
paths['img_path'] = []
paths['gt_path'] = []
with open('file', 'r') as f:        
    for line in f:
        line = line.strip()
        img, gt = line.split()
        paths['img_path'].append(img)
        paths['gt_path'].append(gt)
    l=len(paths['gt_path']) #length of the list
    for i in range(l):
        print "img_path: "+paths['img_path'][i]
        print "gt_path: "+paths['gt_path'][i]

简单的解决方案是创建一个列表字典,即:

paths = {}
paths['img_path'] = []
paths['gt_path'] = []
现在按如下方式修改代码

paths = {}
paths['img_path'] = []
paths['gt_path'] = []
with open('file', 'r') as f:        
    for line in f:
        line = line.strip()
        img, gt = line.split()
        paths['img_path'].append(img)
        paths['gt_path'].append(gt)
    l=len(paths['gt_path']) #length of the list
    for i in range(l):
        print "img_path: "+paths['img_path'][i]
        print "gt_path: "+paths['gt_path'][i]

您正在使用for
i,v
,但我只想
for i
。有更好的方法吗?如果你只使用“for i”,那么你会得到两个值的元组(图像和标签)。使用字典怎么样?因为
i
值由其他条件确定。例如,我们只对范围内的i使用
(开始索引,结束索引)
如果您可以使用数字作为键,那么您可以使用字典。谢谢,您可以更新您的答案使用字典或列表,以便我可以对范围内的i使用循环作为
(开始索引,结束索引)
您使用的是
i,v
,但我只想为我。有更好的方法吗?如果你只使用“for i”,那么你会得到两个值的元组(图像和标签)。使用字典怎么样?因为
i
值由其他条件确定。例如,对于范围内的i(开始索引,结束索引)如果您可以使用数字作为键,那么您可以使用字典。谢谢,您是否可以更新您的答案使用字典或列表,以便我可以使用循环作为范围内的i(开始索引,结束索引),而不是dict list?实际上,你的dict元素的问题。只能使用一个键存储一个值。因此,可以尝试使用list或将键更改为value而不是dict list吗?实际上,你的dict元素的问题。只能使用一个键存储一个值。因此,请尝试使用列表或将键更改为值。您可以使用两个列表。我的期望是一个列表,因为我必须洗牌列表。如果我使用两个列表作为你的解决方案,那么我洗牌一个列表,另一个列表将不会洗牌为相同的索引当有另一个解决方案是我提到的第一个时,键也必须给出相同的img_路径或键,你可以使用升序数字你使用两个列表。我的期望是一个列表,因为我必须洗牌列表。如果我使用两个列表作为你的解决方案,那么我洗牌一个列表,另一个列表将不会洗牌为相同的索引当有另一个解决方案是我提到的第一个时,键也必须给出相同的img_路径或键,你可以使用升序作为数字