Python列表循环到输出平均值 - 返回的记录太多

时间:2016-05-12 06:44:11

标签: python python-3.x

我在下面列出了MyList1

[('128-F,2825', '3'), ('128-F,2854', '4'), ('128-F,2913', '5'), ('128-F,3210', '5'), ('128-F,3301', '4'), ('128-F,4771', '4'), ('128-M,1268', '4'), ('128-M,235', '5'), ('128-M,3367', '5'), ('128-M,4647', '5'), ('128-M,4795', '5'), ('128-M,5627', '3'), ('96-F,2041', '4'), ('96-F,3755', '4'), ('96-F,4946', '5'), ('96-F,5458', '3'), ('96-F,5643', '4'), ('96-F,5812', '2'), ('96-M,1671', '5'), ('96-M,386', '4'), ('96-M,3985', '4'), ('96-M,4169', '5'), ('96-M,5468', '4'), ('96-M,5848', '4'), ('96-M,889', '3')]

这是从字典生成并排序的,

MyList1 = [(k,v) for k,v in MyDict.items()]
MyList1.sort()

我写了新手代码来循环并计算平均值,

rec = " "
for i in range(len(MyList1)):
    #try:
        x = (MyList1[i][0].split(',')[0])
        y = int(MyList1[i][1])
        if rec != x:
            rec = x
            CurrCount = 1
            ThisRating = y
            CurrAvg = y / CurrCount
            #print('1', x, CurrAvg)
        elif rec == x:
            ThisRating = ThisRating + y
            CurrCount += 1
            CurrAvg = ThisRating / CurrCount
        else:
            print(x, 'no rating')

        print(x, CurrAvg)
    #except:
        #pass

我想要的结果是,

128 F   4.166666667
128 M   4.5
96  F   3.666666667
96  M   4.142857143

但我得到了以下内容。该公式正在运行,但Python正在返回每次迭代,而不仅仅是每个组合的最后一条记录。

128-F 3.0
128-F 3.5
128-F 4.0
128-F 4.25
128-F 4.2
128-F 4.166666666666667
128-M 4.0
128-M 4.5
128-M 4.666666666666667
128-M 4.75
128-M 4.8
128-M 4.5
96-F 4.0
96-F 4.0
96-F 4.333333333333333
96-F 4.0
96-F 4.0
96-F 3.6666666666666665
96-M 5.0
96-M 4.5
96-M 4.333333333333333
96-M 4.5
96-M 4.4
96-M 4.333333333333333
96-M 4.142857142857143

有人可以帮我解决我的代码错误以及纠正方法吗?

修改 样本字典按要求包含,

dict_items([('128-F,2854', '4'), ('96-M,3985', '4'), ('128-F,3210', '5'), ('96-F,3755', '4'), ('128-F,2913', '5'), ('128-M,4647', '5'), ('128-F,4771', '4'), ('96-F,4946', '5'), ('128-M,4795', '5'), ('128-M,1268', '4'), ('96-M,5468', '4'), ('96-M,889', '3'), ('96-F,2041', '4'), ('128-M,5627', '3'), ('96-F,5643', '4'), ('128-M,235', '5'), ('96-M,5848', '4'), ('96-M,386', '4'), ('96-M,4169', '5'), ('96-F,5812', '2'), ('128-F,3301', '4'), ('128-M,3367', '5'), ('96-F,5458', '3'), ('96-M,1671', '5'), ('128-F,2825', '3')])

谢谢,Lobbie

2 个答案:

答案 0 :(得分:4)

首先,你可以使用枚举来让你的生活更轻松:

for i, pair in enumerate(MyList1):

(尽管i和枚举对于这个例子来说是不必要的,但要记住它仍然是一件好事)。 然后是下一行:

x = pair[0].split(',')[0]
y = int(pair[1])

所以这是我做的一个快速示例,它完全符合您的要求:

l = [('128-F,2825', '3'), ('128-F,2854', '4'), ('128-F,2913', '5'), ('128-F,3210', '5'), ('128-F,3301', '4'), ('128-F,4771', '4'), ('128-M,1268', '4'), ('128-M,235', '5'), ('128-M,3367', '5'), ('128-M,4647', '5'), ('128-M,4795', '5'), ('128-M,5627', '3'), ('96-F,2041', '4'), ('96-F,3755', '4'), ('96-F,4946', '5'), ('96-F,5458', '3'), ('96-F,5643', '4'), ('96-F,5812', '2'), ('96-M,1671', '5'), ('96-M,386', '4'), ('96-M,3985', '4'), ('96-M,4169', '5'), ('96-M,5468', '4'), ('96-M,5848', '4'), ('96-M,889', '3')]

result = {}

for pair in l:
    key = pair[0].split(',')[0]
    val = int(pair[1])
    try:
        result[key].append(val)
    except KeyError:
        result[key] = [val]

for key, values in sorted(result.items()):
    print(key, sum(values) / len(values))

答案 1 :(得分:1)

你应该直接遍历你的词典,而不是先将它转换成一个列表,这是一个不必要的步骤。

# shortened example data:
MyList1 = [('128-F,2825','3'), ('128-F,2854','4'), ('128-F,2913','5'), ('128-F,3210','5')]

result = {}

for key, value in MyDict.items():
    description = key.split(',')[0]
    rating = int(value)
    result.setdefault(description, []).append(rating)

for description, ratings in sorted(result.items()):
    print(description, sum(ratings) / len(ratings))

为了避免与Matthias Schreiber进行冗长的讨论,请注意上面的答案是在我发布前几分钟发布的,并且他和我的代码的结构有一些相似之处。我没有真正的动机来讨论这个问题,所以如果你想要归因于任何事情,那就拿这个吧。