iTesting软件测试知识分享

Python字典Dict总结

Python字典Dict总结,

  1. 实现对字典排序
    python 字典(dict)的特点就是无序的,按照键(key)来提取相应值(value),如果我们需要字典按值排序的话,怎么办呢?
    假设有这么一个字典 :
    1
    dict = {‘apple’:3, “orange”:2, “oil”:3}
    *按照key排序
    1
    2
    return_dict = sorted(dict.iteritems(), key=lambda d:d[0], reverse=True)
    print return_dict
    *按照value排序
    1
    2
    return_dict = sorted(dict.iteritems(), key=lambda d:d[1], reverse=True)
    print return_dict
  2. 字典实现一键对应多值
    在编码中,比如你得到了一个字典,字典里是一个sprint的每个story及它对应的优先级,那么我们要统计优先级为P的story都有哪些,该怎么办呢?这就涉及到如何利用字典实现一键对应多值。
    例如原生字典如下:
    1
    2
    3
    4
    5
    dict = {"ATEAM-0001": "P1", "ATEAM-0002": "P0", "ATEAM-0003": "P1"}
    new_dict = {}
    for k, v in dict.iteritems():
    new_dict.setdefault(v,[]).append(k)
    print new_dict
    扩展阅读:
    Following is the syntax for setdefault() method −

dict.setdefault(key, default=None)

Parameters

key – This is the key to be searched.
default – This is the Value to be returned in case key is not found.

Return Value

This method returns the key value available in the dictionary and if given key is not available then it will return provided default value.

🐶 您的支持将鼓励我继续创作 🐶
-------------评论, 吐槽, 学习交流,请关注微信公众号 iTesting-------------
请关注微信公众号 iTesting wechat
扫码关注,跟作者互动