• 字符串、列表、元组、字典每个常用的5个方法?


    # 字符串
    words = "today is a wonderfulday"
    print(words.strip('today'))      # 如果strip方法指定一个值的话,那么会去掉这两个值
    print(words.count('a'))          # 统计字符串出现的次数
    print(words.index('is'))         # 找下标
    print(words.index('z'))          # 找下标如果元素不找不到的话,会报错
    print(words.find('z'))           # 找下标,如果元素找不到的话,返回-1
    print(words.replace('day','DAY'))# 字符串替换
    #
    # # 列表
    sample_list = ['a', 1, ('a', 'b')]  # 创建列表
    sample_list = ['a', 'b', 0, 1, 3]   #  Python列表操作
    value_start = sample_list[0]  # 得到列表中的某一个值
    end_value = sample_list[-1]  # 得到列表中的某一个值
    del sample_list[0]  # 删除列表的第一个值
    sample_list[0:0] = ['sample value']  # 在列表中插入一个值
    
    # 元组
    #元组也是一个list,他和list的区别是元组的元素无法修改
    tuple1 = (2, 3, 4, 5, 6, 4, 7)
    print(type(tuple1))
    print(tuple1[:7])
    print(tuple1[: 5: -1])
    for i in range(6):
            print(tuple1[i])
    for i in tuple1:
            print(i)
    
    
    # 字典
    D.get(key, 0)       # 同dict[key],多了个没有则返回缺省值,0。[]没有则抛异常
    D.has_key(key)      # 有该键返回TRUE,否则FALSE
    D.keys()            # 返回字典键的列表
    D.clear()           # 清空字典,同del dict
    D.copy()            # 拷贝字典
  • 相关阅读:
    tkinter 类继承的三种方式
    tkinter 的两个例子
    python 测试驱动开发的简单例子
    python 播放 wav 文件
    Python 操作 MongoDB
    【转】如何拿到半数面试公司Offer——我的Python求职之路
    scrapy 保存到 sqlite3
    scrapy 爬取 useragent
    收集的User-Agent
    scrapy 登录
  • 原文地址:https://www.cnblogs.com/Rivend/p/12047458.html
Copyright © 2020-2023  润新知