• list列表基本操作1


    #列表的切片
    lst = ["abc","bcd","cde","def","deg"]
    print(lst[0:3])#['abc', 'bcd', 'cde']
    print(lst[:3])#['abc', 'bcd', 'cde']
    print(lst[0::2])#['abc', 'cde', 'deg']
    print(lst[::-1])#['deg', 'def', 'cde', 'bcd', 'abc']
    print(lst[::-2])#['deg', 'cde', 'abc']
    #列表list和字符串stirng不一样
    lst_1 = ["abc","bcd","cde","def","deg"]
    lst_1[3] = "@@@" #可以修改成功
    str = "ABCDESF"
    str[2] = "@" #字符串禁止这样修改
    #列表可以直接在后边追加元素内容
    #方法一:用append()函数
    lst = []
    while 1:
        str = input("输入职工姓名,Q表示退出:")
        if(str.upper() == "Q"):
            break
        else:
            lst.append(str)
    print(lst)
    #方法二:指定位置插入insert(pos,data)
    lst.insert(1,"###")
    print(lst)
    #列表删除元素:pop(),remove,clear,del
    #pop()函数
    lst = ["abc","bcd","cde","def","deg"]
    str = lst.pop()#这里和C++数据结构不一样,这里的pop有返回值,默认删除的是最后一个元素
    print("删除的元素是:",str)#删除的元素是: deg
    print("删除后的列表是:",lst)#删除后的列表是: ['abc', 'bcd', 'cde', 'def']
    str1 = lst.pop(1) #按指定的位置删除元素,这里删除第二个元素
    print("删除的元素是:",str1)#删除的元素是: bcd
    print("删除后的列表是:",lst)#删除后的列表是: ['abc', 'cde', 'def']
    #remove()函数
    lst_1 = ["abc","bcd","cde","def","deg"]
    delData = input("你想删除的元素是:") #你想删除的元素是:bcd
    lst_1.remove(delData) #当删除的元素不存在的话,会报错
    print("删除后的列表是:",lst_1) #删除后的列表是: ['abc', 'cde', 'def', 'deg']
    #clear()清空列表
    lst_2 = ["abc","bcd","cde","def","deg"]
    print("请空前:",lst_2) #请空前: ['abc', 'bcd', 'cde', 'def', 'deg']
    lst_2.clear()
    print("清空后:",lst_2)#清空后: []
    #del 切片删除
    lst_3 = ["abc","bcd","cde","def","deg"]
    del lst_3[0:2]
    print("删除后的列表是:",lst_3)#删除后的列表是: ['cde', 'def', 'deg']
    #可以使用for循环来实现对列表的操作
    lst = ["abc","bcd","cde","def","deg"]
    for st in lst:
        print(st,end=" ")
    lst = [2,5,3,6,1,43,90,5,4]
    #统计元素5的个数
    count = lst.count(5) #元素5的个数是 : 2
    print("元素5的个数是 :",count)
    #升序排列
    lst.sort()
    print(lst) #[1, 2, 3, 4, 5, 5, 6, 43, 90]
    #降序排列
    lst.sort(reverse=True)
    print(lst) #[90, 43, 6, 5, 5, 4, 3, 2, 1]
    #列表的反转
    lst_1 = ["abc","bcd","cde","def","deg"]
    lst_1.reverse()
    print(lst_1) #['deg', 'def', 'cde', 'bcd', 'abc']

     上述的sort()是永久排序,只要执行完sort()列表就不会再变回以前的顺序了,所以就引出了临时排序sorted(list)

    lst = ["one","two","three","frou","five"]
    tmepLst = sorted(lst)
    print(lst)#['one', 'two', 'three', 'frou', 'five']
    print(tmepLst)#['five', 'frou', 'one', 'three', 'two']默认升序
    print(lst)#['one', 'two', 'three', 'frou', 'five']
    tempLstRever = sorted(lst,reverse=True) #修改参数reverse,可降序排列
    print(tempLstRever)#['two', 'three', 'one', 'frou', 'five']
    #倒序打印
    lst = ["one","two","three","frou","five"]
    print(lst)#['one', 'two', 'three', 'frou', 'five']
    lst.reverse()
    print(lst)#['five', 'frou', 'three', 'two', 'one']
    #用if来判断一个列表,当列表是空的,就返回False,列表不是空的就返回True
    lst = []
    if lst :
        print("不是空列表")
    else:
        print("是空列表")
    
    
  • 相关阅读:
    oracle之 关闭透明大页
    Mac下使用sublime Text打开隐藏目录
    win下gosublime配置ctag
    Qless 相关知识
    Redis 的 Lua 脚本支持
    HTTP TCP UDP Socket 关系的几个经典图
    golang rpc 简单范例
    golang fatal error: all goroutines are asleep
    JSON 解析的可抛弃
    golang label breaks
  • 原文地址:https://www.cnblogs.com/boost/p/13235912.html
Copyright © 2020-2023  润新知