• 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读写unicode文件的方法
  • Pyramid Mako模板引入helper对象的步骤方法
  • python安装oracle扩展及数据库连接方法
  • python利用正则表达式提取字符串
  • python制作一个桌面便签软件
  • python3中str(字符串)的使用教程
  • Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
  • Python中的字符串类型基本知识学习教程
  • Python中正则表达式的详细教程
  • Python自动化构建工具scons使用入门笔记

文章分类

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

最近更新的内容

    • Python下的twisted框架入门指引
    • python处理二进制数据的方法
    • python进阶教程之动态类型详解
    • python中的函数用法入门教程
    • python实现SMTP邮件发送功能
    • 简单讲解Python中的字符串与字符串的输入输出
    • 解决Python出现_warn_unsafe_extraction问题的方法
    • Python实现的最近最少使用算法
    • 爬山算法简介和Python实现实例
    • Python单例模式实例详解

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

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