张凯朝

张凯朝的博客

他的个人主页  他的博客

Python-basic-3tips

张凯朝  2010年04月08日 星期四 21:48 | 1115次浏览 | 0条评论

1. 改变字典键值::

    >>> dd = {'a': 11, 'b': 22, 'c': 33}
    >>> dd['aa'] = dd.pop('a')
    >>> dd
    {'aa': 11, 'b': 22, 'c': 33}

2. 字典的 setdefault 方法::

如果字典中不存在该键则设置该键对应的值为给定的值,并返回该值;
如果已经存在则不做任何事,并返回该键在字典中的值::

    >>> dd = {'a': 11, 'b': 22, 'c': 33}
    >>> dd.setdefault('d', 44)
    44
    >>> dd
    {'a': 11, 'b': 22, 'c': 33, 'd': 44}
    >>> dd.setdefault('a', 44)
    11
    >>> dd
    {'a': 11, 'b': 22, 'c': 33, 'd': 44}

3. bisect 模块::

    >>> import bisect
    >>> aa = [11, 22, 33, 44, 44, 44, 55, 66]
    >>> bisect.bisect_right(44, aa)
    6
    >>> bisect.bisect_left(44, aa)
    3
    >>> bisect.insort_right(45, aa)
    >>> aa
    [11, 22, 33, 44, 44, 44, 45, 55, 66]
    >>> bisect.insort_right(43, aa)
    >>> aa
    [11, 22, 33, 43, 44, 44, 44, 45, 55, 66]

评论

我的评论:

发表评论

请 登录 后发表评论。还没有在Zeuux哲思注册吗?现在 注册 !

暂时没有评论

Zeuux © 2024

京ICP备05028076号