Python 常用数据结构操作指南

Python 常用数据结构操作指南

字符串操作

  1. 大写和小写

    s = "hello world"
    print(s.upper())  # HELLO WORLD
    print(s.lower())  # hello world
    
  2. 首字母大写

    s = "hello world"
    print(s.capitalize())  # Hello world
    
  3. 每个单词首字母大写

    s = "hello world"
    print(s.title())  # Hello World
    
  4. 替换字符串

    s = "hello world"
    print(s.replace("world", "Python"))  # hello Python
    
  5. 字符串分割

    s = "hello world"
    print(s.split())  # ['hello', 'world']
    
  6. 字符串连接

    words = ['hello', 'world']
    print(" ".join(words))  # hello world
    
  7. 去除首尾空格

    s = "  hello world  "
    print(s.strip())  # 'hello world'
    
  8. 去除左边空格

    s = "  hello world  "
    print(s.lstrip())  # 'hello world  '
    
  9. 去除右边空格

    s = "  hello world  "
    print(s.rstrip())  # '  hello world'
    
  10. 判断前缀和后缀

    s = "hello world"
    print(s.startswith("hello"))  # True
    print(s.endswith("world"))    # True
    
  11. 查找子字符串

    s = "hello world"
    print(s.find("world"))  # 6
    
  12. 获取子字符串的索引

    s = "hello world"
    print(s.index("world"))  # 6
    
  13. 检查字符串是否仅包含字母和数字

    s = "hello123"
    print(s.isalnum())  # True
    
  14. 检查字符串是否仅包含字母

    s = "hello"
    print(s.isalpha())  # True
    
  15. 检查字符串是否仅包含数字

    s = "12345"
    print(s.isdigit())  # True
    
  16. 检查字符串是否仅包含小写字母

    s = "hello"
    print(s.islower())  # True
    
  17. 检查字符串是否仅包含大写字母

    s = "HELLO"
    print(s.isupper())  # True
    
  18. 翻转字符串

    s = "hello"
    print(s[::-1])  # olleh
    

列表操作

  1. 添加元素

    lst = [1, 2, 3]
    lst.append(4)  # [1, 2, 3, 4]
    lst.extend([5, 6])  # [1, 2, 3, 4, 5, 6]
    print(lst)
    
  2. 插入元素

    lst = [1, 2, 3]
    lst.insert(1, 'a')  # [1, 'a', 2, 3]
    print(lst)
    
  3. 删除元素

    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)
    
  4. 列表切片

    lst = [1, 2, 3, 4, 5, 6]
    print(lst[1:4])  # [2, 3, 4]
    
  5. 列表排序

    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)
    
  6. 列表反转

    lst = [1, 2, 3, 4, 5]
    lst.reverse()  # [5, 4, 3, 2, 1]
    print(lst)
    
  7. 统计元素出现次数

    lst = [1, 2, 2, 3, 3, 3]
    print(lst.count(3))  # 3
    
  8. 查找元素索引

    lst = [1, 2, 3, 4, 5]
    print(lst.index(3))  # 2
    
  9. 复制列表

    lst = [1, 2, 3]
    new_lst = lst.copy()
    print(new_lst)  # [1, 2, 3]
    
  10. 清空列表

    lst = [1, 2, 3]
    lst.clear()
    print(lst)  # []
    

字典操作

  1. 添加/更新键值对

    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)
    
  2. 删除键值对

    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)
    
  3. 获取键或值

    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)
    
  4. 查找键值对

    d = {'a': 1, 'b': 2, 'c': 3}
    value = d.get('c', None)  # 3
    print(value)
    
  5. 字典合并

    d1 = {'a': 1, 'b': 2}
    d2 = {'c': 3, 'd': 4}
    d1.update(d2)  # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    print(d1)
    
  6. 字典迭代

    d = {'a': 1, 'b': 2, 'c': 3}
    for key, value in d.items():
        print(key, value)
    
  7. 字典键排序

    d = {'b': 2, 'a': 1, 'c': 3}
    sorted_keys = sorted(d.keys())  # ['a', 'b', 'c']
    print(sorted_keys)
    
  8. 字典值排序

    d = {'b': 2, 'a': 1, 'c': 3}
    sorted_values = sorted(d.values())  # [1, 2, 3]
    print(sorted_values)
    

集合操作

  1. 添加元素

    s = {1, 2, 3}
    s.add(4)  # {1, 2, 3, 4}
    print(s)
    
  2. 删除元素

    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)
    
    
  3. 集合运算

    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)
    
  4. 检查子集和超集

    s1 = {1, 2, 3}
    s2 = {1, 2}
    is_subset = s2.issubset(s1)  # True
    is_superset = s1.issuperset(s2)  # True
    print(is_subset, is_superset)
    
  5. 集合拷贝

    s = {1, 2, 3}
    s_copy = s.copy()
    print(s_copy)  # {1, 2, 3}
    
  6. 清空集合

    s = {1, 2, 3}
    s.clear()
    print(s)  # set()
    
  7. 集合差集更新

    s1 = {1, 2, 3}
    s2 = {3, 4, 5}
    s1.difference_update(s2)  # s1 is now {1, 2}
    print(s1)
    
  8. 集合对称差集

    s1 = {1, 2, 3}
    s2 = {3, 4, 5}
    sym_diff = s1.symmetric_difference(s2)  # {1, 2, 4, 5}
    print(sym_diff)
    
  9. 集合对称差集更新

    s1 = {1, 2, 3}
    s2 = {3, 4, 5}
    s1.symmetric_difference_update(s2)  # s1 is now {1, 2, 4, 5}
    print(s1)
    
  10. 集合对称差集

    s1 = {1, 2, 3}
    s2 = {3, 4, 5}
    sym_diff = s1.symmetric_difference(s2)  # {1, 2, 4, 5}
    print(sym_diff)
    
  11. 集合对称差集更新

    s1 = {1, 2, 3}
    s2 = {3, 4, 5}
    s1.symmetric_difference_update(s2)  # s1 is now {1, 2, 4, 5}
    print(s1)
    
  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机智的小神仙儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值