Python_Built-in Types_list.extend

来源:互联网 发布:mysql ibata文件 编辑:程序博客网 时间:2024/06/06 02:16

API文档:

array.extend(iterable)
Append items from iterable to the end of the array. If iterable is another array, it must have exactly the same type code; if not, TypeError will be raised. If iterable is not an array, it must be iterable and its elements must be the right type to be appended to the array.

翻译文档:

      参数:

             iterable:为一个数组或是string等

      描述:

            将iterable数组或是字符串追加到array中末尾。

            如果iterable为一个其他类型,则返回一个TypeError异常。如果iterable为字符串或是数组、元组等,则追加到array末尾

例子:

#! /usr/bin/env python
#coding=utf-8

list1=['a','b','c']
list2=['h','j']
print list1

list1.extend(list2)
print list1

list2=[1,'2']
list1.extend(list2)
print list1

list1.extend(1)
print list1

输出:

['a', 'b', 'c']
['a', 'b', 'c', 'h', 'j']
['a', 'b', 'c', 'h', 'j', 1, '2']
Traceback (most recent call last):
  File "F:\pyWorkSpace\notes\API\array_extend.py", line 15, in <module>
    list1.extend(1)
TypeError: 'int' object is not iterable


原创粉丝点击