0

最初に bisect in を使用していた長いスクリプトがあります。これはその一部でした (完全に正常に動作し、意図したとおりに動作しました)。

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)

しかし、今は逆二等分 (降順) が必要でした。ありがたいことに、これに対する本当に良い解決策を見つけました。

逆二等分を作成するコードは次のとおりです。

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
4

1 に答える 1

2

繰り返しているリストに挿入していると思います。例えば:

a = [1]

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

1これにより、 に挿入し続けるループに陥りますa

于 2015-05-17T06:12:29.830 に答える