• Python学习笔记--列表


    import os
    import time
    
    
    # 在终端中显示跑马灯(滚动)文字。
    def print_scroll():
        content = '北 京 欢 迎 你 为 你 开 天 辟 地           '
        while True:
            # Windows清除屏幕上的输出
            os.system('cls')
            # macOS清除屏幕上的输出
            # os.system('clear')
            print(content)
            # 休眠0.2秒(200毫秒)
            time.sleep(0.2)
            content = content[1:] + content[0]
    
    
    def test_lists():
        items1 = [35, 12, 99, 68, 55, 87]
        items2 = [45, 8, 29]
        # 列表的拼接
        items3 = items1 + items2
        print(items3)  # [35, 12, 99, 68, 55, 87, 45, 8, 29]
        # 列表的重复
        items4 = ['hello'] * 3
        print(items4)  # ['hello', 'hello', 'hello']
        # 列表的成员运算
        print(100 in items3)  # False
        print('hello' in items4)  # True
        # 获取列表的长度(元素个数)
        size = len(items3)
        print(size)  # 9
        # 列表的索引
        print(items3[0], items3[-size])  # 35 35
        items3[-1] = 100
        print(items3[size - 1], items3[-1])  # 100 100
        # 列表的切片
        print(items3[:5])  # [35, 12, 99, 68, 55]
        print(items3[4:])  # [55, 87, 45, 8, 100]
        print(items3[-5:-7:-1])  # [55, 68]
        print(items3[::-2])  # [100, 45, 55, 99, 35]
    
        # 列表的比较运算
        items5 = [1, 2, 3, 4]
        items6 = list(range(1, 5))
        # 两个列表比较相等性比的是对应索引位置上的元素是否相等
        print(items5 == items6)  # True
        items7 = [3, 2, 1]
        # 两个列表比较大小比的是对应索引位置上的元素的大小
        print(items5 <= items7)  # True
    
    
    # 遍历列表
    def travel_lists():
        items = ['Python', 'Java', 'Go', 'Kotlin']
        # 方法一
        for i in range(len(items)):
            print(items[i])
        # 方法二
        for item in items:
            print(item)
    
    
    # 列表常用方法
    def lists_method():
        items = ['Python', 'Java', 'Go', 'Kotlin']
        # 使用append方法在列表尾部添加元素
        items.append('Swift')
        print(items)  # ['Python', 'Java', 'Go', 'Kotlin', 'Swift']
        # 使用insert方法在列表指定索引位置插入元素
        items.insert(2, 'SQL')
        print(items)  # ['Python', 'Java', 'SQL', 'Go', 'Kotlin', 'Swift']
        # 删除指定的元素
        # 若删除的元素不存在,会ValueError异常
        items.remove('Java')
        print(items)  # ['Python', 'SQL', 'Go', 'Kotlin', 'Swift']
        # 删除指定索引位置的元素
        items.pop(0)
        items.pop(len(items) - 1)
        print(items)  # ['SQL', 'Go', 'Kotlin']
        items.append('Kotlin')
        print(items)
        print("kotlin出现次数:", items.count('Kotlin'))
        # 清空列表中的元素
        items.clear()
        print(items)  # []
        items = ['Python', 'Java', 'Go', 'Kotlin', 'Python']
        items.sort()
        print(items)
        # 反转列表
        items.reverse()
        print(items)
    
    
    # 通过生成式创建列表
    def create_lists():
        # 创建一个由1到9的数字构成的列表
        items1 = []
        for x in range(1, 10):
            items1.append(x)
        print(items1)
        # 生成式
        items11 = [x for x in range(1, 10)]
        print(items11)
    
        # 创建一个由'hello world'中除空格和元音字母外的字符构成的列表
        items2 = []
        for x in 'hello world':
            if x not in ' aeiou':
                items2.append(x)
        print(items2)
        # 生成式
        items22 = [x for x in 'hello world' if x not in ' aeiou']
        print(items22)
    
        # 创建一个由个两个字符串中字符的笛卡尔积构成的列表
        items3 = []
        for x in 'ABC':
            for y in '12':
                items3.append(x + y)
        print(items3)
        # 生成式
        items33 = [x + y for x in 'ABC' for y in '12']
        print(items33)
    
    
    if __name__ == '__main__':
        # test_lists()
        # travel_lists()
        # lists_method()
        create_lists()
    

      

  • 相关阅读:
    Python爬虫之记录一次下载验证码的尝试
    Python之学会测试,让开发更加高效(一)
    NLP(二十八)多标签文本分类
    NLP(二十七)开放领域的三元组抽取的一次尝试
    NLP(二十六)限定领域的三元组抽取的一次尝试
    Numpy之数据保存与读取
    TortoiseGit的首次使用
    NLP(二十五)实现ALBERT+Bi-LSTM+CRF模型
    NLP(二十四)利用ALBERT实现命名实体识别
    NLP(二十三)序列标注算法评估模块seqeval的使用
  • 原文地址:https://www.cnblogs.com/jifeng0902/p/14692085.html
Copyright © 2020-2023  润新知