• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中的字典与成员运算符初步探究

Python中的字典与成员运算符初步探究

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了python 字典,python 字符串转字典,python 字典排序,python字典添加元素,python 遍历字典等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

Python元字典
字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型。列表是有序的对象结合,字典是无序的对象集合。
两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
字典用"{ }"标识。字典由索引(key)和它对应的值value组成。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"

tinydict = {'name': 'john','code':6734, 'dept': 'sales'}


print dict['one'] # 输出键为'one' 的值
print dict[2] # 输出键为 2 的值
print tinydict # 输出完整的字典
print tinydict.keys() # 输出所有键
print tinydict.values() # 输出所有值

</div>

输出结果为:

This is one This is two {'dept': 'sales', 'code': 6734, 'name': 'john'} ['dept', 'code', 'name'] ['sales', 6734, 'john']
</div>

Python成员运算符
除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。

以下实例演示了Python所有成员运算符的操作:

#!/usr/bin/python

a = 10
b = 20
list = [1, 2, 3, 4, 5 ];

if ( a in list ):
  print "Line 1 - a is available in the given list"
else:
  print "Line 1 - a is not available in the given list"

if ( b not in list ):
  print "Line 2 - b is not available in the given list"
else:
  print "Line 2 - b is available in the given list"

a = 2
if ( a in list ):
  print "Line 3 - a is available in the given list"
else:
  print "Line 3 - a is not available in the given list"

</div>

以上实例输出结果:

Line 1 - a is not available in the given list
Line 2 - b is not available in the given list
Line 3 - a is available in the given list
</div>

</div>

您可能想查找下面的文章:

  • Python中异常重试的解决方案详解
  • 详解python 字符串和日期之间转换 StringAndDate
  • Python中异常重试的解决方案详解
  • 详解python 字符串和日期之间转换 StringAndDate
  • 详解Python中类的定义与使用
  • python获取指定时间差的时间实例详解
  • 详解Python中类的定义与使用
  • python获取指定时间差的时间实例详解
  • Python 类的继承实例详解
  • python 类详解及简单实例

相关文章

  • Python使用PDFMiner解析PDF代码实例
  • 在Python中处理字符串之isdigit()方法的使用
  • pymssql数据库操作MSSQL2005实例分析
  • python实现在每个独立进程中运行一个函数的方法
  • Python面向对象编程中的类和对象学习教程
  • Python中装饰器的一个妙用
  • python pdb调试方法分享
  • Python字符串处理之count()方法的使用
  • python文件读写并使用mysql批量插入示例分享(python操作mysql)
  • 实例解析Python设计模式编程之桥接模式的运用

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 通过数据库对Django进行删除字段和删除模型的操作
    • Python下载指定页面上图片的方法
    • Python实现的Google IP 可用性检测脚本
    • Python字符串特性及常用字符串方法的简单笔记
    • python分割列表(list)的方法示例
    • Python实现的tab文件操作类分享
    • Python中的rfind()方法使用详解
    • Python中__new__与__init__方法的区别详解
    • Python实现Windows和Linux之间互相传输文件(文件夹)的方法
    • Python实现FLV视频拼接功能

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有