python字典如何计算键值对的总数

{'a':('123123','asdasd','123asd','000000','111111','123456','1234567','12345678','123456789','a123456'),
'b':('123123','asdasd','123asd','000000','111111','123456','1234567','12345678','123456789','a123456'),
'c':('123123','asdasd','123asd','000000','111111','123456','1234567','12345678','123456789','a123456'),
'd':('123123','asdasd','123asd','000000','111111','123456','1234567','12345678','123456789','a123456'),
'e':('123123','asdasd','123asd','000000','111111','123456','1234567','12345678','123456789','a123456'),
'f':('123123','asdasd','123asd','000000','111111','123456','1234567','12345678','123456789','a123456'),
'g':('123123','asdasd','123asd','000000','111111','123456','1234567','12345678','123456789','a123456')}
如上,键值对有70,使用len()方法只能得出7,求在最好不用FOR循环的情况用dict对象最优的自带方法使结果为70(如果有的话)
慕姐4208626
浏览 4961回答 2
2回答

当年话下

追求性能能的情况下,列表推导性能最高,循环次之,map最差所以写法应该是用列表推导更合适sum([len(_)for_ind.values()])

ibeautiful

那你这是不算是计算键值对的数量啊...而是计算每个值的length之和,所以没有办法使用dict对象内置的东东来解决目前我想到的方法就是sum(map(lambdax:len(x)ifhasattr(x,"__len__")else1,v)//v是你的dict抛砖引玉吧
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript