簡體   English   中英

如何使用另一個列表迭代嵌套列表以創建列表字典Python

[英]How to iterate nested lists with another list to create a dictionary of lists Python

我正在使用Mibian模塊來計算看漲期權。 我有三個嵌套列表的列表。 每個嵌套的列表代表行使價。 每個嵌套列表都有各自的有效期限,即my_list [2]還有30天。

     import mibian as mb
     import pandas as pd

     my_list = [[20, 25, 30, 35, 40, 45], 
       [50, 52, 54, 56, 58, 60, 77, 98, 101],
       [30, 40, 50, 60]]

     days_left = [5, 12, 30]

     my_list[2]
     [30, 40, 50, 60]

     days_left[2]
     30

Mibian Black-Scholes代碼的結構,用於計算看漲期權。

    mb.BS([stock price, strike price, interest rate, days to maturity], volatility)

     data1 = dict()
     for x, sublist in enumerate(my_list):
         data1[x] = option3 = []
         for i in sublist:
             c = mb.BS([120, i, 1, 20], 10)
             option3.append(c.callPrice)

給出具有3個列表的字典的輸出,調用價格基於my_list中三個嵌套列表的每一個。

        data1

         {0: [100.01095590221843,
              95.013694877773034,
              90.016433853327641,
              85.019172828882233,
              80.021911804436854,
              75.024650779991447],
         1: [70.027389755546068,
             68.028485345767905,
             66.029580935989742,
             64.030676526211579,
             62.03177211643343,
             60.032867706655267,
             43.042180223540925,
             22.05368392087027,
             19.055327306203068],
         2: [90.016433853327641,
             80.021911804436854,
             70.027389755546068,
             60.032867706655267]}

我想要得到的是將嵌套列表和日期一起迭代

我想用字典創建與上面相同的內容,但是它不僅按順序迭代my_list,而且還按天迭代了days_left。

我通過new_list = list(zip(days_left,my_list))嘗試了一個zip列表,但它給了我一個錯誤。 誰能幫忙嗎? 非常感謝。

    new_list = list(zip(my_list, days_left))

    [([20, 25, 30, 35, 40, 45], 5),
     ([50, 52, 54, 56, 58, 60, 77, 98, 101], 12),
     ([30, 40, 50, 60], 30)]

    data5 = dict()
    for x, days_left, my_list in enumerate(new_list):
             data5[x] = option5 = []
             for days_left, my_list in new_list:
                           c = mb.BS([120, my_list, 1, days_left ], 10)
                           option5.append(c.callPrice)

對於單個嵌套列表,例如my_list [2]。 輸出為:

    range_list = list(range(1))

data2 = dict()
for x in range_list:
data2[x] = option2 = []

for i in my_list[2]:

    c = mb.BS([120, i, 1, 30  ], 10)

    option2.append(c.callPrice)

option2


[90.024647403788975,
 80.032863205051967,
 70.041079006314973,
 60.049294807577965]

這些值相似,但與data1 [2]中的值不同。 理想的輸出應具有與data1相同的結構,並帶有三個字典,但由於days_left的關系,其值略有不同。 差異看似微不足道,但稍后,我必須將它們乘以100,以便累積這些差異。

我認為這可以滿足您的需求。 請注意,大多數操作都試圖模擬您的環境-您只關心最后幾行。

也就是說,由序號索引的數據結構不應該是字典,而應該是列表。 ;-)

Magic_numbers = [
    100.01095590221843,
    95.013694877773034,
    90.016433853327641,
    85.019172828882233,
    80.021911804436854,
    75.024650779991447,
    70.027389755546068,
    68.028485345767905,
    66.029580935989742,
    64.030676526211579,
    62.03177211643343,
    60.032867706655267,
    43.042180223540925,
    22.05368392087027,
    19.055327306203068,
    90.016433853327641,
    80.021911804436854,
    70.027389755546068,
    60.032867706655267,
]

Magic_index = 0

def mb(details, volatility):
    class C:
        def __init__(self, n):
            self.callPrice = n

    global Magic_index
    result = C(Magic_numbers[Magic_index])
    Magic_index += 1
    return result

mb.BS = mb

strike_prices = [
    [20, 25, 30, 35, 40, 45],
    [50, 52, 54, 56, 58, 60, 77, 98, 101],
    [30, 40, 50, 60]
]

days_left = [5, 12, 30]

data99 = {}  # This is silly. A dict indexed by sequential numbers should be a list.

for i, (days, prices) in enumerate(zip(days_left, strike_prices)):
    data99[i] = [mb.BS([120, price, 1, days], 10).callPrice for price in prices]

import pprint
pprint.pprint(data99)

輸出看起來像這樣:

{0: [100.01095590221843,
     95.01369487777303,
     90.01643385332764,
     85.01917282888223,
     80.02191180443685,
     75.02465077999145],
 1: [70.02738975554607,
     68.0284853457679,
     66.02958093598974,
     64.03067652621158,
     62.03177211643343,
     60.03286770665527,
     43.042180223540925,
     22.05368392087027,
     19.05532730620307],
 2: [90.01643385332764,
     80.02191180443685,
     70.02738975554607,
     60.03286770665527]}

我認為答案可能很簡單:

for x, (days_left, my_list) in enumerate(new_list):
     data5[x] = option5 = []
     for days_left, my_list in new_list:
          c = mb.BS([120, my_list, 1, days_left ], 10)
                    option5.append(c.callPrice)

因為enumerate的輸出將采用(i, x)的形式,在這種情況下, x是一個元組(即(i, (x, y)) )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM