1

这是我的问题。

我有一个列表列表,如下所示:

linesort=[
  ['Me', 1, 596], 
  ['Mine', 1, 551], 
  ['Myself', 1, 533], 
  ['Myself', 1, 624], 
  ['Myself', 1, 656], 
  ['Myself', 1, 928], 
  ['Theirs', 1, 720], 
  ['Theirs', 1, 1921], 
  ['Them', 1, 716], 
  ['Themselves', 1, 527]
]

每个子列表代表参与者对单词进行正确或错误分类所花费的时间(第二个值)和响应时间(第三个值)。我想做的是返回另一个列表列表,其中包含单词、每个列表中第二个值的总和以及第三个值的平均值。

本质上,我需要比较每个子列表的第一个元素,如果它们相等,则计算第二个元素的总和和第三个元素的平均值。

虽然我能够手动执行此操作(即手动分配和创建变量),但我在循环中执行此操作的尝试都失败了。鉴于我有两个包含此类数据的相当大的文本文件,我将不胜感激编程解决方案。

一些可能有用的点:我事先知道每个测试中使用了哪些单词,但我不知道它们会出现在哪里(即使它们出现在任何一组刺激中)。谁能帮我解决这个问题?

我在 Ubuntu 10.04 上使用 Python 2.6.5。

4

3 回答 3

3

不是漂亮的,而是:

from collections import defaultdict

linesort = [['Me', 1, 596], ['Mine', 1, 551], ['Myself', 1, 533], ['Myself', 1, 624],
            ['Myself', 1, 656], ['Myself', 1, 928], ['Theirs', 1, 720], 
            ['Theirs', 1, 1921], ['Them', 1, 716], ['Themselves', 1, 527]]

d = defaultdict(list)
for line in linesort:
    d[line[0]].append(line[1:])


output = {}
for x,val in d.items():
    svals = [y[1] for y in val]
    output[x] = [sum([y[0] for y in val]), sum(svals) / len(svals)] # need to be modified if you need float value

print output
>>> {'Mine': [1, 551], 'Theirs': [2, 1320], 'Me': [1, 596], 'Them': [1, 716], 'Themselves': [1, 527], 'Myself': [4, 685]}

或者使用 groupby(注意它不是最有效的,需要对包含初始数据的列表进行排序):

from itertools import groupby

res = {}
for key, gen in groupby(sorted(linesort), key=lambda x: x[0]):
    val = list(gen)
    svals = [y[2] for y in val]
    res[key] = [sum([y[1] for y in val]), sum(svals) / float(len(svals))]

但是我之前的所有示例都会返回一个字典,所以如果你想获得一个列表,你只需要稍微修改一下代码:

from itertools import groupby

res = []
for key, gen in groupby(sorted(linesort), key=lambda x: x[0]):
    val = list(gen)
    svals = [y[2] for y in val]
    res.append([key, sum([y[1] for y in val]), sum(svals) / float(len(svals))])

print res
>>> [['Me', 1, 596.0], ['Mine', 1, 551.0], ['Myself', 4, 685.25], ['Theirs', 2, 1320.5], ['Them', 1, 716.0], ['Themselves', 1, 527.0]]
于 2011-06-21T12:16:47.647 回答
1

我的详细解决方案

#!/usr/bin/env python

import collections

linesort=[['Me', 1, 596], ['Mine', 1, 551], ['Myself', 1, 533], ['Myself', 1, 624],
          ['Myself', 1, 656], ['Myself', 1, 928],['Theirs', 1, 720], ['Theirs', 1, 1921],
          ['Them', 1, 716], ['Themselves', 1, 527]]
new=[]

d=collections.defaultdict(list)
for i in linesort:
    d[i[0]].append(i[1:])

for k,v in d.iteritems():
    s=sum([i[0] for i in v])
    avg=sum([i[1] for i in v]) / len(v)

    new.append([k,s,avg])

for i in new: print i

输出:

['Me', 1, 596]
['Myself', 4, 685]
['Theirs', 2, 1320]
['Mine', 1, 551]
['Themselves', 1, 527]
['Them', 1, 716]
于 2011-06-21T12:28:50.917 回答
1

这是我的简单解决方案:

#!/usr/bin/python

linesort=[['Me', 1, 596], ['Mine', 1, 551], ['Myself', 1, 533], ['Myself', 1, 624], ['Myself', 1, 656], ['Myself', 1, 928], ['Theirs', 1, 720], ['Theirs', 1, 1921], ['Them', 1, 716], ['Themselves', 1, 527]]

cnts = {};
sums = {};
# here we count occurrences of each word (cnts),
# and we compute the the sum of second elements of each input list
for list in linesort:
  cnts[list[0]] = cnts.get(list[0], 0) + 1;
  sums[list[0]] = sums.get(list[0], 0) + list[1];

# now that we know the occurrences for each work we can compute
# the averages of the third elements of each input list 
avgs = {};
for list in linesort:
  avgs[list[0]] = avgs.get(list[0], 0) + list[2] / cnts[list[0]];

# we build the result as a list of lists
result = [];
for word in avgs:
  result.append([word, sums[word], avgs[word]]);

print result;

输出是:

[['Me', 1, 596], ['Myself', 4, 685], ['Theirs', 2, 1320], ['Mine', 1, 551], ['Themselves', 1, 527], ['Them', 1, 716]]
于 2011-06-21T12:52:43.290 回答