1

数据为:[('1985-08', '15.00'), ('1985-08', '14.88'), ('1985-08', '15.25'), ('1985-08', '15.25 '), ('1985-08', '15.13'), ('1985-08', '14.75'), ('1985-08', '14.88'), ('1985-08', '15.25') , ('1985-08', '15.25'), ('1985-08', '15.00'), ('1985-08', '14.63'), ('1985-08', '14.50'), ( '1985-08', '14.63'), ('1985-08', '15.25'), ('1985-08', '15.00'), ('1985-08', '15.25'), ('1985 -08', '15.13'), ('1985-08', '14.88'), ('1985-08', '15.25'), ('1985-08', '15.38'), ('1985-08' ', '15.75'), ('1985-08', '15.88'), ('1985-07', '15.88'), ('1985-07', '16.25'), ('1985-07', '16.00'), ('1985-07', '16.62'), ('1985- 07', '16.62'), ('1985-07', '16.25'), ('1985-07', '16.50'), ('1985-07', '16.87'), ('1985-07' , '17.37'), ('1985-07', '17.25'), ('1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', ' 17.75'), ('1985-07', '17.87'), ('1985-07', '18.00'), ('1985-07', '18.00'), ('1985-07', '17.62' ), ('1985-07', '17.62'), ('1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', '17.25'), ('1985-07', '18.12')]('1985-07', '16.25'), ('1985-07', '16.00'), ('1985-07', '16.62'), ('1985-07', '16.62'), (' 1985-07', '16.25'), ('1985-07', '16.50'), ('1985-07', '16.87'), ('1985-07', '17.37'), ('1985- 07', '17.25'), ('1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', '17.75'), ('1985-07' , '17.87'), ('1985-07', '18.00'), ('1985-07', '18.00'), ('1985-07', '17.62'), ('1985-07', ' 17.62'), ('1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', '17.25'), ('1985-07', '18.12' )]('1985-07', '16.25'), ('1985-07', '16.00'), ('1985-07', '16.62'), ('1985-07', '16.62'), (' 1985-07', '16.25'), ('1985-07', '16.50'), ('1985-07', '16.87'), ('1985-07', '17.37'), ('1985- 07', '17.25'), ('1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', '17.75'), ('1985-07' , '17.87'), ('1985-07', '18.00'), ('1985-07', '18.00'), ('1985-07', '17.62'), ('1985-07', ' 17.62'), ('1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', '17.25'), ('1985-07', '18.12' )]16.00'), ('1985-07', '16.62'), ('1985-07', '16.62'), ('1985-07', '16.25'), ('1985-07', '16.50' ), ('1985-07', '16.87'), ('1985-07', '17.37'), ('1985-07', '17.25'), ('1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', '17.75'), ('1985-07', '17.87'), ('1985-07', '18.00'), (' 1985-07', '18.00'), ('1985-07', '17.62'), ('1985-07', '17.62'), ('1985-07', '17.62'), ('1985- 07', '17.50'), ('1985-07', '17.25'), ('1985-07', '18.12')]16.00'), ('1985-07', '16.62'), ('1985-07', '16.62'), ('1985-07', '16.25'), ('1985-07', '16.50' ), ('1985-07', '16.87'), ('1985-07', '17.37'), ('1985-07', '17.25'), ('1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', '17.75'), ('1985-07', '17.87'), ('1985-07', '18.00'), (' 1985-07', '18.00'), ('1985-07', '17.62'), ('1985-07', '17.62'), ('1985-07', '17.62'), ('1985- 07', '17.50'), ('1985-07', '17.25'), ('1985-07', '18.12')]('1985-07', '16.50'), ('1985-07', '16.87'), ('1985-07', '17.37'), ('1985-07', '17.25'), (' 1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', '17.75'), ('1985-07', '17.87'), ('1985- 07', '18.00'), ('1985-07', '18.00'), ('1985-07', '17.62'), ('1985-07', '17.62'), ('1985-07' , '17.62'), ('1985-07', '17.50'), ('1985-07', '17.25'), ('1985-07', '18.12')]('1985-07', '16.50'), ('1985-07', '16.87'), ('1985-07', '17.37'), ('1985-07', '17.25'), (' 1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', '17.75'), ('1985-07', '17.87'), ('1985- 07', '18.00'), ('1985-07', '18.00'), ('1985-07', '17.62'), ('1985-07', '17.62'), ('1985-07' , '17.62'), ('1985-07', '17.50'), ('1985-07', '17.25'), ('1985-07', '18.12')]17.75'), ('1985-07', '17.87'), ('1985-07', '18.00'), ('1985-07', '18.00'), ('1985-07', '17.62' ), ('1985-07', '17.62'), ('1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', '17.25'), ('1985-07', '18.12')]17.75'), ('1985-07', '17.87'), ('1985-07', '18.00'), ('1985-07', '18.00'), ('1985-07', '17.62' ), ('1985-07', '17.62'), ('1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', '17.25'), ('1985-07', '18.12')]

我的代码:

def average_data(list_of_tuples):
strMonth = []
counter = 0
addition = 0
while True:
    if (strMonth[counter])[0] == (strMonth[counter+1])[0]:    #line 31
        addition += float(strMonth[counter][1])
        result2 = addition
    else:
        continue
print(result2)
return result2
def main(): 
    file_obj = get_input_descriptor()
    column = int(input('What column:'))
    get_data_list(file_obj, column)
    result2 =get_data_list(file_obj, column)

average_data(result2)
file_obj.close()
main()

Python Shell 的错误代码:

  File "C:\Users\admin\Desktop\proj.py", line 31, in average_data
if (strMonth[counter])[0] == (strMonth[counter+1])[0]:
IndexError: list index out of range

它有什么问题?我想得到每个月的总数。有没有其他方法可以解决这个问题?

4

2 回答 2

2

strMonth是空的。它没有第 0 个元素,因此strMonth[counter]引发IndexError

In [29]: strMonth = []

In [30]: counter = 0

In [31]: strMonth[counter]
IndexError: list index out of range

您可以使用 a dict(或collections.defaultdict)将 yearmonth 字符串映射到表示值总和的浮点数。

import collections
def average_data(strMonth):
    result = collections.defaultdict(float)
    for yearmonth, val in strMonth:
        result[yearmonth] += float(val)
    return dict(result)

def main(): 
    result2 = [('1985-08', '15.00'), ('1985-08', '14.88'), ('1985-08', '15.25'), ('1985-08', '15.25'), ('1985-08', '15.13'), ('1985-08', '14.75'), ('1985-08', '14.88'), ('1985-08', '15.25'), ('1985-08', '15.25'), ('1985-08', '15.00'), ('1985-08', '14.63'), ('1985-08', '14.50'), ('1985-08', '14.63'), ('1985-08', '15.25'), ('1985-08', '15.00'), ('1985-08', '15.25'), ('1985-08', '15.13'), ('1985-08', '14.88'), ('1985-08', '15.25'), ('1985-08', '15.38'), ('1985-08', '15.75'), ('1985-08', '15.88'), ('1985-07', '15.88'), ('1985-07', '16.25'), ('1985-07', '16.00'), ('1985-07', '16.62'), ('1985-07', '16.62'), ('1985-07', '16.25'), ('1985-07', '16.50'), ('1985-07', '16.87'), ('1985-07', '17.37'), ('1985-07', '17.25'), ('1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', '17.75'), ('1985-07', '17.87'), ('1985-07', '18.00'), ('1985-07', '18.00'), ('1985-07', '17.62'), ('1985-07', '17.62'), ('1985-07', '17.62'), ('1985-07', '17.50'), ('1985-07', '17.25'), ('1985-07', '18.12')]
    print(average_data(result2))
main()

产量

{'1985-07': 378.08000000000004, '1985-08': 332.16999999999996}
于 2013-02-26T01:39:19.043 回答
0

蟒蛇3.2

  M=List_of_tuples

  sum(int(m.split("-")[1]) for m,t in M)
于 2013-02-26T02:39:15.770 回答