如何通过 for 循环根据 Python 中的范围创建下限值和上限值列表?
How to create a list of lower and upper bound values based on range in Python via for loop?
我有最大范围 (x),我需要创建增量并生成从前一个值增加 1 的值列表。
硬编码场景示例:
x = 50000
max_val = 16384
id1 = pd_ID.loc[0:16384,]
id2 = pd_ID.loc[16385:32768]
id3 = pd_ID.loc[32769:49152]
id4 = pd_ID.loc[49152:50000]
这个练习的目标是以更自动化的方法复制上面的例子,因为 x 改变了我正在处理的每个帐户。
以下是我当前的方法,但您会看到我在下限变量中的第一个值从 1 而不是 0 开始(我需要它从第一行 [0] 开始)。
import math
x = 50000
increment = math.ceil(x/16384)
print("increment",increment)
for i in range(increment):
print("i=",i)
upper_bound = (16384 * (i+1)) if i < increment-1 else x
lower_bound = upper_bound - 16384 + 1 if i < increment-1 else ((16384 * (i))) + 1
print(lower_bound)
print(upper_bound)
#####[1:16384]
#####[16385:32768]
#####[32769:49152]
#####[49152:50000]
期望的输出
#####[0:16384]
#####[16385:32768]
#####[32769:49152]
#####[49152:50000]
您可以根据需要修改它以匹配您希望定义范围开始和结束的方式。在您的示例输出中,它不一致。
def create_range(upper_lim: int, range_size: int):
current = 0
output = []
while current < upper_lim:
if current + range_size > upper_lim:
output += [range(current , upper_lim)]
else:
output += [range(current, (current + range_size - 1))]
current += range_size
return output
create_range(50000, 16384)
[range(0, 16383),
range(16384, 32767),
range(32768, 49151),
range(49152, 50000)]
您可以在这些线路上尝试一些东西。
max_ = 50000
step = 16384
lower_bound = 0
upper_bound = 0
i = 0
while upper_bound < max_:
if i != 0:
lower_bound = upper_bound + 1
upper_bound = lower_bound + step - 1
else:
upper_bound = lower_bound + step
if upper_bound > max_:
upper_bound = max_
print("lower", lower_bound)
print("upper", upper_bound)
print()
i += 1
lower 0
upper 16384
lower 16385
upper 32768
lower 32769
upper 49152
lower 49153
upper 50000
我有最大范围 (x),我需要创建增量并生成从前一个值增加 1 的值列表。
硬编码场景示例:
x = 50000
max_val = 16384
id1 = pd_ID.loc[0:16384,]
id2 = pd_ID.loc[16385:32768]
id3 = pd_ID.loc[32769:49152]
id4 = pd_ID.loc[49152:50000]
这个练习的目标是以更自动化的方法复制上面的例子,因为 x 改变了我正在处理的每个帐户。
以下是我当前的方法,但您会看到我在下限变量中的第一个值从 1 而不是 0 开始(我需要它从第一行 [0] 开始)。
import math
x = 50000
increment = math.ceil(x/16384)
print("increment",increment)
for i in range(increment):
print("i=",i)
upper_bound = (16384 * (i+1)) if i < increment-1 else x
lower_bound = upper_bound - 16384 + 1 if i < increment-1 else ((16384 * (i))) + 1
print(lower_bound)
print(upper_bound)
#####[1:16384]
#####[16385:32768]
#####[32769:49152]
#####[49152:50000]
期望的输出
#####[0:16384]
#####[16385:32768]
#####[32769:49152]
#####[49152:50000]
您可以根据需要修改它以匹配您希望定义范围开始和结束的方式。在您的示例输出中,它不一致。
def create_range(upper_lim: int, range_size: int):
current = 0
output = []
while current < upper_lim:
if current + range_size > upper_lim:
output += [range(current , upper_lim)]
else:
output += [range(current, (current + range_size - 1))]
current += range_size
return output
create_range(50000, 16384)
[range(0, 16383),
range(16384, 32767),
range(32768, 49151),
range(49152, 50000)]
您可以在这些线路上尝试一些东西。
max_ = 50000
step = 16384
lower_bound = 0
upper_bound = 0
i = 0
while upper_bound < max_:
if i != 0:
lower_bound = upper_bound + 1
upper_bound = lower_bound + step - 1
else:
upper_bound = lower_bound + step
if upper_bound > max_:
upper_bound = max_
print("lower", lower_bound)
print("upper", upper_bound)
print()
i += 1
lower 0
upper 16384
lower 16385
upper 32768
lower 32769
upper 49152
lower 49153
upper 50000