背景
我正在通过python处理一些restjson调用,以获取用户列表。不幸的是,服务器一次最多只能返回50个用户。 e、 g
call“[0:49]”返回前50个用户
call“[51:99]”返回下一批50个用户
服务器确实返回了用户总数,因此,我可以编写一些逻辑来获取具有多个rest调用的所有用户。你知道吗
问题
我编写了一个非常混乱的函数,它以字典格式打印数据:
def get_all_possible_items(max_no_users):
dict={}
base=0
values=max_no_users//50
if max_no_users>49:
dict[base]=base+49
base+=49
else:
base-=1
for i in range(values-1):
base+=1
dict[base]=base+49
base+=49
dict[base+1]=max_no_users
print dict
get_all_possible_items(350)
输出如下所示:
如果最大无用户数为350:
{0: 49, 100: 149, 200: 249, 300: 349, 50: 99, 150: 199, 250: 299, 350: 350}
如果max\u no\u users是351:
{0: 49, 100: 149, 200: 249, 300: 349, 50: 99, 150: 199, 250: 299, 350: 351}
有没有更好的写作方法(一定有!)?你知道吗
您只需使用python的
range(start, stop, step)
。您可以将step
设置为50。伊恩然后可能有一个额外的
if
语句用于最后一个数字,以确保它不超过最大值,即编辑:要特别说明如何使用它,请执行以下操作:
或者如果你想把所有的都放在一行:
试试这个::你的结果不正确。。。在你的问题上
相关问题 更多 >
编程相关推荐