为什么只打印6个字段而不打印7个

2021-09-17 00:22:42 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用下面的代码获取第7条记录?你知道吗

from pprint import pprint
def date_op(total_input_list):
    total_pages=36
    list=[]
    for xy in total_input_list:
        for i in range(1,len(xy)):
            Start_page=xy[i]["start_page"]
            End_page=xy[i+1]["start_page"]-1
            Date = xy[i]["date"]
            list.append(Date)
            date=set(list)
            d1 = [{i: {"start_page": Start_page, "end_page": End_page,"date":date}}]
            print(d1)

total_input_list=[{1:{"start_page":1,"end_page":None,"date":"10/12/2015"},
              2:{"start_page":4,"end_page":None,"date":"12/12/2015"},
              3:{"start_page":8,"end_page":None,"date":"22/1/2016"},
              4:{"start_page":15,"end_page":None,"date":"22/1/2016"},
              5:{"start_page":22,"end_page":None,"date":"22/1/2016"},
              6:{"start_page":24,"end_page":None,"date":"20/12/2016"},
              7:{"start_page":28,"end_page":None,"date":"26/2/2017"}}]
date_op(total_input_list)

我看到了这个输出

[{1: {'date': {'10/12/2015'}, 'end_page': 3, 'start_page': 1}}]
[{2: {'date': {'10/12/2015', '12/12/2015'}, 'end_page': 7, 'start_page': 4}}]
[{3: {'date': {'22/1/2016', '10/12/2015', '12/12/2015'}, 'end_page': 14, 'start_page': 8}}]
[{4: {'date': {'22/1/2016', '10/12/2015', '12/12/2015'}, 'end_page': 21, 'start_page': 15}}]
[{5: {'date': {'22/1/2016', '10/12/2015', '12/12/2015'}, 'end_page': 23, 'start_page': 22}}]
[{6: {'date': {'22/1/2016', '10/12/2015', '12/12/2015', '20/12/2016'}, 'end_page': 27, 'start_page': 24}}]
3条回答
网友
1楼 ·

因为迭代器在len(xy)之前停止

def date_op(total_input_list):
total_pages=36
list=[]
for xy in total_input_list:
    for i in range(1,len(xy)+1):
        Start_page=xy[i]["start_page"]
        if(i<len(xy)):
            End_page=xy[i+1]["start_page"]-1
        Date = xy[i]["date"]
        list.append(Date)
        date=set(list)
        d1 = [{i: {"start_page": Start_page, "end_page": End_page,"date":date}}]
        print(d1)

total_input_list=[{1:{"start_page":1,"end_page":None,"date":"10/12/2015"},
          2:{"start_page":4,"end_page":None,"date":"12/12/2015"},
          3:{"start_page":8,"end_page":None,"date":"22/1/2016"},
          4:{"start_page":15,"end_page":None,"date":"22/1/2016"},
          5:{"start_page":22,"end_page":None,"date":"22/1/2016"},
          6:{"start_page":24,"end_page":None,"date":"20/12/2016"},
          7:{"start_page":28,"end_page":None,"date":"26/2/2017"}}]
date_op(total_input_list)
网友
2楼 ·

因为您要从下一个字段中获取一个值,所以必须将下一个值从位置7设置为null,我假设最后一部分的结束页将为null,否则您所要做的就是更改except部分。你知道吗

这就解决了问题:

for i in range(1,len(xy)+1):
    Start_page=xy[i]["start_page"]
    try: 
      End_page=xy[i+1]["start_page"]-1 #from 1 to 6, the value comes from here
    except:
      End_page=None # when on field 7, value will come from here
    Date = xy[i]["date"]
    list.append(Date)
    date=set(list)
    d1 = [{i: {"start_page": Start_page, "end_page": End_page,"date":date}}]
    print(d1)
网友
3楼 ·

这看起来像一个经典的off-by-one error。你知道吗

enter image description here

7个时间戳定义6个持续时间。你知道吗

要输出第一行:

[{1: {'date': {'10/12/2015'}, 'end_page': 3, 'start_page': 1}}]

您需要来自dict的第一个和第二个值的信息

相关问题