zoukankan      html  css  js  c++  java
  • Python当中list列表的使用(创建列表,删除列表元素,添加列表元素,插入列表元素)

    程序如下:

    #这里我们将进行列表的学习,这个列表不能和R当中的列表弄混了
    classmate=['bob','Python','Java']
    b=["wife","mother"]
    print(len(classmate))
    #下面开始进行元素的访问,python当中的首元素是0,而不是R当中的1.
    print(classmate[0])
    print(classmate[0:])
    print(classmate[1:2])
    print("print the last element:")
    print(classmate[-1])
    print("
    print the second last element:")
    print(classmate[-2])
    print("
    print the third last element:")
    print(classmate[-3])
    print("
    let us append some elements to the end of the list:")
    classmate.append('adding element')
    print(classmate[-1])
    print("
    let us print all the list!:")
    print(classmate)
    print("
    let us delete the last element:")
    classmate.pop()
    print(classmate)
    print("
    let us delete the second element:")
    #delete the element we want
    classmate.pop(1)
    print(classmate)
    print("
    let us insert somthing to the second position:")
    #the insert position is our reference's position
    classmate.insert(1,"something")
    print(classmate)
    print("
    insert a list to our list:")
    classmate.insert(1,["insertelement1","insertelement2"])
    print(classmate)
    print("
    let us get the second list's seconde element:")
    print(classmate[1][1])

    linux下运行结果如下:

    geeksongs@DESKTOP-V7FKNMA:~/code$ python3 list.py
    3
    bob
    ['bob', 'Python', 'Java']
    ['Python']
    print the last element:
    Java
    
    print the second last element:
    Python
    
    print the third last element:
    bob
    
    let us append some elements to the end of the list:
    adding element
    
    let us print all the list!:
    ['bob', 'Python', 'Java', 'adding element']
    
    let us delete the last element:
    ['bob', 'Python', 'Java']
    
    let us delete the second element:
    ['bob', 'Java']
    
    let us insert somthing to the second position:
    ['bob', 'something', 'Java']
    
    insert a list to our list:
    ['bob', ['insertelement1', 'insertelement2'], 'something', 'Java']
    
    let us get the second list's seconde element:
    insertelement2

    得解也。

  • 相关阅读:
    LNMP笔记:解决mail函数不能发送邮件
    OPENCART记录账户密码
    Nginx 0.8.5版本access.log日志分析shell命令
    几个查询信息的api接口
    chart 图表无法在显示
    RegularExpressionValidator
    用户 'WANGYACONG\ASPNET' 登录失败
    'IIS APPPOOL\ASP.NET V4.0' 登录失败
    正则表达式(转载)
    不小心删除了默认数据库的恢复方法
  • 原文地址:https://www.cnblogs.com/geeksongs/p/12389402.html
Copyright © 2011-2022 走看看