美文网首页
Python学习笔记-字典和集合

Python学习笔记-字典和集合

作者: 在Azkaban写代码 | 来源:发表于2018-09-29 11:12 被阅读10次

1、字典

创建的方法
a = dict(one = 1, two = 2, three = 3)
b = {'one' : 1, 'two' : 2, 'three' : 3}
c = dict(zip(['one', 'two', 'three'], [1,2,3]))
d = dict([('two',2),('one', 1),('three',3)])
e = dict({'three' : 3, 'one' : 1,'two' : 2})
各种内置方法
  1. fromkeys()创建并返回一个新字典
>>> dict.fromkeys((1,2,3) , ("one", "two", "three"))
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
  1. key()用于返回字典中的键,values() 用于返回字典中的所有值、items() 返回所有键值对
  2. get(键值)可以得到对应的值,如果没有,返回None
  3. clear() 清空内容
  4. in 或者 not in 可以判断一个键是否在字典中
  5. copy() 复制字典
  6. pop() 弹出对应的值,popitem() 弹出一个项
>>>a=dict.fromkeys((1,2,3) , ("one", "two", "three"))
>>>a
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
>>> a.pop(2)
('one', 'two', 'three')
>>> a
{1: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
>>> a.popitem()
(3, ('one', 'two', 'three'))
>>> a
{1: ('one', 'two', 'three')}
>>>
>>> a = {"白":'白',"xiao":'白',"小白":'白'} # 更新字典
>>> a.update(小白= '5')
>>> 5

2、集合

相关文章

  • Python学习笔记-字典和集合

    1、字典 创建的方法 各种内置方法 fromkeys()创建并返回一个新字典 key()用于返回字典中的键,val...

  • Python学习笔记-集合和字典

    集合 集合写在大括号里,元素之间用逗号隔开,创建一个空集合必须用 set() 而不是 { },因为因为 { } 是...

  • Python从入门到精通(day03)

    接着学习世界上最美丽的语言,Python,数据类型还有字典和集合没有学,今天我们继续把他们学完。 字典和集合 字典...

  • Python学习——字典和集合

    Python学习——字典和集合 泛映射类型 (1)什么是可散列的数据类型?如果一个对象是可散列的,那么在这个对象的...

  • 【10.27】Python语法练习6/17

    学习任务:字典和集合 一、字典python中内置里字典,使用键-值(key-value)存储,具有极快的查找速度。...

  • python 字典相关操作

    1. python 在列表、字典、集合中筛选数据 列表:filter函数、列表解析 字典:字典解析 集合:集合解析...

  • 2020-04-26

    Python 入门 学习使用 Python 处理数字与字符串,编写函数和条件语句;操作列表、集合、字典等常见数据类...

  • Python字典和集合

    init。。

  • python 字典和集合

    一、复习(列表) 1.容器,可以同时存放多个数据。可变,有序2.元素,可以是任何类型的数据,一个列表可以同时存放不...

  • Python字典和集合

    1 字典基础操作 1.1 创建字典 通过{}操作符创建字典aDict = {'name':'ke', 'age':...

网友评论

      本文标题:Python学习笔记-字典和集合

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