2015-05-17 22 views
0

我在我最初使用平分在相当长的脚本这是它的一部分(工作完全正常,并为意):为什么在无限循环上这个自定义平分函数? 。

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 
+0

“sharpes”和“sharpe”是什么类型? –

+0

请创建一个显示问题的[MCVE](http://stackoverflow.com/help/mcve)。由于我们完全不知道“sharpe”,“sharpe”,“portfolio”或“add_sharpe”是什么,我们无法为您提供帮助。 – MattDMo

+0

@BurhanKhalid补充道,我的歉意...... – thefoxrocks

回答

2

我认为你是插入到你迭代的列表。例如:

a = [1] 

for x in a: 
    a.insert(0, x) 
    print a 

这将让你在一个循环中,你一直插入到1a

+0

是特定的,我使用'.insert()',但是。你有任何想法如何简单地添加一次? – thefoxrocks

+0

我无法从你的代码中得知任何变量来自哪里,以及你为什么要迭代组合。您发布的代码是嵌套在另一个循环中的吗? – DTing

+0

我找到了解决方案。我不得不创建一个新的列表,而不是将值插入到我正在迭代的同一个列表中,而是将它插入到新列表中。谢谢你的帮助! – thefoxrocks