• python中创建列表、向列表中添加元素


    >>> test1 = ["aaa","bbb","ccc"]
    >>> type(test1)
    <class 'list'>
    >>> len(test1)
    3
    >>> test1.append("xxx")  ## 追加
    >>> test1
    ['aaa', 'bbb', 'ccc', 'xxx']
    >>> test1.append("yyy")
    >>> test1
    ['aaa', 'bbb', 'ccc', 'xxx', 'yyy']
    >>> test1.append("mmm","nnn")
    Traceback (most recent call last):
      File "<pyshell#416>", line 1, in <module>
        test1.append("mmm","nnn")
    TypeError: append() takes exactly one argument (2 given)
    >>> test1.extend("mmm","nnn")
    Traceback (most recent call last):
      File "<pyshell#417>", line 1, in <module>
        test1.extend("mmm","nnn")
    TypeError: extend() takes exactly one argument (2 given)
    >>> test1.extend(["mmm","nnn"])  ## 列表的扩展,添加多个元素
    >>> test1
    ['aaa', 'bbb', 'ccc', 'xxx', 'yyy', 'mmm', 'nnn']
    >>> test1.insert(0,111)  ## 列表插入
    >>> test1
    [111, 'aaa', 'bbb', 'ccc', 'xxx', 'yyy', 'mmm', 'nnn']
    >>> test1.insert(1,222)
    >>> test1
    [111, 222, 'aaa', 'bbb', 'ccc', 'xxx', 'yyy', 'mmm', 'nnn']
    >>> test1.insert(5,3333)
    >>> test1
    [111, 222, 'aaa', 'bbb', 'ccc', 3333, 'xxx', 'yyy', 'mmm', 'nnn']
    >>> test1.insert(-1,888)
    >>> test1
    [111, 222, 'aaa', 'bbb', 'ccc', 3333, 'xxx', 'yyy', 'mmm', 888, 'nnn']
    >>> test1 = ["aaa","bbb","ccc"]
    >>> test2 = ["mmm",111,333]
    >>> test1 + test2
    ['aaa', 'bbb', 'ccc', 'mmm', 111, 333]
    >>> test2 + test1
    ['mmm', 111, 333, 'aaa', 'bbb', 'ccc']
  • 相关阅读:
    绝对定位position: absolute;
    加号选择器(ul>li + li)
    position: absolute;绝对定位水平居中问题
    nth-child 和 nth-of-type 的区别
    Scrapy Shell 待续。。。
    TypeError: write() argument must be str, not bytes
    ModuleNotFoundError :No module named 'win32api'
    scrapy 简介
    3月27下午(补交)
    软件工程作业二:需求分析
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14156429.html
Copyright © 2020-2023  润新知