给定的列表包含每个记录的3个元素,如下所示:
a_list = [('This is A.', 00, 2.0),
('This is B.', 01, 3.0),
('This is C.', 02, 3.0),
('This is D.', 03, 1.5)]
结果应仅产生第三要素得分等于或大于平均值的记录。结果应该是这样的:
result = [('This is B.', 01, 3.0),
('This is C.', 02, 3.0)]
因为给定列表中第三个元素的平均值是2.375
。
这是我一直在尝试的尝试和错误消息,请参见以下内容:
get_mean = np.mean(dict(a_list).values())
d = dict(a_list)
get_final = dict((x, y, z) for x, y, z in d.items() if z >= get_mean)
get_item = get_final.items()
get_result = sorted(get_item, reverse=True, key=lambda tup: tup[1])
return get_result
ErrorMsg --> ValueError: dictionary update sequence element #0 has length 3; 2 is required
这将完成以下工作:
您可以尝试使用列表理解:
输出:
编辑:
您得到的错误来自
dict(a_list)
。当从元组列表创建字典时,每个元组应该有2个元素(key, value)
。不可能创建包含3个元素的元组的字典。你知道吗相关问题 更多 >
编程相关推荐