【问题标题】:Print series of number every 50每 50 次打印一系列数字
【发布时间】:2019-10-05 16:54:35
【问题描述】:

我需要通过 API 进行分页,并且正在创建 URL。

网址如下所示: /search/officers?q=XXXXX&items_per_page=50&start_index={}

返回的 JSON 中每页允许的最大项目数为 50,根据我需要更改的页数 start_index={} 字符串。

我通过将总结果数除以每页的最大项目数来计算需要执行的分页数。

pages = 355 
count_by_n = 50

for i in range(pages+1):
    if i is 0:
        print("start_index={}".format(i))
    else:
        global count_by_n 
        count_by_n += 50
        print(str("start_index={}".format(str(count_by_n + 1))))`

产生:

start_index=0
start_index=101
start_index=151
start_index=201
start_index=251
start_index=301
start_index=351
start_index=401

<>:7: SyntaxWarning: name 'count_by_n' is assigned to before global declaration

从技术上讲,这是我想要的结果,但我想知道是否有办法绕过该消息,也许可以通过递归解决这个问题。

【问题讨论】:

  • 为什么以start_index=0开头?此外,51 不见了。
  • 因为第一页是从0到50,第二页是从51到100。是的,我发帖后想的,现在编辑。
  • 但这没有多大意义,要么第一页从1到50(第二页从51到100),要么第一页从0到49,第二页从50到 99。否则两个页面有不同数量的元素。它也与您后来的请求不“和谐”,从那时起您从101 获取到151,因此跳数为 50,而不是 51。
  • 你是对的,它是从 1 到 50 - 51 到 100 等等...

标签: python algorithm loops recursion functional-programming


【解决方案1】:

是的,你可以在一个范围内指定start(包括)、stop(不包括)和step,所以你可以这样写:

pages = 123
count_by_n = 50

for i in range(1, 50*pages + 1, 50):
    print('start_index={}'.format(i))

然后产生:

>>> pages = 355
>>> for i in range(1, 50*pages + 1, 50):
...     print('start_index={}'.format(i))
... 
start_index=1
start_index=51
start_index=101
start_index=151
start_index=201
start_index=251
start_index=301

【讨论】:

    【解决方案2】:

    由于您已经计算了分页,恕我直言,最简单的解决方案是简单地遍历该范围并打印 50*i + 1

    pages = 355
    for i in range(pages):
        print(f'start_index={50*i+1}')
    
    # start_index=1
    # start_index=51                                              
    # start_index=101              
    # ... 
    # start_index=17601
    # start_index=17651                                         
    # start_index=17701                                    
    

    如果你想事先计算页数,因为你想根据url的数量进行循环n,我建议这样做

    n = 17710
    for i in range(0, n, 50):
        print(f'start_index={i+1}')
    

    为了可读性。

    【讨论】:

      【解决方案3】:

      SyntaxWarning 出现是因为 global count_by_n 在 for 循环中被多次评估,更重要的是,在已经为变量分配了一个值之后。为了消除警告,您应该对每个变量只使用一次 global 关键字(因此在 for 循环之外)并且在赋值之前,如下所示:

      pages = 123
      global count_by_n
      count_by_n = 50
      
      for i in range(pages+1):
          if i is 0:
              print("start_index={}".format(i))
          else:
              count_by_n += 50
              print(str("start_index={}".format(str(count_by_n + 1))))
      

      【讨论】:

      • 不,这是错误的。抛出语法错误是因为声明为全局的变量在此声明之前已使用。顺便说一句,这也写在错误消息中:“name 'count_by_n' is assigned to before global declaration”。除此之外,在这种情况下不需要声明任何东西是全局的。测试您自己的代码,无需此行。
      • 我确实测试了这段代码,它在没有警告的情况下工作,因为在分配值之前使用了 global:修复警告的确切问题。在发表评论之前检查你的话。
      • 如果我应该检查我的话 - 你为什么要相应地编辑你的答案?但是 - 在回复之前检查单词是一个很好的建议:请注意,我并不怀疑您检查了您的代码。我让你检查你的代码删除行后global count_by_n。因为这里不需要全局。当然使用它不会导致警告,但这不是使用任何语句的好理由,对吧?
      猜你喜欢
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      相关资源
      最近更新 更多