随堂笔记

字符串总结:

 字符串的拼接 ''.join(可迭代对象)

               + str  返回一个新字符串

字符串的分割   split partition 

            ().split('',maxsplit=number)

().rsplit('',maxsplit=number)

            ().splitlines()

            ().partition('')返回三部分 中间部分为分隔符

字符串大小写 

           (可迭代对象).upper()

  (可迭代对象).lower()

  (可迭代对象).swapcase()

字符串的修改

().replace('a')  

a = 'www.magedu.com'

            a.replace('www','ll')

output: 'll.magedu.com'      

strip([chars]) -> str

lstrip([chars]) -> str

       从字符串'两端'去除指定的字符集chars中的所有字符

       如果chars没有指定,去除'两端的'空白字符

             a.strip(' ')

a = '  00wer234 '

a.strip().lstrip('0')

集set 

  可变的 无序的 不重复的元素集合 非线性的

  s = set ()   new empty object

  s = set (range(5))

  s = {} 特例 定义字典%%%%%

  s = {1,2}  set

  s = set()  定义集

  s1 = set('a'*5)  返回{'a'}

  s2 = {1,2,3,1,2,3} 返回{1,2,3}

  s3 = {1,2,'aabc',1}

  s3 .add (3)  #元组

  s3 .add('abcde') #字符串

  s3 .add ([1]) 不支持列表

  s3.add({2}) 不支持set

  s4 = {1,'a',('a',1)}

hash() 内建函数  O(1)

   hash(1)  hash('a') hash((1,'a'))

   元素看能不能hash 用函数试  set dic 列表不可hash

set增加 

    add 增加一个元素到set 

updata 合并其他元素到set集合中来

参数others必须是可迭代对象

就地修改

 s2 = {(1,2),1,3,'ab','aabc'}

 s2.add(1,2,3)

 s2.updata(*abc(可迭代多个对象))

 s2 .updata([1,2,3]、{1,3,2})

 s2.remove(value) 没有该值 提示:key error 独一无二(unique、key) 就地修改

 s2.discard 从set中移除一个元素 元素不存在 不报错 

 s2.pop() 随机弹出一个元素出来 该元素消失 set减少

 set 不需要修改 只能增加或者删除 都是单一元素 不重复的元素集合

      非线性结构无法索引 不支持查询

 遍历(只要是数据结构都是可以遍历) 可以迭代所有元素 

 成员运算符  in   

  小规模的数据结构 列表小规模 用in遍历可以但是规模大 会很慢   %%%%%%%%%%%%%%%%%%%%%%%%%%

   规模大可以用set可以频繁使用in

 set 最大的特点 依靠hash 用空间换时间 

   

  线性结构的查询时间复杂度为 O(n)

  可变的类型都不可以hash

  


猜你喜欢

转载自blog.51cto.com/12950054/2161715