使用两个list创建dict
a = ['math','english','art']
b = ['99','78','66']
c = dict(zip(a,b))
创建指定list为键名的dict
c = {}.fromkeys([keylist],默认值)
使用sorted对字典进行排序,本质上还是sorted函数的使用
sorted(iterable, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
e = sorted(c.items(),key=lambda d : d[0])
e = sorted(c.items(),key=lambda d : d[1])
本文介绍了如何使用Python中的两个List来创建字典(dict),包括使用zip函数将两个List组合成字典,以及使用fromkeys方法创建具有指定键的字典。此外,还探讨了如何利用sorted函数对字典项进行排序,包括按键或值的升序或降序排列。

4105

被折叠的 条评论
为什么被折叠?



