我处理的是三本非常大的字典,看起来像这样:
dict_a = { ( 't','e' ) : [0.5,0.1,0.6], ( 'a','b' ) : [0.2,0.3,0.9] }
dict_b = { ( 'a','b' ) : [0.1,0.5,0.3] , ( 't','e' ) : [0.6,0.1,0.6] }
dict_c = { ( 'a','b' ) : [0.1,0.5,0.3] , ( 't','e' ) : [0.6,0.5,0.6] }
我在寻找这样的输出:
name first_value second_value third_value
0 (t, e) [0.5, 0.1, 0.6] [0.6, 0.1, 0.6] [0.6, 0.5, 0.6]
1 (a, b) [0.2, 0.3, 0.9] [0.1, 0.5, 0.3] [0.1, 0.5, 0.3]
我试过的是:
final_dict = {'name': [] , 'first_value' : [] ,'second_value': [] , 'third_value': [] }
for a,b in dict_a.items():
for c,d in dict_b.items():
for e,f in dict_c.items():
if a==c==e:
final_dict['name'].append(a)
final_dict['first_value'].append(b)
final_dict['second_value'].append(d)
final_dict['third_value'].append(f)
这是真正没有效率和优化的方式来完成这项任务。我想用熊猫。你知道吗
如何在最小的时间复杂度下完成这项任务?你知道吗
谢谢你!你知道吗
因为这些是字典,所以您只需要迭代一个。您可以使用该键从其他键获取相应的值。你知道吗
示例:
怎么样:
输出:
试试这个方式:-你知道吗
你知道吗输出:-你知道吗
相关问题 更多 >
编程相关推荐