python-列表类型-元组类型-字典类型-集合类型

一、列表类型

  1. 用途:记录多个值(同种属性)

  2. 定义方式:在[]内用逗号分隔开多个任意类型的值

    l = ['a','b','c'] #l = list('a','b','c')
    

    类型转换

    l = list('hello')
    l = list({'x':1,'y':2})
    print(l)
    
  3. 常用操作+内置方法

    优先掌握的操作

    1. 按索引取值(正向存取+反向存取):即可存也可以取

      l = ['a', 'b', 'c', 'd', 'e']
      print(l[0]) #a
      print(l[-1]) #e
      print(id(l))#id 2573892519880
      l[0] = 'A'
      print(id(l)) #id 2573892519880
      l[5] = 'dddddd'
      print(l) # 索引不存在报错
      
    2. 切片(顾头不顾尾,步长)

      l = ['a', 'b', 'c', 'd', 'e']
      print(l[1:4]) #
      print(l)
      
      
    3. 长度

      l = ['a', 'b', 'c', 'd', 'e']
      print(len(l)) #5
      
    4. 成员运算in 和not in

      l = ['a', 'b', 'c', 'd', 'e']
      print('a' in l) #True
      print('ssssss' not in l) #True
      
    5. 追加&插入

      l=['a','b','c','d','e']
      l.append(33)
      l.append(44)
      print(l) #['a', 'b', 'c', 'd', 'e', 33, 44]
      
      l.insert(0,11)
      print(l) #[11, 'a', 'b', 'c', 'd', 'e', 33, 44]
      
    6. 删除

      l=['a','b','c','d','e']
      del l[0]
      print(l) #['b', 'c', 'd', 'e']
      res=l.remove('b')
      print(l) #指定元素删除
      print(res) #remove没有返回值为None
      
      list2 = ['a', 'b', 'c', 'd', 'e']
      res = list2.pop(0)
      print(list2) #['b', 'c', 'd', 'e']
      print(res) #pop有返回值,值为a
      
      
    7. 循环

      l = ['a', 'b', 'c', 'd', 'e']
      for item in l:
          print(item)
      

      需要掌握的操作

    8. count

      l = ['a', 'b', 'a', 'c', 'd', 'e']
      print(l.count('a')) #2
      
    9. extend

      l = ['a', 'b', 'a', 'c', 'd', 'e']
      items = [1, 2, 3, 4, 5]
      for item in items:
          l.append(item)
      print(l) #['a', 'b', 'a', 'c', 'd', 'e', 1, 2, 3, 4, 5]
      
      #用extend方法将一个列表添加到另一个列表中
      l = ['a', 'b', 'a', 'c', 'd', 'e']
      items = [1, 2, 3, 4, 5]
      l.extend(items)
      print(l) #['a', 'b', 'a', 'c', 'd', 'e', 1, 2, 3, 4, 5]
      
    10. index

    ​```python
    l = ['a', 'b', 'a', 'c', 'd', 'e']
    print(l.index('a', 2, 5)) #在起始位置和结束位置中查找
    print(l.index(123)) #找一个没有的元素直接报错
    print('111' in l)  #False
    ​```
    
    1. reverse

      l = ['a', 'b', 'a', 'c', 'd', 'e']
      l.reverse()
      print(l) #['e', 'd', 'c', 'a', 'b', 'a']
      
      
    2. sort

      l = [10, -1, 3, 11, 9]
      l.sort()
      print(l) #从小到大排序[-1, 3, 9, 10, 11]
      l.sort(reverse=True)
      print(l) #从大到小排序[11, 10, 9, 3, -1]
      

      该类型总结:

      存多个值

      有序

      可变(1、可变:值变,id不变。可变不可hash 2、不可变:值变,id就变。不可变可hash)

      练习题

    #1.队列:先进先出
    l = []
    #入队
    l.append('first')
    l.append('second')
    l.append('third')
    print(l) #['first', 'second', 'third']
    # 出队
    print(l.pop(0)) #first
    print(l.pop(0)) #second
    print(l.pop(0)) #third
    
    #2.堆栈:后进先出
    # 2. 堆栈:后进先出
    l = []
    # 入栈
    l.append('first')
    l.append('second')
    l.append('third')
    print(l) #['first', 'second', 'third']
    # 出栈
    print(l.pop(-1)) #third
    print(l.pop(-1)) #second
    print(l.pop(-1)) #first
    #pop()和pop(-1)效果一样```
    

二、元组类型

  1. 用途:元组就是一个不可变的的列表

  2. 定义方式:在()内用逗号分隔开多个任意类型的元素

    t = (1, 2.2, 'aa', ('b', 'c'), ['a', 'b', 'c']) #t = tuple(...)
    print(type(t)) #<class 'tuple'>
    
    t=('a',)
    print(type(t)) #<class 'tuple'>
    print(t) #('a',)
    
  3. 类型转换

    t1 = tuple('hello')
    t2 = tuple([1, 2, 3])
    print(t1) #('h', 'e', 'l', 'l', 'o')
    print(t2) # (1, 2, 3)
    
  4. 常用操作+内置的方法

    优先掌握的操作

    1. 按照索引取值(正向取+反向取):只能取

      t = (1, 2.2, 'aa', ('b', 'c'), ['a', 'b', 'c'])
      print(t[0]) #1
      print(t[-1]) #['a', 'b', 'c']
      
    2. 切片(顾头不顾尾,步长)

      t = ('a', 'b', 'c', 'e', 'f')
      print(t[1:4]) #('b', 'c', 'e')
      
    3. 长度

      t = ('a', 'b', 'c', 'e', 'f')
      print(len(t)) #5
      
    4. 成员运算in和not in

      t = ('a', 'b', 'c', 'e', 'f')
      print('a' in t) #True
      
    5. 循环

      t = ('a', 'b', 'c', 'e', 'f')
      for item in t:
          print(item)
      
    6. index,count

      t = ('a', 'b', 'c', 'e', 'a', 'f')
      print(t.index('a', 1, 5)) #4
      print(t.count('a')) #2
      

      该类型总结

      存多个值

      有序

      不可变(1、可变:值变,id不变。可变不可hash 2、不可变:值变,id就变。不可变可hash)

      t = ('a', 111, ['b', 'c'])
      print(t)
      print(id(t))
      print(id(t[0]), id(t[1]), id(t[2]))
      t[2][0] = 'B'
      print(t)
      print(id(t))
      print(id(t[0]), id(t[1]), id(t[2]))
      

三、字典类型

  1. 用途:记录多个值,列表是索引对应值,而字典是key对应值,其中key对value有描述性的功能

  2. 定义方式:在{}用逗号分隔开多个元素,每个元素都是key:value的形式,其中key可以不可变类型,通常是字符串类型,而value可以是任意类型

    d = {1: 'aaa', 2.2: 'bbb', 'xxx': 'ccc', (1, 2, 3): 'dddd'} #d=dict(...)
    print(d[(1,2,3)]) #dddd
    
  3. 类转换

    d = dict(x=1, y=2, z=3)
    print(d) #{'x': 1, 'y': 2, 'z': 3}
    
    items = [('name', 'egon'), ('age', 18), ('gender', 'male')]
    d = {}
    for item in items:
        d[item[0]] = item[1]
    print(d) #将列表转换成字典 {'name': 'egon', 'age': 18, 'gender': 'male'}
    
    d = dict(items)
    print(d)
    
    #了解
    keys = ['name', 'age', 'gender', 'height', 'weight']
    d = {}
    for key in keys:
        d[key] = None
    print(d)#{'name': None, 'age': None, 'gender': None, 'height': None, 'weight': None}
    
    keys = ['name', 'age', 'gender', 'height', 'weight']
    d = {}
    d = {}.fromkeys(keys, None)
    print(d) #{'name': None, 'age': None, 'gender': None, 'height': None, 'weight': None}
    
  4. 常用操作+内置方法

    优先掌握的操作

    1. 按key存取值:可存可取

      dic = {'name': 'egon', 'age': 18}
      print
      print(dic.get('name')) #egon
      print(dic.get('xxx')) #None
      print(dic['xxxx']) #没有的key 报错
      dic['name']='EGON' #有key修改
      dic['gender']='male' #没有key添加
      print(dic) #{'name': 'EGON', 'age': 18, 'gender': 'male'}
      
    2. 长度len

      dic = {'name': 'egon', 'age': 18}
      print(dic)
      print(len(dic)) #2
      
    3. 成员运算in 和 not In :是以字典的key为准的

      dic = {'name': 'egon', 'age': 18}
      print('name' in dic) #True
      print('egon' in dic) #False
      
    4. 删除

      dic = {'name': 'egon', 'age': 18}
      del dic['name']
      print(dic) #{'age': 18}
      
      dic = {'name': 'egon', 'age': 18}
      res = dic.pop('name')
      print(dic) #{'age': 18}
      print(res) #egon pop删除有返回值
      
      res1 = dic.popitem()
      print(res1) #随机删除
      
    5. 键keys(),值values(),键值对items()

      #在python2
      >>> dic={'name':'egon','age':18}
      >>> dic.keys()
      ['age', 'name']
      >>> dic.values()
      [18, 'egon']
      >>> dic.items()
      [('age', 18), ('name', 'egon')]
      
      #在python3
      >>> dic={'name':'egon','age':18}
      >>> dic.keys()
      dict_keys(['name', 'age'])
      >>> dic.values()
      dict_values(['egon', 18])
      >>> dic.items()
      dict_items([('name', 'egon'), ('age', 18)])
      
      
    6. 循环

      dic={'name':'egon','age':18}
      for k,v in dic.items(): #k,v=('name', 'egon')
          print(k,v) 
      

      需要掌握的操作

    7. update

      dic = {'name': 'egon', 'age': 18}
      dic = {'name': 'egon', 'age': 18}
      dic.update({'age': 19, 'gender': 'male'})
      print(dic) #{'name': 'egon', 'age': 19, 'gender': 'male'}
      
    8. setdefault

      dic = {'name': 'egon', 'age': 18}
      # 当key存在时,不改原值,返回原值
      res = dic.setdefault('name', 'EGON')
      print(dic) #{'name': 'egon', 'age': 18}
      print(res) #返回原值egon
      
      # 当key不存在时,增加新值
      res = dic.setdefault('gender','male')
      print(dic) #{'name': 'egon', 'age': 18, 'gender': 'male'}
      print(res) #返回新值male
      
      

      该类型总结

      存多个值

      无序

      可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变可hash)

四、集合类型

```python
pythons = ['张铁蛋', '李铜淡', '王金蛋', '赵银胆', 'alex', 'kevin']
linuxs = ['oldboy', '张铁蛋', '赵银胆', 'alex', 'wxx']
res = []
for stu in pythons:
    if stu in linuxs:
        res.append(stu)
print(res) #['张铁蛋', '赵银胆', 'alex']
  1. 用途:I: 关系运算 II:去重

  2. 定义方式:在{}内用逗号分隔开多个元素,但是元素的特点是:

    # I: 集合内的元素必须是不可变类型
    # II: 集合内元素无序
    # III: 集合内元素不能重复
    s = {1, 'aaa', 2, } 
    print(s, type(s)) #{1, 'aaa', 2} <class 'set'>
    
    s = set()
    print(s, type(s)) #set() <class 'set'>
    
  3. 数据类型转换

    res = set('hello')
    print(res) #{'h', 'o', 'e', 'l'}
    res = set([1, 'a', 'b'])
    print(res) #{1, 'a', 'b'}
    
    
  4. 常用操作+内置的方法

    长度len

    成员运算in和not in

|合集:求所有报名的学生

pythons = {'张铁蛋','李铜淡','王金蛋','赵银胆','alex','kevin'}
linuxs = {'oldboy','张铁蛋','赵银胆','alex','wxx'}
print(pythons | linuxs)
  1. &交集:求同时报名两门课程的学生

    print(pythons & linuxs)
    
  2. -差集: 求只报名python课程的学员

    print(pythons - linuxs) #只报名pythons课程的学员
    print(linuxs - pythons) #只报名linux课程的学员
    
  3. ^对称差集:求没有同时报名两门课程的学生

    res = (pythons - linuxs) | (linuxs - pythons)
    res = pythons ^ linuxs
    print(res)
    
  4. ==

    s1 = {1, 2, 3}
    s2 = {3, 2, 1}
    print(s1 == s2) #True
    
  5. 父集:>= 子集:<=

    s1 = {1, 2, 3}
    s2 = {1, 2, 3, 4}
    print(s2 >= s1) #True
    print(s1 <= s2) #True
    

    需要掌握的

  6. update

    s1 = {1, 2, 3}
    s1.update({3, 4, 5, 6})
    print(s1) # {1, 2, 3, 4, 5, 6}
    
  7. pop

    s1 = {1, 'aa', 'bb', 3}
    print(s1.pop()) #1
    
  8. remove

    s1 = {1, 'aa', 'bb', 3}
    res = s1.remove('bbbbbb') #移除一个不存在的元素时会发生错误
    
  9. discard

    s1 = {1, 'aa', 'bb', 3}
    s1.discard('bbb')
    print(s1) #移除一个不存在的元素时不会发生错误
    s1.add(4)
    print(s1) #{1, 3, 4, 'bb', 'aa'}
    

    该类型总结

    存多个值

    无序

    可变(1、可变:值变,id不变。可变不可hash 2、不可变:值变,id就变。不可变可hash)

    集合的好处

    #1.集合的去重

    集合的局限性

    #1. 只能针对不可变类型
    #2. 不能保证原来的顺序

    names = ['egon', 'egon', 'egon', 'alex', 'alex', 'kevin']
    new_names = list(set(names))
    print(new_names) #['egon', 'kevin', 'alex'] #不能保证原来的顺序
    
    #列表去重
    l = [
        {'name': 'egon', 'age': 18, 'sex': 'male'},
        {'name': 'alex', 'age': 73, 'sex': 'male'},
        {'name': 'kevin', 'age': 20, 'sex': 'female'},
        {'name': 'egon', 'age': 18, 'sex': 'male'},
        {'name': 'egon', 'age': 18, 'sex': 'male'},
    ]
    new_l = []
    for dic in l:
        if dic not in new_l:
            new_l.append(dic)
    print(new_l)
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值