• 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']
  • 相关阅读:
    java
    java
    Java hashCode() 和 equals()
    Python可变参数*和**
    Hadoop Mapreduce分区、分组、二次排序
    Java 内部类
    java valueOf()函数
    java接口和抽象类
    Java instanceof运算符
    JAVA ==号和equals()的区别
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14156429.html
Copyright © 2020-2023  润新知