字典被“附加”并随机打印

时间:2018-05-09 21:59:46

标签: python arrays list dictionary

好的,在我问之前,我已经看到了以下答案:

Python OrderedDict not keeping element order

Converting dict to OrderedDict

OrderedDict does not preserve the order

但我仍然无法做对。

背景:我有一个groundtruth文件,比如jp1_GT.txt(还有其他文件,在不同的路径,但目前我只关心一个文件)在某个路径上。在此文件中,每一行代表格式中的一个边界框。我正在改变一些计算,并打算将它们写在一个新的groundtruth文件中。

#Path containing old folders
src_path = r"C:\Users\username\Downloads\LITIV_dataset"

#dictionary creation
newgtdict ={}

for folderName in os.listdir(src_path):
  folderPath = src_path + "\\" + folderName

  if not os.path.isdir(folderPath):
    continue

  listOfFilePaths = glob.glob(folderPath + "\\"+ folderName +"_GT.txt")
  for filePath in listOfFilePaths:

    openedFile = open(filePath, 'r')
    lines = openedFile.readlines()
    linecount =0

    for line in lines:
        linecount+=1
        linenumber = str(linecount)
        my_list = str(line).split(",")
        bboxes=[]

        #checking for non-empty lines
        if len(my_list)>0:
            x_br = int(my_list[2])
            y_br = int(my_list[3])
            x_tl = int(my_list[0]) - (x_br/2)
            y_tl = int(my_list[1]) - (y_br/2)
            box = [x_tl,y_tl,x_br,y_br]
        newgtdict[str(filePath)+str(linenumber)] = box 
        print newgtdict

我的jp1_GT.txt的前四行如下所示:

189,163,72,72
190,162,72,72
188,160,72,72
189,163,72,72

但是,前四行输出如下所示:

{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt1': [153, 127, 72, 72]} 
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt1': [153, 127, 72, 72], 'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt2': [154, 126, 72, 72]} 
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt1': [153, 127, 72, 72], 'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt3': [152, 124, 72, 72], 'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt2': [154, 126, 72, 72]}
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt4': [153, 127, 72, 72], 'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt1': [153, 127, 72, 72], 'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt3': [152, 124, 72, 72], 'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt2': [154, 126, 72, 72]}

预计前4行输出:

{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt1': [153, 127, 72, 72]} 
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt2': [154, 126, 72, 72]} 
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt3': [152, 124, 72, 72]} 
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt4': [153, 127, 72, 72]} 

只是一个附带问题:你是否建议采用其他更好的方法。

1 个答案:

答案 0 :(得分:3)

您在每次迭代时打印字典,而不是在每次迭代时将新项目打印到字典

你可以这样做:

for line in lines:
    # <snip>
    newgtdict[str(filePath)+str(linenumber)] = box 
    print box

for line in lines:
    # <snip>
    newgtdict[str(filePath)+str(linenumber)] = box 
    # don't print here

for line in newgtdict:
    print(line)

请注意您没有使用OrderedDict,您应该使用OrderedDict(如果您希望在早于3.7的Python版本中订购结果)

newgtdict = collections.OrderedDict()
# rather than = {}