Python數(shù)據(jù)結(jié)構(gòu)常用方法

  1. Integer類型
1. a + b
2. a - b
3. a * b
4. a ** b  
    a的b次方,同pow(a, b)
5. a / b
    a除以b,5 / 2 = 2.5
6. a // b
    a除以b,去掉余數(shù), 5 // 2 = 2, 5.1 // 2 == 2.0
7. abs(a)
    取絕對值
8. divmod(a, b)
    以二元組形式返回商和a對b的取余數(shù)(a%b)
    divmod(5, 2) = (2, 5)
9. pow(a, b)
    a的b次方
10. pow(a, b, c)
    a的b次方,然后對c取余 (a ** b) % c
11. round(a, b)
    對a進(jìn)行四舍五入,保留b位小數(shù)點(diǎn)
12. bin(a)
      返回a的二進(jìn)制表示 bin(255) = "0b11111111"
13. hex(a)
      返回a的十六進(jìn)制表示 hex(255) = "0xff"
14. int(a)
      可以把str, float, 八進(jìn)制,十六進(jìn)制,二進(jìn)制轉(zhuǎn)化為10進(jìn)制整數(shù)
      int(0x1111) = 15
      int(10.999) = 10
      int("10") = 10
15. oct(a)
    返回a的八進(jìn)制表示 oct(10) = 0o12
16. sys.float_info.epsilon
      機(jī)器可區(qū)分的浮點(diǎn)數(shù)的最小區(qū)別
  1. str字符串
str = "hello world"  
1. for c in str:
    iterable
2. str[0:2] = "he"
    str[0:4:2] = "hl"
    str[2:] = "llo world"
    str[:2] = "he"
    str[::2] = "hlowrd"
    str[-2] = "l"
3. str.capitalize()
    首字母大寫,其他字母小寫 "hELLo".capitalize() = "Hello"
4. str.count(c, start, end)
    str中,子字符串c出現(xiàn)的次數(shù)
5. str.endswith(c, start, end)
    str或者str的分片是否以c結(jié)尾
6. str.startswith(c, start, end)
    str或者str的分片是否以c開始
7. str.expandtabs(size)
    str中的制表符使用8個(gè)或者size個(gè)空格替換,"\ta".expandtabs(10)
8. str.find(c, start, end)
    返回c第一次在str中出現(xiàn)的位置,沒有找到返回-1
9. str.format()
    格式化
    "{0} is a {1}".format("she", "girl")
    "{0[0]} is a {0[1]}".format(("She", "girl"))
    "{who} is a {what}".format(who="She", what="girl")
    格式化的時(shí)候,!s對應(yīng)__str__, !a對應(yīng)__ascii__, !r對應(yīng)__repr__
10. str.index(c, start, end)
    同str.find(),但是找不到會拋出ValueError
11. str.isalnum()
    str非空,且每個(gè)字符都為字母數(shù)字
12. str.isalpha()
    str非空,且每個(gè)字符都為字母
13. str.isnumeric()
          True: Unicode數(shù)字,全角數(shù)字(雙字節(jié)),羅馬數(shù)字,漢字?jǐn)?shù)字
          False: 無
          Error: byte數(shù)字(單字節(jié))
      str.isdecimal()
          True: Unicode數(shù)字,,全角數(shù)字(雙字節(jié))
          False: 羅馬數(shù)字,漢字?jǐn)?shù)字
          Error: byte數(shù)字(單字節(jié))
      str.isdigit()
          True: Unicode數(shù)字,byte數(shù)字(單字節(jié)),全角數(shù)字(雙字節(jié)),羅馬數(shù)字
          False: 漢字?jǐn)?shù)字
          Error: 無
14. str.islower()
      str非空,且都是小寫
      str.isupper()
      str非空,且都是大寫
      str.istitle()
      str非空,且首字母大寫,其他小寫
15. str.join(seq)
      "-".join(("1991", "8", "6")) = 1991-8-6
16. str.ljust(width, c)      左對齊,用c填充
      str.rjust(width, c)      右對齊,用c填充
      str.center(width, c)    中間對齊,用c填充
17. str.replace(t, u, n)
      將字符串中的t換成u,最多替換n次
18. str.split(c, n)
      將字符串以c分割,最多分割n次
19. str.strip(chars)
      去除str前后的空白字符(或者chars中的字符)
20. str.swapcase()
      交換字符串的大小寫
21. str.title()
      str首字母大寫,其他字母小寫
22. str.lower()    小寫
      str.upper()    大寫
23. str.zfill(w)  
      用0填充str到長度w
24. ord(c)
      返回字符c的ASCII碼
25. chr(ascii)
      返回ascii對應(yīng)的字符
  1. list列表
lst = list()
1. lst.append(x)
2. lst.count(x)
3. lst.extend(lst2)
    相當(dāng)于 lst += lst2
4. lst.index(x, start, end)
    x首次出現(xiàn)的位置, 不存在拋出ValueError
5. lst.insert(index, x)
    在index處插入x,如果index > len(lst),則在末尾插入
6. lst.remove(x)
    刪除首次出現(xiàn)的x,如果不存在拋出ValueError
7. lst.pop(i)
    刪除i處的數(shù)據(jù),如果i >= len(lst), 拋出IndexError
8. lst.pop()
    刪除最后一個(gè)
9. lst.sort(reverse=True, key=lambda x: x%3)
10. 列表內(nèi)涵
      lst = [x for x in range(1900, 2017) if (x % 4 ==0 and x % 100 != 0) or (x % 400 == 0)]
  1. set集合類型
s = set()
1. s.add(x)
2. s.clear()
3. s.copy()
    淺拷貝
4. s.difference(t)  s-t,在s中,但不在t中的
5. s.difference_update(t)
    移除s中的既在t中,也在s中的 {1,2}.difference_update({1, 5}) = {2}
6. s.intersection(t) s&t,在s中,也在t中
    s.intersection_update(t) 使得s中,只包含s與t的交集 {1,2}.intersetion_update({1,5}) = {1}
7. s.union(t) s | t, 并集,s與t的并集
8. s.update(t) 
    使得s包含s與t的并集
9. s.symmetric_difference(t)
    返回不同時(shí)在s和t中的集合 {1, 2, 3}.symmetric_difference({1, 4, 5})  = {2, 3, 4, 5}
10. s.symmetric_difference_update(t)
    使得s等于不同時(shí)在s和t中的所有項(xiàng)的集合
11. s.isdisjoint(t)
    如果s和t沒有相同的項(xiàng),返回True
12. s.issuperset(t) s是否是t的超集
13. s.issubset(t)  s是否是t的子集
14. s.pop()
      移除s的一個(gè)隨機(jī)項(xiàng),(第一項(xiàng))
15. s.remove(x)
      移除x
  1. 字典dict
d = dict() || d = {}
1. d.clear()
2. d.copy()
    淺拷貝
3. d.fromkeys(list, value)
    添加list數(shù)據(jù)作為key,值為value || None
4. d.get(k)
    如果不存在k,返回None
5. d.get(k, v)
    如果不存在k,返回v
6. d.items()
    返回(key, value)值對
    for key, value in d.items():
      print(key, value)
7. d.keys()
    返回所有的鍵
8. d.pop(k)
    如果k在d中,返回并移除,如果不存在,拋出KeyError
9. d.pop(k, v)
    如果k在d中,返回并移除,不存在,返回v
10. d.popitem()
    隨機(jī)移除并返回一個(gè)鍵值對,如果為空,拋出KeyError
11. d.setdefaut(k, v)
    如果存在,返回d[k], 如果不存在,設(shè)定d[k] = v
12. d.update(a)
    更新字典,a可以是字典,元祖對,或者key=value
13. d.values(a)
    獲取字典所有的值
14. 字典內(nèi)涵:
      ``` 
      def  asc(start, end):
          while(ord(start) <= ord(end)):
              yield start
              start = chr(ord(start) + 1)    
      d = {k: "we are default" in asc('a', 'c')}
      ```
      ```
      m = lambda x: [v for v in asc(x, "z")]
      d = {k: m(k) for k in asc("a", "c")}
      ```
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容