我需要将'rev'和'tta'键及其值附加到新列表中。我该怎么做?
这是字典:
vl = {0: {'art': 5612306, 'rev': 3.0, 'report_id': 751233}, 1: {'art': 567206, 'tta': 2.8, 'report_id': 751233}}
我尝试做的事情并出现错误:
n_list = []
for elem in vl:
n_list.append(vl[elem][1])
答案 0 :(得分:1)
这是您想要的吗?
function formatData(data){
var nObject = {};
data.forEach(d=>{
nObject[moment(d.datetime).format('MM-DD-YYYY')]=d.Score;
});
return nObject;
}
答案 1 :(得分:0)
尝试这样
vl = {0: {'art': 5612306, 'rev': 3.0, 'report_id': 751233}, 1: {'art': 567206, 'tta': 2.8, 'report_id': 751233}}
n_list = []
for elem in vl:
val = vl[elem]
if 'rev' in val:
n_list.append(val['rev'])
elif 'tta' in val:
n_list.append(val['tta'])
print(n_list)
#[3.0, 2.8]
答案 2 :(得分:0)
您可以这样写,但是这段代码仅适合这种结构,
n_list = []
for elem in vl:
n_list.append(vl[elem][tuple(vl[elem])[1]])