python数据类型(四)

dy6--数据类型(四)

回顾

  • 字符串所调用的功能都会生成一个新的东西
  • 列表的独有功能要记住

补充

  • 列表功能:

    • .reverse 反转

      info = ["1","2","3"]
      info.reverse()
      print(info)                         输出结果,3 2 1
    • .sort() 排序

      info = ["1","2","3"]
      info.sort(reverse=False)
      print(info)                          输出结果,1.2.3
  • 字典

    • none是python里面的空

    • .get[“k1”] 取值判断是否存在,存在输出,不存在返回none

      info = {"1":"2","3":"4"}
      print(info.get("k12211"))                 输出none
      print(info.get("k111111"),666)           输出none 666
    • .pop 删除

      info = {"1":"2","3":"4"}
      resu = info.pop("1")
      print(resu)
      print(info)
    • .update() 不存在就添加,存在就覆盖

    • 判断字符,是否在列表里面用in,元组也可以,字典中默认是按照键是否存在,判断值需要循环判断(用.values),也可以强制转换成列表

      实际操作题
      要求,判断用户输入字符串中是否有列表中的元素,如果有输出对不起
      方式一:
      info = ["1","2","3",]
      c = input("请输入:")
      for i in info:
          if i in c :
              print("对不起")
              break
      方式二:
      info = ["1","2","3",]
      c = input("请输入:")
      s = True
      for i in info:
          if i in c :
              s = False
              break
      if s == True:
          print("成功")
      else:
          print("对不起")

一,集合

  • 集合是无序的不可重复的列表

    v = {1,2,3,34,45,}            集合的格式
    #v = {}                       是空字典
    #v = set()                   是空集合 

1.独有功能

  • .add 添加

    v = {1,2,3,4,5,}
    v.add(6)
    print(v)
  • .discard() 删除

    v = {1,2,3,4,5,}
    v.discard(2)
    print(v)
  • .pop() 随机删除一个

    v = {1,2,3,4,5,}
    v.pop
    print(v)
  • .update() 批量添加

    v = {1,2,3,4,5,}
    v.update(11,22,33,)
    print(v)
  • v = {1,2,3,4,5,}
    d = v.intersection({2,3,4,})             交集
    print(d)
  • v = {1,2,3,4,5,}
    d = v.union({2,3,4,})                     并集
    print(d)
  • v = {1,2,3,4,5,}
    d = v.difference({2,3,4,})               差集
    print(d)

2.公用功能

  • len
  • for循环

3.嵌套

  • 字典,列表,集合,不可以在集合内,不可以做字典的k

    哈希算法:可变值在通过哈希算法,内存处理较快

二,内存相关

2. 内存相关

  • 示例一

    v1 = [11,22,33]
    v2 = [11,22,33]
    
    v1 = 666
    v2 = 666
    
    v1 = "asdf"
    v2 = "asdf"
    
    # 按理 v1 和 v2 应该是不同的内存地址。特殊:
    1. 整型:  -5 ~ 256 
    2. 字符串:"alex",'asfasd asdf asdf d_asdf '       ----"f_*" * 3  - 重新开辟内存。
  • 示例二:

    v1 = [11,22,33,44]
    v1 = [11,22,33]
  • 示例三:

    v1 = [11,22,33]
    v2 = v1 
    
    # 练习1 (内部修改)
    v1 = [11,22,33]
    v2 = v1 
    v1.append(666)
    print(v2) # 含 666
    
    # 练习2:(赋值)
    v1 = [11,22,33]
    v2 = v1 
    v1 = [1,2,3,4]
    print(v2)
    
    # 练习3:(重新赋值)
    v1 = 'alex'
    v2 = v1
    v1 = 'oldboy'
    print(v2)
  • 示例四

    v = [1,2,3]
    values = [11,22,v]
    
    # 练习1:
    """
    v.append(9)
    print(values) # [11,22,[1,2,3,9]]
    """
    # 练习2:
    """
    values[2].append(999)
    print(v) # [1, 2, 3, 999]
    """
    # 练习3:
    """
    v = 999
    print(values) # [11, 22, [1, 2, 3]]
    """
    # 练习4:
    values[2] = 666
    print(v) # [1, 2, 3]
  • 示例五

    v1 = [1,2]
    v2 = [2,3]
    
    v3 = [11,22,v1,v2,v1]
    
  • 更改内存地址不变,重新赋值内存地址变化

  • 查看内存地址

    v1 = 234
    v2 = "eee"
    print(id(v1),id(v2))
  • python数据存储机制:对于数字python认为-5到256,不会重新开辟新的内存地址,常用字符串不会重新开辟,字符串里面包含了非字母数字下划线会重新开辟。

  • “==”比较的是值,is比较的是地址

猜你喜欢

转载自www.cnblogs.com/ffmmxx/p/10648895.html