从函数返回的列表不包含正确的结果

时间:2017-12-17 09:25:05

标签: python python-2.7 list dictionary

我有以下功能,它提取与索引项关联的非顺序文件名:

check_increment(plot_inf):

    lists = sorted(plot_inf.items()) # sorted by key, return a list of tuples
    x, y = zip(*lists) # unpack a list of pairs into two tuples

    bad_steps =[]

    for item in range(len(x)-1):

        diff = (y[item+1])-(y[item])

        if diff <> increment_value:
            bad_steps.append((x[item]))
    return bad_steps

以下是我的称呼方式

results = check_increment(plot_info)

print results

结果如下:

['image_D2017-06-26T17-02-01-994022Z_2.jpg', 'image_D2017-06-26T17-02-02-
327353Z_0.jpg', 'image_D2017-06-26T17-02-02-660684Z_1.jpg', 'image_D2017-06-
26T17-02-02-994007Z_2.jpg', 'image_D2017-06-26T17-02-03-327330Z_0.jpg', 
'image_D2017-06-26T17-02-03-660654Z_1.jpg', 'image_D2017-06-26T17-02-03-
993973Z_2.jpg', 'image_D2017-06-26T17-02-04-327305Z_0.jpg', 'image_D2017-06-
26T17-02-04-660625Z_1.jpg']

当我单独运行代码时,它可以工作:

plot_dict = {'image_D2017-06-26T17-02-03-327330Z_0.jpg': 6231, 'image_D2017-
06-26T17-02-02-660684Z_1.jpg': 6229, 'image_D2017-06-26T17-02-02-
327353Z_0.jpg': 622, 'image_D2017-06-26T17-02-04-660625Z_1.jpg': 6235, 
'image_D2017-06-26T17-02-03-993973Z_2.jpg': 6233, 'image_D2017-06-26T17-02-
02-994007Z_2.jpg': 6230, 'image_D2017-06-26T17-02-01-994022Z_2.jpg': 6227, 
'image_D2017-06-26T17-02-04-993951Z_2.jpg': 6236, 'image_D2017-06-26T17-02-
03-660654Z_1.jpg': 6232, 'image_D2017-06-26T17-02-04-327305Z_0.jpg': 6234}

 increment_value = 1

 lists = sorted(plot_dict.items()) # sorted by key, return a list of tuples
 x, y = zip(*lists) # unpack a list of pairs into two tuples

bad_steps =[]

for item in range(len(x)-1):

  diff = (y[item+1])-(y[item])

  if diff <> increment_value:
      bad_steps.append((x[item]))

  print bad_steps

有了这个结果:

['image_D2017-06-26T17-02-01-994022Z_2.jpg', 'image_D2017-06-26T17-02-02-327353Z_0.jpg']

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好的,找到了问题的根源,@ zwer在正确的位置。当从完整脚本调用时,Increment_value参数作为字符串进入。因此,为了解决这个问题,我修改了以下行以将其转换为int

increment_value = int(args.increment)

它在独立工作,因为它从一开始就被声明为int。

感谢您的帮助!