简体   繁体   English

如何迭代不相等的嵌套列表来创建一个新的列表Python

[英]How to iterate unequal nested lists to create a new list Python

I'm stuck on iterating several nested lists in order to calculate Call options by using a Python module, Mibian. 我一直在迭代几个嵌套列表,以便使用Python模块Mibian计算Call选项。

If I use mibian to calculate made up European call options. 如果我使用mibian来计算欧洲看涨期权。

import mibian as mb

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


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

For calculating multiple call options, first, I create a range. 为了计算多个呼叫选项,首先,我创建一个范围。 If I select, say the first nested list, my_list[0] , and run a for -loop. 如果我选择,比如第一个嵌套列表my_list[0] ,并运行for -loop。 I get all the call options for the stock. 我得到了股票的所有看涨期权。

range_list = list(range(len(my_list)))
range_list
# [0, 1, 2]

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

    for i in my_list[0]:

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

        option2.append(c.callPrice)

option2

This gives the 6 call prices of the first nested list from my_list. 这给出了my_list中第一个嵌套列表的6个调用价格。

Output: 输出:

 [100.01095590221843,
  95.013694877773034,
  90.016433853327641,
  85.019172828882233,
  80.021911804436854,
  75.024650779991447]

What I'm trying to figure out, is how I can iterate all the nested lists in one go, and get a new list of nested lists that contain the call option prices for my_list[0] , my_list[1] , and my_list[2] . 我想弄清楚的是,我可以一次迭代所有嵌套列表,并获得一个新的嵌套列表列表,其中包含my_list[0]my_list[1]my_list[2]的调用选项价格my_list[2]

I'd like this output in one go for all three nested lists. 对于所有三个嵌套列表,我想一次性输出这个输出。

Output: 输出:

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

Can anyone help? 有人可以帮忙吗? I'm sure it's something very simple that I'm missing. 我确信这很简单,我很想念。 Many thanks. 非常感谢。 PS I can't get the indentation right when editing my code on here. PS我在这里编辑代码时无法正确缩进。

Let's start with your current approach: 让我们从您当前的方法开始:

range_list = list(range(len(my_list)))

data = dict()
for x in range_list:
    data[x] = option2 = []
    for i in my_list[0]:
        c = mb.BS([120, i, 1, 20 ], 10)
        option2.append(c.callPrice)

The first thing you should note is that there is enumerate to get the index and the part at the same time, so you can omit the range_list variable: 你应该注意的第一件事是有enumerate来同时获取索引和部分,所以你可以省略range_list变量:

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

This also takes care of the problem with the "dynamic indexing" because you can just iterate over the sublist : 这也解决了“动态索引”的问题,因为您可以遍历sublist

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

Then you can use a list comprehension to replace the inner loop: 然后你可以使用list comprehension来替换内部循环:

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

and if you feel like you want this shorter (not recommended but some like it) then use a dict comprehension instead of the outer loop: 如果你觉得你想要这个更短(不推荐但有些喜欢它),那么使用dict理解而不是外部循环:

data = {x: [mb.BS([120, i, 1, 20 ], 10).callPrice for i in sublist] 
        for x, sublist in enumerate(my_list)}

provided that 提供的

my_nested_list = [[1,2,3], [4,5,6,7], [8,9]]
[i for i in my_nested_list]

returns 回报

[[1, 2, 3], [4, 5, 6, 7], [8, 9]]

something along 一些东西

my_list = [[20, 25, 30, 35, 40, 45], [50, 52, 54, 56, 58, 60, 77, 98, 101],
      [30, 40, 50, 60]]    
[mb.BS([120, i, 1, 20 ], 10) for i in my_list]

shall return what you expect? 应该回报你的期望?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM