多平台统一管理软件接口,如何实现多平台统一管理软件接口
293
2022-08-27
Python小记——初始化字典序列,字典排序和字典推导(Python 字典 排序)
字典的初始化
In [5]: a = dict(one=1, two=2, three=3)In [6]: b = {'one':1, 'two':2, 'three':3}In [7]: aOut[7]: {'one': 1, 'three': 3, 'two': 2}In [8]: bOut[8]: {'one': 1, 'three': 3, 'two': 2}
dictNum = dict(enumerate([0] * 10, 0)) # int:int
{0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
dictNum[0] += 1
{0: 1, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
字典排序
d={"ok":1,"no":2}#对字典按键排序,用元组列表的形式返回d1 = sorted(d.items(), key=lambda d:d[0],reverse = False) #[('no', 2), ('ok', 1)]#对字典按值排序,用元组列表的形式返回d2 = sorted(d.items(), key=lambda d:d[1],reverse = True) #[('ok', 1), ('no', 2)]print d1,'\n',d2
In [10]: DIAL_CODES = [ ...: (86, 'China'), ...: (91, 'India'), ...: (1, 'US'), ...: (62, 'Indonesia'), ...: (55, 'Brazil'), ...: (92, 'Pakistan'), ...: (880, 'Bangladesh'), ...: (234, 'Nigeria'), ...: (7, 'Russia'), ...: (81, 'Japan'), ...: ] In [11]: country_code = {country:code for code, country in ...: DIAL_CODES} In [12]: country_code Out[12]: {'Bangladesh': 880, 'Brazil': 55, 'China': 86, 'India': 91, 'Indonesia': 62, 'Japan': 81, 'Nigeria': 234, 'Pakistan': 92, 'Russia': 7, 'US': 1} In [13]: {code:country.upper() for country, code in country ...: _code.items() if code < 66} Out[13]: {1: 'US', 7: 'RUSSIA', 55: 'BRAZIL', 62: 'INDONESIA'}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~