嵌套for循环使用List Comprehension进行多个操作

时间:2017-03-19 03:27:53

标签: python loops for-loop list-comprehension

只是理解List Comprehensions,就像在最近的一次采访中那样,技术人员问我这个问题,并且作为一个自学者,我回答了lambda,这不是列表理解。

假设我们有时间序列数据" Shiller",http://us.spindices.com/indices/real-estate/sp-corelogic-case-shiller-us-national-home-price-nsa-index

我使用以下循环计算了aic / bic:

shiller = [please use some random data or use the link above]

import matplotlib.pyplot as plt
import statsmodels.api as sm
import pandas as pd

def aicbic(shiller):
  arimaijk = []
  aicijk = []
  bicijk = []
  index = []
  for i in range(1,3):
      for j in range(1,2):
          for k in range(0,5):
              arimaijk.append(sm.tsa.ARIMA(shiller,(i,j,k)).fit())
              index.append([i,j,k])
              aicijk.append(arimaijk[k].aic)            
              bicijk.append(arimaijk[k].bic)
  return aicijk, bicijk

aicbic(shiller)
Out[9]: 
([-235.77314152121426,-233.9375761653174,-233.3841011331017,-241.65994870973782,-240.2975620564456,-235.77314152121426,-233.9375761653174,-233.3841011331017,-241.65994870973782,-240.2975620564456],
 [-227.98778197081049,-223.55709676477906,-220.40850188242874,-226.08922960893028,-222.13172310550345,-227.98778197081049,-223.55709676477906,-220.40850188242874,-226.08922960893028,-222.13172310550345])

现在,我想使用List Comprehension这个结果,所以我写了以下几行,这些行返回错误:

def aicbic(data):    
  arimaijk = []
  aicijk = []
  bicijk = []
  index = []
  [(sm.tsa.ARIMA(data,(i,j,k)).fit(),index.append([i,j,k]),\
  aicijk.append(arimaijk[k].aic),bicijk.append(arimaijk[k].bic)) \
  for i in range(1,3) for j in range(1,2) for k in range(0,5)]

IndexError:列表索引超出范围

3 个答案:

答案 0 :(得分:2)

错误与列表理解本身无关:

[(sm.tsa.ARIMA(data,(i,j,k)).fit(),index.append([i,j,k]),\
  aicijk.append(arimaijk[k].aic),bicijk.append(arimaijk[k].bic)) \
  for i in range(1,3) for j in range(1,2) for k in range(0,5)]

引发错误IndexError: list index out of range是因为您希望访问arimaijk[k],而arimaijk是一个空列表(aicbic(data)函数的第一行是arimaijk=[]) 。

答案 1 :(得分:1)

您的函数使用itertools和list comprehensions重新编写:

<强>代码:

import itertools as it

def aicbic(shiller):
    loop = list(it.product(range(1, 3), range(1, 2), range(0, 5)))
    arimaijk = [sm.tsa.ARIMA(shiller, (i, j, k)).fit() for i, j, k in loop]
    aicijk = [arimaijk[k].aic for i, j, k in loop]
    bicijk = [arimaijk[k].bic for i, j, k in loop]
    return aicijk, bicijk

测试代码:

result = aicbic(shiller)

import numpy as np
assert np.all(np.isclose(result, (
    [-235.77314152121426, -233.9375761653174, -233.3841011331017,
     -241.65994870973782, -240.2975620564456, -235.77314152121426,
     -233.9375761653174, -233.3841011331017, -241.65994870973782,
     -240.2975620564456],
    [-227.98778197081049, -223.55709676477906, -220.40850188242874,
     -226.08922960893028, -222.13172310550345, -227.98778197081049,
     -223.55709676477906, -220.40850188242874, -226.08922960893028,
     -222.13172310550345]
)))

注意:

您可能有某种错误,因为aicijkbicijk仅依赖于k

答案 2 :(得分:0)

感谢Hossein,我想我现在知道如何使用List Comprehensions了!

def aicbic(data):
  arimaijk = []
  aicijk = []
  bicijk = []
  index = []
  [(arimaijk.append(sm.tsa.ARIMA(data,(i,j,k)).fit()),index.append([i,j,k]),\
  aicijk.append(arimaijk[k].aic),bicijk.append(arimaijk[k].bic)) \
  for i in range(1,3) for j in range(1,2) for k in range(0,5)]
  return aicijk, bicijk


result = aicbic(shiller)