作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下字典:
rts = {
"PO1": {
"congruent": {
"rt": [0.647259, 0.720116, 0.562909, 0.538918, 0.633367],
"correct": ["True", "True", "True", "True", "True", "False",]
},
"incongruent": {
"rt": [0.647259, 0.720116, 0.562909, 0.538918, 0.633367],
"correct": ["True", "True", "True", "True", "True", "False",]
}
},
"PO2": {
"congruent": {
"rt": [0.647259, 0.720116, 0.562909, 0.538918, 0.633367],
"correct": ["True", "True", "True", "True", "True", "False",]
},
"incongruent": {
"rt": [0.647259, 0.720116, 0.562909, 0.538918, 0.633367],
"correct": ["True", "True", "True", "True", "True", "False",]
}
}
}
import csv
from pathlib import Path
import json
import numpy as np
from numpy import array
def main():
rts = {}
statsDict = {}
data = Path('C:/Users/oli.warriner/Desktop/data(2)/data')
for csvfile in data.glob('*.csv'):
key = csvfile.stem
with csvfile.open() as f:
csv_reader = csv.reader(f)
# Skip the header
_ = next(csv_reader)
rts[key] = {
'congruent': {
'rt': [],
'correct': []
},
'incongruent': {
'rt': [],
'correct': []
},
}
for tn, ctext, cname, condition, response, rt, correct in csv_reader:
rts[key][condition]['rt'].append(float(rt))
rts[key][condition]['correct'].append(correct)
for k in rts:
key = k
statsDict[key] = {
'congruent': {
'mean': [],
'stddev': [],
'correct': []
},
'incongruent': {
'mean': [],
'stddev': [],
'correct': []
},
}
for n in rts[k]:
for i in rts[key][n]
array([rts[k] for k in rts]).mean()
print(array)
if __name__ == "__main__":
main()
最佳答案
基于rts
的例子给定,您可以使用以下代码片段构建一个带有统计信息的字典:
import statistics
import json
rts = { ... as given ... }
stats_dict = {}
for k in rts.keys():
stats_dict[k] = {}
for ck in rts[k].keys():
stats_dict[k][ck] = {}
stats_dict[k][ck]["mean"] = statistics.mean(rts[k][ck]["rt"])
stats_dict[k][ck]["stdev"] = statistics.stdev(rts[k][ck]["rt"])
stats_dict[k][ck]["true_percentage"] = len([x for x in rts[k][ck]["correct"] if x == "True"]) / len(rts[k][ck]["correct"])
print(json.dumps(stats_dict, indent=2))
numpy
计算静力。内置statistics
包够用rts
并为统计字典使用相同的键 stats_dict
{
"PO1": {
"congruent": {
"mean": 0.6205138,
"stdev": 0.07207165926839758,
"true_percentage": 0.8333333333333334
},
"incongruent": {
"mean": 0.6205138,
"stdev": 0.07207165926839758,
"true_percentage": 0.8333333333333334
}
},
"PO2": {
"congruent": {
"mean": 0.6205138,
"stdev": 0.07207165926839758,
"true_percentage": 0.8333333333333334
},
"incongruent": {
"mean": 0.6205138,
"stdev": 0.07207165926839758,
"true_percentage": 0.8333333333333334
}
}
}
关于python - 循环遍历嵌套字典值以填充第二个字典 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60378720/
我是一名优秀的程序员,十分优秀!