Python 字典(Dictionary) cmp()方法


描述

Python 字典的 cmp() 函数用于比较两个字典元素。高佣联盟 www.cgewang.com

语法

cmp()方法语法:

cmp(dict1, dict2)

参数

  • dict1 -- 比较的字典。
  • dict2 -- 比较的字典。

返回值

如果两个字典的元素相同返回0,如果字典dict1大于字典dict2返回1,如果字典dict1小于字典dict2返回-1。

实例

以下实例展示了 cmp()函数的使用方法:

实例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- dict1 = {'Name': 'Zara', 'Age': 7}; dict2 = {'Name': 'Mahnaz', 'Age': 27}; dict3 = {'Name': 'Abid', 'Age': 27}; dict4 = {'Name': 'Zara', 'Age': 7}; print "Return Value : %d" % cmp (dict1, dict2) print "Return Value : %d" % cmp (dict2, dict3) print "Return Value : %d" % cmp (dict1, dict4)

以上实例输出结果为:

Return Value : -1
Return Value : 1
Return Value : 0

Python 字典(Dictionary) cmp()方法的更多相关文章

  1. Python 字典(Dictionary) clear()方法

    Python 字典(Dictionary) clear()方法 描述 Python 字典(Dictionary) clear() 函数用于删除字典内所有元素.高佣联盟 www.cgewang.com ...

  2. Python 字典(Dictionary) type()方法

    Python 字典(Dictionary) type()方法 描述 Python 字典(Dictionary) type() 函数返回输入的变量类型,如果变量是字典就返回字典类型.高佣联盟 www.c ...

  3. Python 字典(Dictionary) str()方法

    Python 字典(Dictionary) str()方法 描述 Python 字典(Dictionary) str() 函数将值转化为适于人阅读的形式,以可打印的字符串表示.高佣联盟 www.cge ...

  4. Python 字典(Dictionary) len()方法

    Python 字典(Dictionary) len()方法 描述 Python 字典(Dictionary) len() 函数计算字典元素个数,即键的总数.高佣联盟 www.cgewang.com 语 ...

  5. Python 字典(Dictionary) get()方法

    描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...

  6. Python 字典(Dictionary) setdefault()方法

    描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...

  7. Python 字典(Dictionary) has_key()方法

    描述 Python 字典(Dictionary) has_key() 函数用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false. 语法 has_key()方法语法:dic ...

  8. Python 字典(Dictionary) values()方法

    描述 Python 字典(Dictionary) values() 函数以列表返回字典中的所有值. 语法 values()方法语法: dict.values() 参数 NA. 返回值 返回字典中的所有 ...

  9. Python 字典(Dictionary) keys()方法

    Python 字典(Dictionary) keys() 函数以列表返回一个字典所有的键. 语法 keys()方法语法: dict.keys() 参数 NA. 返回值 返回一个字典所有的键. 实例 以 ...

随机推荐

  1. 同一WpfApplication下简单的页面转换

    别人写的很不错的Dome...分享学习下 源文件地址 : http://pan.baidu.com/share/link?shareid=1698564707&uk=3912660076

  2. HTTPS 和 SSL/TLS 协议:密钥交换(密钥协商)算法及其原理

    转自:https://blog.csdn.net/andylau00j/article/details/54583769 本系列的前一篇,咱们聊了“密钥交换的难点”以及“证书体系”的必要性.今天这篇来 ...

  3. PBR(基于物理的渲染)学习笔记

    PBR基本介绍 PBR代表基于物理的渲染,本质上还是 gl_FragColor = Emssive + Ambient + Diffuse + Specular 可能高级一些在考虑下AO也就是环境光遮 ...

  4. 《 ZooKeeper : Wait-free coordination for Internet-scale systems 》论文研读

    Zookeeper 研读 说明:本文为论文 < ZooKeeper : Wait-free coordination for Internet-scale systems > 的个人理解, ...

  5. GitHub 热点速览 Vol.27:程序员的自我救赎——GitHub 摸鱼

    作者:HelloGitHub-小鱼干 摘要:都知道 VSCode 有各种摸鱼小插件,边听云音乐.边在 IDE 斗地主,再来一个 NBA 直播,怎一个美滋滋了得.作为 VSCode 的同门,GitHub ...

  6. mybitis下choose..when. otherwise条件不起作用

    我的代码如下: <select id="findList" resultType="TyArticle"> SELECT <include r ...

  7. 【python接口自动化】01-requests请求

    requests是模拟HTTP.https请求接口. 一.HTTP协议介绍 1.1HTTP协议特点 1.2HTTP协议组成 请求方法 主要的请求方法有:GET.POST.DELETE .PUT .HE ...

  8. djangorestframework学习1-通过HyperlinkedModelSerializer,ModelViewSet,routers编写第一个接口

    前提首先安装了django,安装方式:pip install django 1. djangorestftamework安装: pip install djangorestframework 2. 创 ...

  9. python os 模块的使用

    1.显示当前文件的绝对路径: os.path.abspath(__file__) 2.显示当前文件父目录的路径 os.path.dirname(os.path.abspath(__file__name ...

  10. 轮播图-bxslider

    bxSlider下载+参数说明 “bxSlider”就是一款响应式的幻灯片js插件 bxSlider特性 充分响应各种设备,适应各种屏幕: 支持多种滑动模式,水平.垂直以及淡入淡出效果: 支持图片.视 ...