简体   繁体   English

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

[英]List returned from function not containing correct results

I have the following function which extracts non-sequential files names associated with the index items: 我具有以下函数,该函数提取与索引项关联的非顺序文件名:

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

Here is how I call it 这是我的称呼

results = check_increment(plot_info)

print results

This is the result: 结果如下:

['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']

When I run the code stand alone it works: 当我独立运行代码时,它可以工作:

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

with this result: 结果如下:

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

Any ideas? 有任何想法吗?

OK, found the source of the problem and @zwer was in the right place. 好,找到问题的根源,@ zwer在正确的位置。 When called from by full script the Increment_value argument was coming in as a string. 当由完整脚本调用时,Increment_value参数作为字符串进入。 So to fix the problem I modified the following line to convert it to an int 因此,为解决此问题,我修改了以下行以将其转换为int

increment_value = int(args.increment)

It worked in the standalone because it was declared as an int from the start. 它可以独立运行,因为从一开始就将其声明为int。

Thanks for all you help on this! 多谢您的协助!

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

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