美文网首页
python学习指南之字典

python学习指南之字典

作者: 码上就说 | 来源:发表于2018-08-22 21:43 被阅读28次

字典的value可以是任何python对象。操作非常灵活。
字典数据结构中存在的基本操作 :

  • 访问字典
  • 添加key-value对
  • 修改字典value
  • 删除key-value
  • 遍历字典

删除key-value

dict = {"one":3, "two":"hello,world", "three":23.8, "four":True};
print "原来的字典序列:",dict;

del dict["one"];
print "删除后的字典序列:", dict;

输出的结果是:

原来的字典序列: {'four': True, 'three': 23.8, 'two': 'hello,world', 'one': 3}
删除后的字典序列: {'four': True, 'three': 23.8, 'two': 'hello,world'}

遍历字典

遍历key-value对

dict = {"one":3, "two":"hello,world", "three":23.8, "four":True};
for key,value in dict.items():
    print "key=",key,
    print "value=", value;

输出的结果是:

key= four value= True
key= three value= 23.8
key= two value= hello,world
key= one value= 3

遍历key

dict = {"one":3, "two":"hello,world", "three":23.8, "four":True};
for key in dict.keys():
    print "key=",key;

输出的结果是:

key= four
key= three
key= two
key= one

遍历value

dict = {"one":3, "two":"hello,world", "three":23.8, "four":True};
for value in dict.values():
    print "value=",value;

输出的结果是:

value= True
value= 23.8
value= hello,world
value= 3

相关文章

  • python字典

    本篇将介绍Python里面的字典,更多内容请参考:Python学习指南 Python是什么? Python内置了字...

  • python学习指南之字典

    字典的value可以是任何python对象。操作非常灵活。字典数据结构中存在的基本操作 : 访问字典添加key-v...

  • python模块之默认值字典

    python模块之默认值字典

  • 一 -22 python (基础)字典的定义

    1 字典的定义 - 字典是没有索引,是无序的 dictionary(字典) 是 除列表以外 Python 之...

  • 字典

    Python的字典 字典:在python中,字典是一系列键-值对。每个键都与一个值相关联,可以使用键来访问与之相关...

  • Python之列表、字典、元祖常见操作

    Python 将列表转为字典 Python元祖转为字典

  • python之字典

    字典(dictionary):是一种映射类型(mapping type),它是一个无序的键:值对集合。关键字必须使...

  • python之字典

    字典是另一种可变容器模型,且可存储任意类型对象。字典的每个键值(key=>value)对用冒号(:)分割,每个对之...

  • Python之字典

    字典我理解为感C语言中的链表,Perl语言中的键值对一样。 chl = {'emperor': 'nobility...

  • Day01自学 - Python 字典(Dictionary)

    学习参考博客地址:Python 字典(Dictionary) | Python 优雅的操作字典 一、创建字典 字典...

网友评论

      本文标题:python学习指南之字典

      本文链接:https://www.haomeiwen.com/subject/mpkuiftx.html