zoukankan      html  css  js  c++  java
  • python----基础之数据类型(元祖,字典,集合)

    元祖:

    元祖的定义和特性

    定义:列表已经介绍过,现在介绍一种与类表相似的类型,叫做元祖,只不过把[]改成()。

    特性:

    1.可以存放多个指

    2.不可变

    3.按照从左往右的顺序定义元祖的元素,下标从0开始依次顺序访问,有序

    元祖的创建与常用类型

    1 # 创建
    2 >>> we = (11, 22, 33, 44, 55)
    3 >>> we
    4 (11, 22, 33, 44, 55)
    5 >>> type(we)
    6 <class 'tuple'>
    7 >>> tuple((11,33,44))
    8 (11, 33, 44)

    常用操作

    # 索引取值
    >>> we
    (11, 22, 33, 44, 55)
    >>> we[0]
    11
    >>> we[-1]
    55
    >>> we[2]
    33
    # 遍历
    >>> for k in we:
    ...     print(k)
    ...
    11
    22
    33
    44
    55

    len方法,切片,包含等方法与列表一样

    元祖的特性:

    1.可存放多个值

    如果元祖中只有一个值

    # 元祖中如果只存在一个值,要这样写
    >>> a = (1)
    >>> type(a) # 这样写,python默认判断为是一个int类型的
    <class 'int'>
    >>> a = (1,)
    >>> type(a)  # 这样写,python才可以判断为是元祖类型
    <class 'tuple'>

    元祖中不仅可以可以存放数字,字符串,还可以存放更加复杂的数据类型.

    2.不可变

    元祖本身不可变,如果元祖中还包含其他可变的元素,这些可变的元素可以改变.

     1 >>> a = (1,2,3,4,[5,6,7,8])
     2 >>> a
     3 (1, 2, 3, 4, [5, 6, 7, 8])
     4 >>> a[2] = 9
     5 Traceback (most recent call last):
     6   File "<stdin>", line 1, in <module>
     7 TypeError: 'tuple' object does not support item assignment
     8 >>> a[4][0] = 9
     9 >>> a
    10 (1, 2, 3, 4, [9, 6, 7, 8])
    11 >>> a[4][1] = 10
    12 >>> a
    13 (1, 2, 3, 4, [9, 10, 7, 8])
    14 >>>

    字典:

    字典的定义与特性

    字典是python语言中唯一的映射类型.

    Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。

    定义:

    {key1:value1,key2:value2}

    1.键与值之间用':'隔开;

    2.项与项之间用','隔开;

    特性:

    1.key-value结构

    2.key必须科hash,且必须为不可变数据类型,必须唯一

    3.可存放任意多个值,可修改,可以不唯一

    4.无序

    字典的创建与常见操作

    字典的创建

    1 >>> pep = {'name':'alex', 'age':18}
    2 >>> pep
    3 {'name': 'alex', 'age': 18}
    4 >>> pep = dict(name='ike',age=18)
    5 >>> pep
    6 {'name': 'ike', 'age': 18}
    7 >>> pep = dict({'name':'joke','age':5})
    8 >>> pep
    9 {'name': 'joke', 'age': 5}
    1 >>> dic = {}.fromkeys(['k1','k2'],[])
    2 >>> dic
    3 {'k1': [], 'k2': []}
    4 >>> dic['k1'].append(22)
    5 >>> dic
    6 {'k1': [22], 'k2': [22]}
    7 >>> dic['k2'].append(220)

    字典的常见操作

    键、值、键值对
        1、dic.keys() 返回一个包含字典所有KEY的列表;
        2、dic.values() 返回一个包含字典所有value的列表;
        3、dic.items() 返回一个包含所有(键,值)元祖的列表;
        4、dic.iteritems()、dic.iterkeys()、dic.itervalues() 与它们对应的非迭代方法一样,不同的是它们返回一个迭代子,而不是一个列表;
    新增
        1、dic['new_key'] = 'new_value';
        2、dic.setdefault(key, None) ,如果字典中不存在Key键,由 dic[key] = default 为它赋值;_
    删除
        1、dic.pop(key[,default]) 和get方法相似。如果字典中存在key,删除并返回key对应的vuale;如果key不存在,且没有给出default的值,则引发keyerror异常;
        2、dic.clear() 删除字典中的所有项或元素;    
    修改
        1、dic['key'] = 'new_value',如果key在字典中存在,'new_value'将会替代原来的value值;
        2、dic.update(dic2) 将字典dic2的键值对添加到字典dic中
    查看
        1、dic['key'],返回字典中key对应的值,若key不存在字典中,则报错;
        2、dict.get(key, default = None) 返回字典中key对应的值,若key不存在字典中,则返回default的值(default默认为None)
    循环
        1、for k in dic.keys()
        2、for k,v in dic.items()
        3、for k in dic
    长度
        1、len(dic)

    给定一个名字,要查找对应的成绩,就先要在names中找到对应的位置,再从scores取出对应的成绩,list越长,耗时越长。

    如果用dict实现,只需要一个“名字”-“成绩”的对照表,直接根据名字查找成绩,无论这个表有多大,查找速度都不会变慢。用Python写一个dict如下:

    >>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
    >>> d['Michael']
    95

    为什么dict查找速度这么快?因为dict的实现原理和查字典是一样的。假设字典包含了1万个汉字,我们要查某一个字,一个办法是把字典从第一页往后翻,直到找到我们想要的字为止,这种方法就是在list中查找元素的方法,list越大,查找越慢。

    第二种方法是先在字典的索引表里(比如部首表)查这个字对应的页码,然后直接翻到该页,找到这个字。无论找哪个字,这种查找速度都非常快,不会随着字典大小的增加而变慢。

    dict就是第二种实现方式,给定一个名字,比如'Michael',dict在内部就可以直接计算出Michael对应的存放成绩的“页码”,也就是95这个数字存放的内存地址,直接取出来,所以速度非常快。

    你可以猜到,这种key-value存储方式,在放进去的时候,必须根据key算出value的存放位置,这样,取的时候才能根据key直接拿到value。

    把数据放入dict的方法,除了初始化时指定外,还可以通过key放入:

    >>> d['Adam'] = 67
    >>> d['Adam']
    67

    由于一个key只能对应一个value,所以,多次对一个key放入value,后面的值会把前面的值冲掉:

    >>> d['Jack'] = 90
    >>> d['Jack']
    90
    >>> d['Jack'] = 88
    >>> d['Jack']
    88

    如果key不存在,dic就会报KeyError错误:

    >>> d['Thomas']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'Thomas'

    要避免key不存在的错误,有两种办法,一是通过in判断key是否存在:

    >>> 'Thomas' in d
    False

    二是通过dict提供的get()方法,如果key不存在,可以返回None,或者自己指定的value:

    >>> d.get('Thomas')
    >>> d.get('Thomas', -1)
    -1

    注意:返回None的时候Python的交互环境不显示结果。

    要删除一个key,用pop(key)方法,对应的value也会从dict中删除:

    >>> d.pop('Bob')
    75
    >>> d
    {'Michael': 95, 'Tracy': 85}

    字典的工厂函数

     1 class dict(object):
     2     """
     3     dict() -> new empty dictionary
     4     dict(mapping) -> new dictionary initialized from a mapping object's
     5         (key, value) pairs
     6     dict(iterable) -> new dictionary initialized as if via:
     7         d = {}
     8         for k, v in iterable:
     9             d[k] = v
    10     dict(**kwargs) -> new dictionary initialized with the name=value pairs
    11         in the keyword argument list.  For example:  dict(one=1, two=2)
    12     """
    13     def clear(self): # real signature unknown; restored from __doc__
    14         """ D.clear() -> None.  Remove all items from D. """
    15         pass
    16 
    17     def copy(self): # real signature unknown; restored from __doc__
    18         """ D.copy() -> a shallow copy of D """
    19         pass
    20 
    21     @staticmethod # known case
    22     def fromkeys(*args, **kwargs): # real signature unknown
    23         """ Returns a new dict with keys from iterable and values equal to value. """
    24         pass
    25 
    26     def get(self, k, d=None): # real signature unknown; restored from __doc__
    27         """ D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None. """
    28         pass
    29 
    30     def items(self): # real signature unknown; restored from __doc__
    31         """ D.items() -> a set-like object providing a view on D's items """
    32         pass
    33 
    34     def keys(self): # real signature unknown; restored from __doc__
    35         """ D.keys() -> a set-like object providing a view on D's keys """
    36         pass
    37 
    38     def pop(self, k, d=None): # real signature unknown; restored from __doc__
    39         """
    40         D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
    41         If key is not found, d is returned if given, otherwise KeyError is raised
    42         """
    43         pass
    44 
    45     def popitem(self): # real signature unknown; restored from __doc__
    46         """
    47         D.popitem() -> (k, v), remove and return some (key, value) pair as a
    48         2-tuple; but raise KeyError if D is empty.
    49         """
    50         pass
    51 
    52     def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
    53         """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
    54         pass
    55 
    56     def update(self, E=None, **F): # known special case of dict.update
    57         """
    58         D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
    59         If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]
    60         If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v
    61         In either case, this is followed by: for k in F:  D[k] = F[k]
    62         """
    63         pass
    64 
    65     def values(self): # real signature unknown; restored from __doc__
    66         """ D.values() -> an object providing a view on D's values """
    67         pass
    View Code

    请务必注意,dict内部存放的顺序和key放入的顺序是没有关系的。

    和list比较,dict有以下几个特点:

    1. 查找和插入的速度极快,不会随着key的增加而变慢;
    2. 需要占用大量的内存,内存浪费多。

    而list相反:

    1. 查找和插入的时间随着元素的增加而增加;
    2. 占用空间小,浪费内存很少。

    所以,dict是用空间来换取时间的一种方法。

    dict可以用在需要高速查找的很多地方,在Python代码中几乎无处不在,正确使用dict非常重要,需要牢记的第一条就是dict的key必须是不可变对象

    这是因为dict根据key来计算value的存储位置,如果每次计算相同的key得出的结果不同,那dict内部就完全混乱了。这个通过key计算位置的算法称为哈希算法(Hash)。

    要保证hash的正确性,作为key的对象就不能变。在Python中,字符串、整数等都是不可变的,因此,可以放心地作为key。而list是可变的,就不能作为key:

    >>> key = [1, 2, 3]
    >>> d[key] = 'a list'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'list'

    集合:

    认识集合

    集合是一个数学概念:由一个或多个确定的元素所构成的整体叫做集合。

    集合中的元素有三个特征:

    1.确定性,(元素必须科hash)

    2.互异性(去重)

    3.无序性(集合中的元素没有先后之分),比如:{123},{213}算作是一个集合

    注意:集合存在的意义就在于去重和关系运算,有自动过滤的功能

    集合解决的问题

    >>> w = {'张三','李四','王五','joker'}  # 定义一个集合
    >>> e = {'张三','李四','王五','janny'}
    >>> we = w&e
    >>> we
    {'李四', '张三', '王五'}

    集合的定义

    1 l= {1,2,3,1}  # 此处应说明集合“去重”的效果
    2 # 定义可变集合
    3 >>> set_test=set('hello')  # 此处应说明集合的“无序性”
    4 >>> set_test
    5 {'l', 'o', 'e', 'h'}
    6 # 改为不可变集合frozenset
    7 >>> f_set_test=frozenset(set_test)
    8 >>> f_set_test
    9 frozenset({'l', 'e', 'h', 'o'})

    集合的关系运算

    交集:&或intersection

    >>> w = {'张三','李四','王五','joker'}  # 定义一个集合
    >>> e = {'张三','李四','王五','janny'}
    >>> we = w&e
    >>> we
    {'李四', '张三', '王五'}

    并集:|或union

    1 >>> a = {'张三','李四','janny'}
    2 >>> b = {'张三','李四','joker'}
    3 >>> a.union(b)
    4 {'janny', '张三', '李四', 'joker'}
    5 >>> print(a|b)
    6 {'janny', '张三', '李四', 'joker'}

    差集:-或difference

     1 >>> a = {'张三','李四','janny'}
     2 >>> b = {'张三','李四','joker'}
     3 >>> a
     4 {'张三', '李四', 'janny'}
     5 >>> b
     6 {'张三', '李四', 'joker'}
     7 >>> a.difference(b)
     8 {'janny'}
     9 
    10 >>> a
    11 {'张三', '李四', 'janny'}
    12 >>> b
    13 {'张三', '李四', 'joker'}
    14 >>> print(b-a)
    15 {'joker'}

    对称差集:^或symmetric_difference

    1 >>> a = {1,2,3,4}
    2 >>> b={1,2,3,4,5,6}
    3 >>> a.symmetric_difference(b)
    4 {5, 6}
    5 >>> a^b
    6 {5, 6}

    包含关系:

    1、in,not in:判断某元素是否在集合内

    2、==,!=:判断两个集合是否相等

    两个集合之间一般有三种关系,相交、包含、不相交。在Python中分别用下面的方法判断:

    • set.isdisjoint(s):判断两个集合是不是不相交
    • set.issuperset(s):判断集合是不是包含其他集合,等同于a>=b
    • set.issubset(s):判断集合是不是被其他集合包含,等同于a<=b

    集合的常用操作

    元素的增加

    单个元素的增加 : add(),add的作用类似列表中的append

    对序列的增加 : update(),而update类似extend方法,update方法可以支持同时传入多个参数:

    >>> a = {1,2}
    >>> a.update([3,4],[5,6,8,7])
    >>> a
    {1, 2, 3, 4, 5, 6, 7, 8}
    >>> a.update('hello')
    >>> a
    {1, 2, 3, 4, 5, 6, 7, 8, 'l', 'h', 'e', 'o'}
    >>> a
    {1, 2, 3, 4, 5, 6, 7, 8, 'l', 'h', 'e', 'o'}
    >>> a.add('hello')
    >>> a
    {1, 2, 3, 4, 5, 6, 7, 8, 'l', 'h', 'e', 'hello', 'o'}

    元素的删除

    集合删除单个元素有两种方法:

    元素不在原集合中时:

    set.discard(x)不会抛出异常

    set.remove(x)会抛出KeyError错误

    >>> a = {1,2,3,45,6}
    >>> a.discard(1)
    >>> a
    {2, 3, 6, 45}
    >>> a.discard(1)
    >>> a
    {2, 3, 6, 45}
    >>> a.remove(3)
    >>> a.remove(3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 3

    pop():由于集合是无序的,pop返回的结果不能确定,且当集合为空时调用pop会抛出KeyError错误,

    clear():清空集合

    >>> a={3,"a",2.1,11,2,3,4,5}
    >>> a
    {2, 3, 'a', 4, 5, 11, 2.1}
    >>> a.pop()
    2
    >>> a.pop()
    3
    >>> a.pop()
    'a'
    >>> a.pop()
    4
    >>> a
    {5, 11, 2.1}
    >>> a.clear()
    >>> a
    set()
    >>> a.pop()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'pop from an empty set'

    集合的工厂函数

      1 class set(object):
      2     """
      3     set() -> new empty set object
      4     set(iterable) -> new set object
      5 
      6     Build an unordered collection of unique elements.
      7     """
      8     def add(self, *args, **kwargs): # real signature unknown
      9         """
     10         Add an element to a set.
     11 
     12         This has no effect if the element is already present.
     13         """
     14         pass
     15 
     16     def clear(self, *args, **kwargs): # real signature unknown
     17         """ Remove all elements from this set. """
     18         pass
     19 
     20     def copy(self, *args, **kwargs): # real signature unknown
     21         """ Return a shallow copy of a set. """
     22         pass
     23 
     24     def difference(self, *args, **kwargs): # real signature unknown
     25         """
     26         相当于s1-s2
     27 
     28         Return the difference of two or more sets as a new set.
     29 
     30         (i.e. all elements that are in this set but not the others.)
     31         """
     32         pass
     33 
     34     def difference_update(self, *args, **kwargs): # real signature unknown
     35         """ Remove all elements of another set from this set. """
     36         pass
     37 
     38     def discard(self, *args, **kwargs): # real signature unknown
     39         """
     40         与remove功能相同,删除元素不存在时不会抛出异常
     41 
     42         Remove an element from a set if it is a member.
     43 
     44         If the element is not a member, do nothing.
     45         """
     46         pass
     47 
     48     def intersection(self, *args, **kwargs): # real signature unknown
     49         """
     50         相当于s1&s2
     51 
     52         Return the intersection of two sets as a new set.
     53 
     54         (i.e. all elements that are in both sets.)
     55         """
     56         pass
     57 
     58     def intersection_update(self, *args, **kwargs): # real signature unknown
     59         """ Update a set with the intersection of itself and another. """
     60         pass
     61 
     62     def isdisjoint(self, *args, **kwargs): # real signature unknown
     63         """ Return True if two sets have a null intersection. """
     64         pass
     65 
     66     def issubset(self, *args, **kwargs): # real signature unknown
     67         """ 
     68         相当于s1<=s2
     69 
     70         Report whether another set contains this set. """
     71         pass
     72 
     73     def issuperset(self, *args, **kwargs): # real signature unknown
     74         """
     75         相当于s1>=s2
     76 
     77          Report whether this set contains another set. """
     78         pass
     79 
     80     def pop(self, *args, **kwargs): # real signature unknown
     81         """
     82         Remove and return an arbitrary set element.
     83         Raises KeyError if the set is empty.
     84         """
     85         pass
     86 
     87     def remove(self, *args, **kwargs): # real signature unknown
     88         """
     89         Remove an element from a set; it must be a member.
     90 
     91         If the element is not a member, raise a KeyError.
     92         """
     93         pass
     94 
     95     def symmetric_difference(self, *args, **kwargs): # real signature unknown
     96         """
     97         相当于s1^s2
     98 
     99         Return the symmetric difference of two sets as a new set.
    100 
    101         (i.e. all elements that are in exactly one of the sets.)
    102         """
    103         pass
    104 
    105     def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
    106         """ Update a set with the symmetric difference of itself and another. """
    107         pass
    108 
    109     def union(self, *args, **kwargs): # real signature unknown
    110         """
    111         相当于s1|s2
    112 
    113         Return the union of sets as a new set.
    114 
    115         (i.e. all elements that are in either set.)
    116         """
    117         pass
    118 
    119     def update(self, *args, **kwargs): # real signature unknown
    120         """ Update a set with the union of itself and others. """
    121         pass
    View Code

    总结:本文介绍了元祖,字典,集合的定义、特性、常用操作以及各自的内置方法。

  • 相关阅读:
    02Hibernate入门
    01Hibernate概述
    ExtJS布局-Layout
    STARLIMS 安装升级解决方案
    Windows Server 2012 安装.Net Framework 3.5
    vim 复制操作
    linux下挂载新硬盘
    转载 更改mysql 数据存储路径
    转载 阿里云centOS防火墙配置
    (转载)java线程
  • 原文地址:https://www.cnblogs.com/cnike/p/10437049.html
Copyright © 2011-2022 走看看