为什么这个自定义平分函数会无限循环?
Why is this custom bisect function on endless loop?
我有一个很长的脚本,我最初在其中使用了 bisect。这是其中的一部分(工作得非常好并且符合预期):
portfolios = [[1], [0.9], [0.8], [0.7], [0.6]] #Fills up list to avoid "index out of range" error later on in code
add_sharpe = [sharpe, name_a, weight_a, exchange_a, name_b, weight_b, exchange_b, name_c, weight_c, exchange_c]
for x in portfolios:
if sharpe > x[0]:
sharpes = [i[0] for i in portfolios]
if name_a not in x and name_b not in x and name_c not in x:
print sharpe
print sharpes
print portfolios
print add_sharpe
position = reverse_bisect(sharpes, sharpe)
print position
portfolios.insert(position, add_sharpe)
但是,现在我需要反向平分(降序)。谢天谢地,我找到了 a really good solution to this.
创建反向平分的代码如下:
def reverse_bisect(a, x, lo=0, hi=None):
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if x > a[mid]: hi = mid
else: lo = mid+1
return lo
当我用简单的计算测试它时,它工作得很好。但是,当我将它插入我的脚本时,它会导致脚本在 运行 时冻结在该点。我不知道为什么会这样,因为我使用与以前相同的逻辑 bisect.bisect
,效果非常好。
这是现在不起作用的:
if name_a not in x and name_b not in x and name_c not in x:
position = reverse_bisect(sharpes, sharpe)
portfolios.insert(position, add_sharpe)
出于某种原因,使用该函数似乎无休止地循环 portfolios.insert(position, add_sharpe)
。
输出:
[1, 0.9, 0.8, 0.7, 0.6] #print portfolios
[[1], [0.9], [0.8], [0.7], [0.6]] #print sharpes
[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '] #print portfolios
0 #print position
1.62759369021 #print sharpe
[1.6275936902107178, 1, 0.9, 0.8, 0.7, 0.6] #print portfolios
[[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '], [1], [0.9], [0.8], [0.7], [0.6]]
[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE ']
1
1.62759369021
[1.6275936902107178, 1.6275936902107178, 1, 0.9, 0.8, 0.7, 0.6]
[[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '], [1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '], [1], [0.9], [0.8], [0.7], [0.6]]
[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE ']
2
1.62759369021
[1.6275936902107178, 1.6275936902107178, 1.6275936902107178, 1, 0.9, 0.8, 0.7, 0.6]
[[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '], [1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '], [1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '], [1], [0.9], [0.8], [0.7], [0.6]]
[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE ']
3
我认为您正在插入到您正在迭代的列表中。例如:
a = [1]
for x in a:
a.insert(0, x)
print a
这会让你进入一个循环,在这个循环中你不断地插入 1
到 a
。
我有一个很长的脚本,我最初在其中使用了 bisect。这是其中的一部分(工作得非常好并且符合预期):
portfolios = [[1], [0.9], [0.8], [0.7], [0.6]] #Fills up list to avoid "index out of range" error later on in code
add_sharpe = [sharpe, name_a, weight_a, exchange_a, name_b, weight_b, exchange_b, name_c, weight_c, exchange_c]
for x in portfolios:
if sharpe > x[0]:
sharpes = [i[0] for i in portfolios]
if name_a not in x and name_b not in x and name_c not in x:
print sharpe
print sharpes
print portfolios
print add_sharpe
position = reverse_bisect(sharpes, sharpe)
print position
portfolios.insert(position, add_sharpe)
但是,现在我需要反向平分(降序)。谢天谢地,我找到了 a really good solution to this.
创建反向平分的代码如下:
def reverse_bisect(a, x, lo=0, hi=None):
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if x > a[mid]: hi = mid
else: lo = mid+1
return lo
当我用简单的计算测试它时,它工作得很好。但是,当我将它插入我的脚本时,它会导致脚本在 运行 时冻结在该点。我不知道为什么会这样,因为我使用与以前相同的逻辑 bisect.bisect
,效果非常好。
这是现在不起作用的:
if name_a not in x and name_b not in x and name_c not in x:
position = reverse_bisect(sharpes, sharpe)
portfolios.insert(position, add_sharpe)
出于某种原因,使用该函数似乎无休止地循环 portfolios.insert(position, add_sharpe)
。
输出:
[1, 0.9, 0.8, 0.7, 0.6] #print portfolios
[[1], [0.9], [0.8], [0.7], [0.6]] #print sharpes
[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '] #print portfolios
0 #print position
1.62759369021 #print sharpe
[1.6275936902107178, 1, 0.9, 0.8, 0.7, 0.6] #print portfolios
[[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '], [1], [0.9], [0.8], [0.7], [0.6]]
[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE ']
1
1.62759369021
[1.6275936902107178, 1.6275936902107178, 1, 0.9, 0.8, 0.7, 0.6]
[[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '], [1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '], [1], [0.9], [0.8], [0.7], [0.6]]
[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE ']
2
1.62759369021
[1.6275936902107178, 1.6275936902107178, 1.6275936902107178, 1, 0.9, 0.8, 0.7, 0.6]
[[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '], [1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '], [1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE '], [1], [0.9], [0.8], [0.7], [0.6]]
[1.6275936902107178, "CARR'S GROUP (CARR.L)", 0.9, 'LSE ', 'Church & Dwight Co. Inc. (CHD)', 0.05, 'NYSE ', 'NCC Group plc. (NCC.L)', 0.05, 'LSE ']
3
我认为您正在插入到您正在迭代的列表中。例如:
a = [1]
for x in a:
a.insert(0, x)
print a
这会让你进入一个循环,在这个循环中你不断地插入 1
到 a
。