我在我最初使用平分在相當長的腳本這是它的一部分(工作完全正常,併爲意):爲什麼在無限循環上這個自定義平分函數? 。
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
「sharpes」和「sharpe」是什麼類型? –
請創建一個顯示問題的[MCVE](http://stackoverflow.com/help/mcve)。由於我們完全不知道「sharpe」,「sharpe」,「portfolio」或「add_sharpe」是什麼,我們無法爲您提供幫助。 – MattDMo
@BurhanKhalid補充道,我的歉意...... – thefoxrocks