python / 内置的数据结构概述

@time    2019-07-30
@author  Ruo_Xiao
@notice  后续部分这两天补充。

零、前言

type中文名称
list列表
tuple元组
dict字典
set集合

一、list

https://blog.csdn.net/itworld123/article/details/104966308

二、tuple

1、特点

(1)固定列表,一旦初始化其中的元素不可修改。

(2)列表中的元素可以是 python 中的任何对象。

(3)所有元素由一个小括号“( )”包裹。

2、相关操作

(1)增

由于元组本身是不可改变的,所以没有函数向元组中增加元素,不过可以直接使用“+”来增加元素。但是,增加完元素之后的元组和增加元素之前的元组不是同一个,即:位于不同的内存空间中。栗子如下:

tup = (1,2,3,"tt",4.2)
new_tup = tup + ("23",)
print("new_tup = ",new_tup)
print("tup = ",tup)
new_tup =  (1, 2, 3, 'tt', 4.2, '23')
tup =  (1, 2, 3, 'tt', 4.2)

 (2)删

none。

(3)改

元素本身不能更改的,但是若元素是 list ,是可以更改 list 中的元素,但是不能将元素 list 改为整数、字符串什么的 。

(4)查

类似于数组的索引。

三、dict

1、特点

(1)字典中的数据必须得以键值对(key - value)的形式出现。

(2)键值不可重复,值可以重复。但是 dict 只会记得最后一个值。

(3)键(key)不可改变,但是值(value)可以改变。

(4)所有的键值对(key - value)以大括号“{ }”的形式包裹。

2、相关操作

(1)增

        a、update(),在字典尾部加入新的 key - value 。

        b、直接修改某 key 的 value,若该 key 不存在本 dict 中,则自动创建该 key 。

(2)删

        a、pop(),指定 key,删除 key - value 。

        b、del ,指定 key,删除 key - value 。

        c、clear(),清空 dict 。

(3)改

        直接修改已存在的 key 对应的 value 即可。

(4)查

        a、类似于 map 的索引。

        b、用 get() 查询指定的 key 是否存在,若不存在则返回 null。

栗子:

if __name__ == '__main__':
    dicttest = {'one': 1, 'two': 2, 'three': 3}
    print('init:', dicttest)

    # 增
    dicttest.update({'four': 4})
    print('added four:', dicttest)

    dicttest['five'] = 5
    print('added five:', dicttest)

    # 删
    dicttest.pop('two')
    print('delete two:', dicttest)

    del dicttest['five']
    print('delete five:', dicttest)

    # 改
    dicttest['one'] = 10
    print('modify one:', dicttest)

    # 查
    print('show three:', dicttest['three'])

结果:

init: {'one': 1, 'two': 2, 'three': 3}
added four: {'one': 1, 'two': 2, 'three': 3, 'four': 4}
added five: {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
delete two: {'one': 1, 'three': 3, 'four': 4, 'five': 5}
delete five: {'one': 1, 'three': 3, 'four': 4}
modify one: {'one': 10, 'three': 3, 'four': 4}
show three: 3

 四、set

1、特点

(1)接近于数学中集合的概念。

(2)各个元素不可重复。可通过这个特性来过滤数据结构中重复的元素。

(3)元素不可修改。

TypeError: 'set' object does not support item assignment

(3)所有的元素以大括号“{ }”的形式包裹。

2、相关操作

(1)增

        add(),在 set 中添加元素,该元素的位置不确定。

(2)删

        remove(),删除指定的元素。

(3)改

        none 。

(4)查

        none,不支持索引。

(5)支持求解集合的交集(&)和并集(|)

栗子:

if __name__ == '__main__':
    settest = set([1, 2, 3])
    print('init:', settest)

    # 增
    settest.add('38')
    print('add str38:', settest)

    # 删
    settest.remove(2)
    print('remove 2:', settest)

    # 改
    pass

    # 查
    pass


    settest1 = {1, 2, 32, 5}
    settest2 = {4, 2, 3, 1, 32}

    # &
    print('&:', settest1 & settest2)

    # |
    print('|:', settest1 | settest2)

结果:

init: {1, 2, 3}
add str38: {1, 2, 3, '38'}
remove 2: {1, 3, '38'}
&: {32, 1, 2}
|: {32, 1, 2, 3, 4, 5}

五、拓展

1、list 和 tuple 异同点

(1)

2、dict 和 set 异同点

 

(SAW:Game Over!)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值