Python 常用数据结构操作指南
字符串操作
-
大写和小写
s = "hello world" print(s.upper()) # HELLO WORLD print(s.lower()) # hello world
-
首字母大写
s = "hello world" print(s.capitalize()) # Hello world
-
每个单词首字母大写
s = "hello world" print(s.title()) # Hello World
-
替换字符串
s = "hello world" print(s.replace("world", "Python")) # hello Python
-
字符串分割
s = "hello world" print(s.split()) # ['hello', 'world']
-
字符串连接
words = ['hello', 'world'] print(" ".join(words)) # hello world
-
去除首尾空格
s = " hello world " print(s.strip()) # 'hello world'
-
去除左边空格
s = " hello world " print(s.lstrip()) # 'hello world '
-
去除右边空格
s = " hello world " print(s.rstrip()) # ' hello world'
-
判断前缀和后缀
s = "hello world" print(s.startswith("hello")) # True print(s.endswith("world")) # True
-
查找子字符串
s = "hello world" print(s.find("world")) # 6
-
获取子字符串的索引
s = "hello world" print(s.index("world")) # 6
-
检查字符串是否仅包含字母和数字
s = "hello123" print(s.isalnum()) # True
-
检查字符串是否仅包含字母
s = "hello" print(s.isalpha()) # True
-
检查字符串是否仅包含数字
s = "12345" print(s.isdigit()) # True
-
检查字符串是否仅包含小写字母
s = "hello" print(s.islower()) # True
-
检查字符串是否仅包含大写字母
s = "HELLO" print(s.isupper()) # True
-
翻转字符串
s = "hello" print(s[::-1]) # olleh
列表操作
-
添加元素
lst = [1, 2, 3] lst.append(4) # [1, 2, 3, 4] lst.extend([5, 6]) # [1, 2, 3, 4, 5, 6] print(lst)
-
插入元素
lst = [1, 2, 3] lst.insert(1, 'a') # [1, 'a', 2, 3] print(lst)
-
删除元素
lst = [1, 'a', 2, 3] lst.remove('a') # [1, 2, 3] print(lst) lst = [1, 2, 3, 4, 5, 6] lst.pop(1) # [1, 3, 4, 5, 6] print(lst)
-
列表切片
lst = [1, 2, 3, 4, 5, 6] print(lst[1:4]) # [2, 3, 4]
-
列表排序
lst = [3, 1, 4, 5, 2] lst.sort() # [1, 2, 3, 4, 5] print(lst) lst.sort(reverse=True) # [5, 4, 3, 2, 1] print(lst)
-
列表反转
lst = [1, 2, 3, 4, 5] lst.reverse() # [5, 4, 3, 2, 1] print(lst)
-
统计元素出现次数
lst = [1, 2, 2, 3, 3, 3] print(lst.count(3)) # 3
-
查找元素索引
lst = [1, 2, 3, 4, 5] print(lst.index(3)) # 2
-
复制列表
lst = [1, 2, 3] new_lst = lst.copy() print(new_lst) # [1, 2, 3]
-
清空列表
lst = [1, 2, 3] lst.clear() print(lst) # []
字典操作
-
添加/更新键值对
d = {'a': 1, 'b': 2} d['c'] = 3 # {'a': 1, 'b': 2, 'c': 3} d.update({'d': 4, 'e': 5}) # {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5} print(d)
-
删除键值对
d = {'a': 1, 'b': 2, 'c': 3} del d['a'] # {'b': 2, 'c': 3} print(d) d = {'b': 2, 'c': 3} value = d.pop('b') # {'c': 3}, value is 2 print(d, value)
-
获取键或值
d = {'a': 1, 'b': 2, 'c': 3} keys = d.keys() # dict_keys(['a', 'b', 'c']) values = d.values() # dict_values([1, 2, 3]) items = d.items() # dict_items([('a', 1), ('b', 2), ('c', 3)]) print(keys, values, items)
-
查找键值对
d = {'a': 1, 'b': 2, 'c': 3} value = d.get('c', None) # 3 print(value)
-
字典合并
d1 = {'a': 1, 'b': 2} d2 = {'c': 3, 'd': 4} d1.update(d2) # {'a': 1, 'b': 2, 'c': 3, 'd': 4} print(d1)
-
字典迭代
d = {'a': 1, 'b': 2, 'c': 3} for key, value in d.items(): print(key, value)
-
字典键排序
d = {'b': 2, 'a': 1, 'c': 3} sorted_keys = sorted(d.keys()) # ['a', 'b', 'c'] print(sorted_keys)
-
字典值排序
d = {'b': 2, 'a': 1, 'c': 3} sorted_values = sorted(d.values()) # [1, 2, 3] print(sorted_values)
集合操作
-
添加元素
s = {1, 2, 3} s.add(4) # {1, 2, 3, 4} print(s)
-
删除元素
s = {1, 2, 3, 4} s.remove(1) # {2, 3, 4} print(s) s = {2, 3, 4} s.discard(5) # {2, 3, 4}, no error if 5 is not present print(s) s = {2, 3, 4} s.pop() # {3, 4}, removes and returns an arbitrary element print(s)
-
集合运算
s1 = {1, 2, 3} s2 = {3, 4, 5} union = s1.union(s2) # {1, 2, 3, 4, 5} intersection = s1.intersection(s2) # {3} difference = s1.difference(s2) # {1, 2} print(union, intersection, difference)
-
检查子集和超集
s1 = {1, 2, 3} s2 = {1, 2} is_subset = s2.issubset(s1) # True is_superset = s1.issuperset(s2) # True print(is_subset, is_superset)
-
集合拷贝
s = {1, 2, 3} s_copy = s.copy() print(s_copy) # {1, 2, 3}
-
清空集合
s = {1, 2, 3} s.clear() print(s) # set()
-
集合差集更新
s1 = {1, 2, 3} s2 = {3, 4, 5} s1.difference_update(s2) # s1 is now {1, 2} print(s1)
-
集合对称差集
s1 = {1, 2, 3} s2 = {3, 4, 5} sym_diff = s1.symmetric_difference(s2) # {1, 2, 4, 5} print(sym_diff)
-
集合对称差集更新
s1 = {1, 2, 3} s2 = {3, 4, 5} s1.symmetric_difference_update(s2) # s1 is now {1, 2, 4, 5} print(s1)
-
集合对称差集
s1 = {1, 2, 3} s2 = {3, 4, 5} sym_diff = s1.symmetric_difference(s2) # {1, 2, 4, 5} print(sym_diff)
-
集合对称差集更新
s1 = {1, 2, 3} s2 = {3, 4, 5} s1.symmetric_difference_update(s2) # s1 is now {1, 2, 4, 5} print(s1)