美文网首页
python基础:—— 理解:list,tuple,dict,s

python基础:—— 理解:list,tuple,dict,s

作者: zcc_ios | 来源:发表于2018-04-01 23:04 被阅读8次

    1.list——可变数组

    list:有序数组大小可变 类似oc中的NSMutableArray
    怎么写:

    >>> listExample = ['hello', 'how', 'are', 'u']
    >>> listExample
    ['hello', 'how', 'are', 'U']
    

    外面中扩号,里面逗号隔开。
    添加元素:

    >>> listExample.append(3)
    >>> listExample
    ['hello', 'how', 'are', 'U', 3]
    

    元素可以是基本类型。
    指定位置插入对象:

    >>> listExample.insert(0, 'apple')
    >>> listExample
    ['apple', 'hello', 'how', 'are', 'U', 3]
    

    删除对象:

    >>> listExample.pop()
    3
    >>> listExample
    ['apple', 'hello', 'how', 'are', 'U']
    >>> 
    

    pop()方法不加参数默认删除队尾最后一个对象
    也可选择第几个删除

    >>> listExample
    ['apple', 'hello', 'how', 'are', 'U']
    >>> listExample.pop(1)
    'hello'
    >>> listExample
    ['apple', 'how', 'are', 'U']
    >>> 
    

    list可以直接替换某个对象比如:

    >>> listExample
    ['apple', 'how', 'are', 'U']
    >>> listExample[0] = 'hello'
    >>> listExample
    ['hello', 'how', 'are', 'U']
    

    稍微复杂点的 list可以存另一个list

    >>> example2 = ['python', 'is', 'easy']
    >>> listExample.append(example2)
    >>> listExample
    ['hello', 'how', 'are', 'U', ['python', 'is', 'easy']]
    >>> 
    

    获取example2中的元素:

    >>> listExample
    ['hello', 'how', 'are', 'U', ['python', 'is', 'easy']]
    >>> listExample[4][1]
    'is'
    

    类似于C语言中的二维数组


    2.tuple——不可变数组

    初始化:

    >>> tupleExample = ('i', 'am', 'fine', 'thank', 'U')
    >>> tupleExample
    ('i', 'am', 'fine', 'thank', 'U')
    
    • 但是tuple在一定情况下又是可变的:
      比如 在tuple中加入list数组
    >>> tupleExample2 = ('php', 'Object-C', 'java', listExample)
    >>> tupleExample2
    ('php', 'Object-C', 'java', ['hello', 'how', 'are', 'U', ['python', 'is', 'easy']])
    >>> tupleExample2[3][1] = 'apple'
    >>> tupleExample2
    ('php', 'Object-C', 'java', ['hello', 'apple', 'are', 'U', ['python', 'is', 'easy']])
    
    

    3.字典dict

    创建一个字典:

    >>> dictExample = {'language': 'python', 'name': 'xiaohua', 'year': 12}
    >>> dictExample
    {'language': 'python', 'name': 'xiaohua', 'year': 12}
    

    添加一个键值对

    >>> dictExample['sex'] = '男'
    >>> dictExample
    {'language': 'python', 'name': 'xiaohua', 'year': 12, 'sex': '男'}
    >>>  
    

    删除元素

    >>> dictExample.pop('language')
    'python'
    >>> dictExample
    {'name': 'xiaohua', 'year': 12, 'sex': '男'}
    

    获取某个元素

    {'name': 'xiaohua', 'year': 12, 'sex': '男'}
    >>> dictExample['year']
    12
    

    但是如果键值错了或者不存在会崩溃报错

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

    为了避免这个情况 需要用到get()方法,key值不存在时返回none

    >>> print(dictExample.get('age'))
    None
    

    也可以自定义返回内容

    >>> dictExample.get('age', '该键值不存在')
    '该键值不存在'
    

    set 类似 dict 但是只是存一组key 并且不能重复 看到这东西的时候我是有点懵的

    创建一个set

    >>> setExample = set([1, 2, 3, (4, 5)])
    >>> setExample
    {(4, 5), 1, 2, 3}
    

    set不能存字典

    >>> list1 = [dictExample, 2]
    >>> list1
    [{'name': 'xiaohua', 'year': 11, 'sex': '男'}, 2]
    >>> setExample2 = set(list1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'dict'
    

    同样也可以增加key 删去key(不能像list一样通过序号去删除)

    >>> setExample
    {(4, 5), 1, 2, 3}
    >>> setExample.add(23)
    >>> setExample
    {1, 2, 3, (4, 5), 23}
    >>> setExample.remove(4)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 4
    >>> setExample.remove((4, 5))
    >>> setExample
    {1, 2, 3, 23}
    

    那么 set可以加list, tuple或者dict吗?

    
    >>> setExample.add([1, 2, 3])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    以上添加list失败
    
    
    >>> setTest = set([(1, 2, 3)])
    >>> setTest
    {(1, 2, 3)}
    以上添加tuple成功
    
    >>> setExample
    {1, 2, 3, 23}
    >>> setExample.add([1, 2, 3])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'list'
    说明只要包含list的 都不能添加到set中
    
    >>> dictTest = {'1': (1,2,3)}
    >>> dictTest
    {'1': (1, 2, 3)}
    >>> setTest.add(dictTest)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'dict'
    set也不能添加dict
    
    
    并且包含dic的tuple也不行
    >>> setTest = set([(1, 2, 3)])
    >>> setTest
    {(1, 2, 3)}
    >>> setTest.add(('1', {'2':'22'}))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'dict'
    

    相关文章

      网友评论

          本文标题:python基础:—— 理解:list,tuple,dict,s

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