zoukankan      html  css  js  c++  java
  • python 数据类型

    列表 list

    创建list 

     a_list = ['a', 'b', 'mpilgrim', 'z', 'example'] 

    访问元素

    >> a_list[4] 
    
    'example' 
    
    >> a_list[-1] 
    
    'example'
    
    

    列表切片

    >>> a_list[1:3]
    
    ['b', 'mpilgrim']
    
    >>> a_list[1:-1] 
    ['b', 'mpilgrim', 'z']
    
    >>> a_list[:3]  
    ['a', 'b', 'mpilgrim'] 
    
    >>> a_list[3:] 
    ['z', 'example']
    
    >>> a_list[:] 
    ['a', 'b', 'mpilgrim', 'z', 'example']
    
    

    向列表中新增项

    >>> a_list = ['a'] 
    >>> a_list = a_list + [2.0, 3] 
    
    >>> a_list.insert(0, 'Ω') 
    
    >>> a_list 
    ['Ω', 'a', 2.0, 3, True, 'four', 'Ω']
    
    >>> a_list = ['a', 'b', 'c'] 
    
    >>> a_list.extend(['d', 'e', 'f'])
    
    >>> a_list 
    ['a', 'b', 'c', 'd', 'e', 'f']
    
    >>> a_list.append(['g', 'h', 'i']) 
    
    >>> a_list 
    ['a', 'b', 'c', 'd', 'e', 'f', ['g', 'h', 'i']]
    
    

    在列表中检索值

    >>> a_list = ['a', 'b', 'new', 'mpilgrim', 'new']
    
    >>> a_list.count('new') 
    
    2 
    
    >>> 'new' in a_list 
    
    True
    
    >>> a_list.index('mpilgrim') 
    
    3 
    
    >>> a_list.index('new') 
    
    2
    
    

    从列表中删除元素

    >>> a_list = ['a', 'b', 'new', 'mpilgrim', 'new'] 
    
    >>> a_list[1]
    
    'b'
    
    >>> del a_list[1] 
    
    >>> a_list ['a', 'new', 'mpilgrim', 'new']
    
    >>> a_list.remove('new') 
    
    >>> a_list ['a', 'mpilgrim', 'new']
    
    >>> a_list.remove('new') 
    
    >>> a_list 
    ['a', 'mpilgrim']
    
    >>> a_list = ['a', 'b', 'new', 'mpilgrim'] 
    
    >>> a_list.pop() 
    
    'mpilgrim'
    
    >>> a_list ['a', 'b', 'new']
    
    >>> a_list.pop(1) 
    
    'b'
    
    

    复制列表  b = a_list[:]  而不是b = a_list   因为简单的赋值结果他们还指向同一个列表 修改一个另一个会受到影响

    元组

    元组 是不可变的列表。一旦创建之后,用任何方法都不可以修改元素。

    新建   切片

    >>> a_tuple = ("a", "b", "mpilgrim", "z", "example") 
    
    >>> a_tuple 
    ('a', 'b', 'mpilgrim', 'z', 'example')
    
    >>> a_tuple[0] 
    
    'a'
    
    >>> a_tuple[-1] 
    
    'example' 
    
    >>> a_tuple[1:3] 
    
    ('b', 'mpilgrim')
    
    可用的方法 tuple()可将列表转化为元组

    同样list() 可将元组转化为列表

    元组不可修改 也就没有 pop() remove() del()等方法

    任何至少包含一个上元素的元组为真值。元素的值无关紧要。为创建单元素元组,需要在值之后加上一个逗号。没有逗号,Python 会假定这只是一对额外的圆括号,虽然没有害处,但并不创建元组

    ()、2不是元组 但 2,  是 (false,)也是元组

    以下是一种很酷的编程捷径:在 Python 中,可使用元组来一次赋多值

    >>> v = ('a', 2, True)
    
    >>> (x, y, z) = v 
    
    >>> x
    
    'a' 
    
    >>> y 
    
    2 
    
    >>> z
    
    True
    
    

    该特性有多种用途。假设需要将某个名称指定某个特定范围的值。可以使用内建的 range() 函数进行多变量赋值以快速地进行连续变量赋值。

    >>> (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)  
    >>> MONDAY0
    >>> TUESDAY
    1
    >>> SUNDAY
    6

    range() 函数返回的既不是列表也不是元组,而是一个 迭代器

    集合

    集合是一个装唯一值的容器 值不能重复

    创建集合

    >>>a_set = {1,2,3,true}      #创建集合只需将值用{}括起来,多个值之间用, 隔开
    
    >>>a_set
    
    {1,2,3,true}
    
    >>> type(a_set) 
    
    <class 'set'>            #set 以class的形式存在
    
    

    还可以 列表 为基础创建集合

    >>> a_list = ['a', 'b', 'mpilgrim', True, False, 42]
    >>> a_set = set(a_list)                                #要从列表创建集合,可使用 set() 函数>>> a_set                                         
    {'a', False, 'b', True, 'mpilgrim', 42}

    创建空列表

    >>> a_set = set() 
    >>> a_set  set() 
    >>> type(a_set)  
    <class 'set'> 
    >>> len(a_set) 
    0

    修改集合

    向现有集合中添加值: add() 方法和 update() 方法

    >>> a_set = {1, 2} 
    >>> a_set.add(4)  
    >>> a_set 
    {1, 2, 4} 
    >>> len(a_set)  
    3 
    >>> a_set.add(1)                 #集合是一个装唯一值的容器 值不能重复  像集合中添加已有的值将不会有任何反映 也不会报错
    >>> a_set 
    {1, 2, 4} 
    >>> len(a_set)  
    3>>> a_set = {1, 2, 3} 
    >>> a_set 
    {1, 2, 3} 
    >>> a_set.update({2, 4, 6})     #update()函数相当于add  并且会忽略掉冲突的值
    >>> a_set  
    {1, 2, 3, 4, 6} 
    >>> a_set.update({3, 6, 9}, {1, 2, 3, 5, 8, 13}) #update()函数可以添加多个参数 
    >>> a_set 
    {1, 2, 3, 4, 5, 6, 8, 9, 13} 
    >>> a_set.update([10, 20, 30])             #update()函数 把一些其它数据类型作为参数 
    >>> a_set 
    {1, 2, 3, 4, 5, 6, 8, 9, 10, 13, 20, 30}
    删除集合中的值有三种方法
    discard() remove() 和 pop()
    >>> a_set = {1, 3, 6, 10, 15, 21, 28, 36, 45}
    >>> a_set 
    {1, 3, 36, 6, 10, 45, 15, 21, 28} 
    >>> a_set.discard(10)                      #删掉10
    >>> a_set 
    {1, 3, 36, 6, 45, 15, 21, 28} 
    >>> a_set.discard(10)                      #如果需要删掉的值不存在 不会有任何反映 只是相当于一条空指令
    >>> a_set 
    {1, 3, 36, 6, 45, 15, 21, 28} 
    >>> a_set.remove(21)                       #删掉值 
    >>> a_set 
    {1, 3, 36, 6, 45, 15, 28} 
    >>> a_set.remove(21)                       #如果删掉的值不存在 引发一个KeyError 例外
    Traceback (most recent call last): 
    	File "<stdin>", line 1, in <module> 
    KeyError: 21
    >>> a_set = {1, 3, 6, 10, 15, 21, 28, 36, 45} 
    >>> a_set.pop()                                     #集合没有顺序的概念 pop()随机删除一个值 并返回删除的值36
    >>> a_set 
    {6, 10, 45, 15, 21, 28} 
    >>> a_set.clear()                                 #clear()函数清空集合 相当于a_set = set() 创建一个空集合并覆盖掉之前的集合        
    >>> a_set set() 
    >>> a_set.pop() 
    Traceback (most recent call last):  
    	 File "<stdin>", line 1, in <module> 
    KeyError: 'pop from an empty set'

    常见的集合操作

    • 要检测某值是否是集合的成员,可使用 in 运算符。其工作原理和列表的一样。    10 in a_set
    • union() 方法返回一个新集合,其中装着 在两个 集合中出现的元素。  (并集)             a_set.union(b_set)
    • intersection() 方法返回一个新集合,其中装着 同时 在两个集合中出现的所有元素。(交集)  a_set.intersection(b_set)
    • difference() 方法返回的新集合中,装着所有在 a_set 出现但未在 b_set 中的元素。(差集  a_set – b_set)   a_set.difference(b_set)
    • symmetric_difference() 方法返回一个新集合,其中装着所有 只在其中一个 集合中出现的元素。a_set.symmetric_difference(b_set)

    a_set.issubset(b_set)                #检测a_set 是不是b_set的子集

    b_set.issuperset(a_set)        #检测b_set是不是a_set的超集

    字典

    创建字典

    a_dict = {“name” : “goodspeed”, ”phone”:”64748947”}
    
    a_dict[“name”]                            #根据键查找键值
    
    goodspeed
    
    

    修改字典

    a_dict[“phone”] = “15266666”
    
    a_dict
    
    {“name” : “goodspeed”,”phone”:”15266666”}
    
    a_dict[“age”] = “23”
    
    a_dict
    
    {“name” : “goodspeed”,”phone”:”15266666”,”age”:”23”}
    
    

    #键值可以是多种形式 列表也可以用作键值

    SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],  1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']}

    通过字典解析实现的小技巧: 交换字典的键和值

    >>> a_dict = {'a': 1, 'b': 2, 'c': 3}
    >>> {value:key for key, value in a_dict.items()}
    {1: 'a', 2: 'b', 3: 'c'}

    None

    None是python的特殊常量     是唯一的空值  有自己的数据类型NoneType


    作者:GoodSpeed Cheng
    出处:http://www.cnblogs.com/cacique/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。


  • 相关阅读:
    most-wanted-letter
    non-unique-elements
    python组建之paramiko的简介和简单使用
    Android平台使用termux,随时随地写代码
    发现生活中数学的美,然后记录下来
    别再用Microsoft Office,改为WPS或者Latex
    office2016 vol 中文版本
    Office 2016 英文版(VOL版)下载
    选择好用的生产力工具
    使用windows(win7和win10),最好用chocolatey
  • 原文地址:https://www.cnblogs.com/cacique/p/2364364.html
Copyright © 2011-2022 走看看