如何使用另一个列表迭代嵌套列表以创建列表字典 [英] How to iterate nested lists with another list to create a dictionary of lists Python

查看:63
本文介绍了如何使用另一个列表迭代嵌套列表以创建列表字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mibian模块来计算通话选项.我有三个嵌套列表的列表.每个嵌套的列表代表执行价格.每个嵌套列表都有各自的有效期限,即my_list [2]还有30天.

I'm using Mibian module to calculate call options. I have a list of three nested lists. Each nested list represent strike prices. Each nested list has their own respective days left to expiration, i.e. my_list[2] has 30 days left.

     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代码的结构.

The structure of a Mibian Black-Scholes code for calculating the call option.

    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中三个嵌套列表中的每个列表.

The gives an output of a dictionary with 3 lists, the call prices based on each of the three nested lists from 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]}

我想要获取的是嵌套列表和要一起迭代的日期

What I'm trying to get is for both the nested lists and the dates to be iterated together

我想用字典创建与上面相同的内容,但它不仅按顺序迭代my_list,而且还按顺序迭代days_left.

I'd like to create the same as above, with a dictionary, but that iterates not only my_list, but also the days_left in order.

我通过new_list = list(zip(days_left,my_list))尝试了一个zip列表,但它给了我一个错误.谁能帮忙吗?非常感谢.

I tried a zip list by new_list = list(zip(days_left, my_list)), but it gave me an error. Can anyone please help? Many thanks.

    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].输出为:

For a single nested list like my_list[2]. The output is:

    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,以便累积这些差异.

The values are similar, but not the same as those in data1[2]. The ideal output should have the same structure as data1, with three dictionaries, but the values slightly different due to the days_left. The differences may seem trivial, but later, I'll have to multiply them by 100, so those differences build up.

推荐答案

我认为这可以满足您的需求.请注意,这大部分是在尝试模拟您的环境-您只关心最后两行.

I think this does what you want. Please note, most of this is trying to simulate your environment - you only care about the last couple of lines.

也就是说,由序号索引的数据结构不应该是字典,而应该是列表. ;-)

That said, a data structure indexed by sequential numbers shouldn't be a dict, it should be a list. ;-)

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]}

这篇关于如何使用另一个列表迭代嵌套列表以创建列表字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆