从元组中获取相同的元素并返回两个索引列表

时间:2017-11-18 12:01:43

标签: python list indexing

我有下面的列表(实际上它更长但是只是提出这个想法):

[[('P', 0.3178082191780822, 1750.0, 12.5),
  ('C', 0.8191780821917808, 1800.0, 332.80000000000001),
  ('P', 0.3178082191780822, 1325.0, 1.95),
  ('P', 0.14520547945205478, 1550.0, 1.0),
  ('C', 1.8136986301369864, 1900.0, 305.56999999999999),
  ('P', 0.3178082191780822, 1700.0, 9.9000000000000004),
  ('P', 0.14520547945205478, 2010.0, 18.949999999999999)]]

每个元组引用的内容(option_type,time_to_maturity,strike,option_price)。

我必须在time_to_maturity和strikes上执行双重积分,因此我会为每个不同的time_to_maturity(每个元组的第二个元素)选择相应的strike值(每个元组的第三个元素)。我将获得的是一个包含到期时间的列表和另一个包含罢工元组的列表,这些列表对应于单个到期时间(到期时间与不同的罢工相关,但通常相反的情况不成立)。有没有办法做到这一点?

修改 这是我将删除带有'P'的元组的10个列表中的一个,它具有与'C'相同的连续元组的打击:

(0.8328767123287671,[('P',1200.0,7.75),('P',1300.0,11.199999999999999),('P',1400.0,15.5),('P',1500.0,21.600000000000001),('C ',1500.0,590.14999999999998),('P',1550.0,24.75),('P',1575.0,26.0),('C',1575.0,522.0),('P',1600.0,29.100000000000001),('P ',1650.0,33.5),('P',1675.0,35.899999999999999),('P',1700.0,39.700000000000003),('P',1725.0,42.600000000000001),('P',1800.0,53.0),('P ',1850.0,62.100000000000001),('P',1875.0,67.5),('P',1900.0,72.700000000000003),('C',1900.0,243.09999999999999),('P',1950.0,84.900000000000006),('C ',1975.0,189.30000000000001),('P',2000.0,98.0),('C',2000.0,171.0),('C',2050.0,139.09999999999999),('C',2075.0,122.59999999999999),('P ',2075.0,126.0),('C',2100.0,108.0),('P',2100.0,133.0),('C',2150.0,81.400000000000006),('C',2200.0,57.700000000000003),('C ',2250.0,39.0),('P',2250.0,217.59999999999999),('C',2300.0,24.350000000000001),('P',2300.0,253.40000000000001),('C',2350 .0,14.35),('C',2375.0,11.0),('C',2400.0,8.0),('C',2500.0,2.5499999999999998),('P',2500.0,427.85000000000002)])

2 个答案:

答案 0 :(得分:1)

您可以使用列表推导来提取维度:

time_to_maturity_list = [time_to_maturity for option_type, time_to_maturity, strike, option_price in my_list]
strikes_list = [strike for option_type, time_to_maturity, strike, option_price in my_list]

这是非常易读的,但它确实意味着在列表上循环两次。另一种方法是创建两个列表,并在执行正常循环时附加项目:

time_to_maturity_list = []
strike_list = []
for option_type, time_to_maturity, strike, option_price in my_list:
    time_to_maturity_list.append(time_to_maturity)
    strike_list.append(strike)

注意:my_list只是一个列表[tuple,tuple]或者获取数据的第一个元素,或者做一些事情来连接所有列表(比如嵌套的for循环)

答案 1 :(得分:1)

如果我理解正确,您希望按time_to_maturity对记录进行分组,那么为什么不使用itertools.groupby?这需要你进行排序,但是为了能够整合你必须排序,所以我想这没关系。

import itertools as it
import operator as op

data, = [[('P', 0.3178082191780822, 1750.0, 12.5),
          ('C', 0.8191780821917808, 1800.0, 332.80000000000001),
          ('P', 0.3178082191780822, 1325.0, 1.95),
          ('P', 0.14520547945205478, 1550.0, 1.0),
          ('C', 1.8136986301369864, 1900.0, 305.56999999999999),
          ('P', 0.3178082191780822, 1700.0, 9.9000000000000004),
          ('P', 0.14520547945205478, 2010.0, 18.949999999999999)]]

# sort records ignoring 0th column
ds = sorted(data, key=op.itemgetter(slice(1, None)))
# group by 1st column
gr = it.groupby(ds, op.itemgetter(1))
# cut the first two entries from each record in each group
# the 1st entry is redundant with key, and the 0th I don't know what
# it's good for. To retain it use vi[:1] + vi[2:] instead of just vi[2:]
gr = [(k, [vi[2:] for vi in v]) for k, v in gr]
print(gr)

打印:

[(0.14520547945205478, [(1550.0, 1.0), (2010.0, 18.95)]), (0.3178082191780822, [(1325.0, 1.95), (1700.0, 9.9), (1750.0, 12.5)]), (0.8191780821917808, [(1800.0, 332.8)]), (1.8136986301369864, [(1900.0, 305.57)])]

请注意,正如它所显示的那样,“P'' C'柱。但是,如果您需要保留它,可以很容易地解决这个问题,请参阅代码中的注释。