Python:跟踪字典中特定值的最佳方法

时间:2014-07-24 22:51:48

标签: python mysql dictionary

我正在使用SQLAlchemy来查询数据库。结果输出是字典列表。

示例输出

[{'status': 'complete', 'value': '1', 'question': 'q1', 'user_name': u'Charlie', 'id': 4054810},
{'status': 'complete', 'value': '1', 'question': 'q2', 'user_name': u'Charlie', 'id': 4054810},
{'status': 'complete', 'value': '1', 'question': 'q3', 'user_name': u'Charlie', 'id': 4054810},
{'status': 'complete', 'value': '0', 'question': 'q4', 'user_name': u'Charlie', 'id': 4054810},
{'status': 'complete', 'value': '1', 'question': 'q5', 'user_name': u'Charlie', 'id': 4054810},
{'status': 'complete', 'value': '1', 'question': 'q1', 'user_name': u'Charlie', 'id': 4018647},
{'status': 'complete', 'value': '1', 'question': 'q2', 'user_name': u'Charlie', 'id': 4018647},
{'status': 'complete', 'value': '1', 'question': 'q3', 'user_name': u'Charlie', 'id': 4018647},
{'status': 'complete', 'value': '1', 'question': 'q4', 'user_name': u'Charlie', 'id': 4018647},
{'status': 'complete', 'value': '1', 'question': 'q5', 'user_name': u'Charlie', 'id': 4018647}]

我需要跟踪相应问题的值。即

如果有两个'q5'列,我需要按值递增。

期望的输出:

[Charlie, q1, 2], [Charlie, q2, 2] 

但是因为q4中的1个值为0,所以它将是

[Charlie, q4, 1]

2 个答案:

答案 0 :(得分:1)

这个怎么样?

# Results container
results = {}

# Collect the data
for item in data:
    # key here is just for later, 
    # you could use anything unique to the entry 
    # (username for example)
    key = "{0}.{1}".format(item.get('user_name'), item.get('question'))
    if key in results:
        results[key] += item.get('value', 0)
    else:
        results[key] = item.get('value', 0)

# Prints the results as you would like.
for key, count in results.iteritems():
   name, question = key.split('.')
   print [name, question, count]

答案 1 :(得分:0)

将结果合并在一起。

def merge_dicts(results):
    out = {}
    for result in results:
        res = out.get(result["question"])
        if res is None:
            result["value"] = int(result["value"])
            res = result
        else:
            res["value"] += int(result["value"])
        out[res["question"]] = res
    return out.values()

a = merge_dict([{'status': 'complete', 'value': '1', 'question': 'q1', 'user_name': u'Charlie', 'id': 4054810},
{'status': 'complete', 'value': '1', 'question': 'q2', 'user_name': u'Charlie', 'id': 4054810},
{'status': 'complete', 'value': '1', 'question': 'q3', 'user_name': u'Charlie', 'id': 4054810},
{'status': 'complete', 'value': '0', 'question': 'q4', 'user_name': u'Charlie', 'id': 4054810},
{'status': 'complete', 'value': '1', 'question': 'q5', 'user_name': u'Charlie', 'id': 4054810},
{'status': 'complete', 'value': '1', 'question': 'q1', 'user_name': u'Charlie', 'id': 4018647},
{'status': 'complete', 'value': '1', 'question': 'q2', 'user_name': u'Charlie', 'id': 4018647},
{'status': 'complete', 'value': '1', 'question': 'q3', 'user_name': u'Charlie', 'id': 4018647},
{'status': 'complete', 'value': '1', 'question': 'q4', 'user_name': u'Charlie', 'id': 4018647},
{'status': 'complete', 'value': '1', 'question': 'q5', 'user_name': u'Charlie', 'id': 4018647}])