Python 列表 Append() 示例
Append 方法是什么 Python?
附加函数 Python 帮助将新元素插入到基础列表中。项目将附加到现有列表的右侧。append 方法接受单个参数并将列表的大小增加 1。
下图说明了 Python的附加功能:
语法:
List.append(object)
请注意: 这里,对象可以是整数、字符串或浮点数。append 函数不返回任何值或列表。它只是修改和增加基础列表。
如何利用附加功能创建 Python 清单?
A Python 可以使用两种方法创建和填充列表。
- 在第一种方法中,使用列表推导。
- 第二种方法是利用 Append 函数和“for循环“。通过这种方法,您可以创建一个使用 for 循环和附加的用户定义函数。
看一下下面使用第二种方法的示例:-
import math def calc_sqr_root(b_list): bop=[] for number in b_list: bop.append(math.sqrt(number)) return bop base_list=(4,9,100,25,36,49,81) print("the Squared number list is as follows",base_list) calc_sqr_root(base_list) print("the numbers with square root in list is as follows",calc_sqr_root(base_list))
输出:
the Squared number list is as follows (4, 9, 100, 25, 36, 49, 81) the numbers with square root in the list is as follows [2.0, 3.0, 10.0, 5.0, 6.0, 7.0, 9.0]
代码说明:
- 使用方括号定义一个空列表。
- for循环和append函数在用户定义的define函数下一起使用。
- 它从头开始填充一个空列表。
- 它利用 for 循环逐个插入单个项目。
- 附加列表用于返回用户定义函数的值。
下面是使用第一种方法的示例:
计费示例:
Python 码:
import math def calc_sqr_root(b_list): return [math.sqrt(number) for number in b_list] base_list=(4,9,100,25,36,49,81) print("the Squared number list is as follows",base_list) calc_sqr_root(base_list) print("the numbers with square root in list is as follows",calc_sqr_root(base_list))
输出:
the Squared number list is as follows (4, 9, 100, 25, 36, 49, 81) the numbers with a square root in the list are as follows [2.0, 3.0, 10.0, 5.0, 6.0, 7.0, 9.0]
代码说明:
- 您可以使用列表推导作为替代来附加函数 Python 从头开始填写清单。
- 它有助于从一开始就填充列表。
- 自定义列表下的列表推导有助于填充原始列表中的元素。
- 与 for 循环和 append 函数的组合相比,它有助于优化数据处理。
Append 方法如何工作?
Append 功能以以下方式提供帮助:-
- 中的 Append 函数 Python 将对象添加到基础列表。
- 它将对象作为参数并将其放置在下一个空白处。
- 列表项是有序的,可以使用索引进行访问。
下面的图片显示了元素的索引:
让我们看下面的例子,将元素添加到基础列表中。
Python 计费示例:
baselist = ['P','Y','3','4.2','T'] print("The original list", baselist) print("At index 0:", baselist[0]) print("At index 3:",baselist[3]) baselist.append('n') print("The appended list", baselist) print("At index 5 post append:",baselist[5])
输出:
The original list ['P', 'Y', '3', '4.2', 'T'] At index 0: P At index 3: 4.2 The appended list ['P', 'Y', '3', '4.2', 'T', 'n'] At index 5 post append: n
代码说明:
- Append 函数将对象的对象数据类型添加到列表中可用的保留空间。
- Python 列表是可迭代序列,可以保存不同的数据类型和对象。
append 函数在索引 5 处添加一个新元素,如下所示:-
如何在不使用 Append 的情况下将元素插入列表?
如果不使用附加函数,程序员可以通过应用两步过程将元素添加到列表中。
使用 Len 函数,您可以找出列表中最后一个项目的长度。将标识的空白空间分配给新对象。以下示例说明了这一概念:–
计费示例:
base_list=[2,4,6] print("The list before append",base_list) base_list[len(base_list):]=[10] print("The list after append",base_list)
输出:
The list before append [2, 4, 6] The list after append [2, 4, 6, 10]
如何使用追加函数定义堆栈?
以下属性适用于堆栈:-
- 堆栈可以定义为将项目一个接一个地放置的数据结构。
- 可以按照后进先出的原则插入或删除项目。
- 通常,堆栈会将一个项目压入堆栈末尾或顶部,而弹出操作则会从堆栈中删除一个项目。
- 附加函数充当堆栈的推送操作,而列表默认具有用于删除项目的弹出函数。
- 默认情况下,当没有为函数指定参数时,pop 方法将返回并删除列表中的最后一项。
- 当列表为空时,它会抛出索引错误。
- 如果向函数提供了一个整数参数,那么它将返回列表的索引。
- 它会删除列表中该索引处的项目。
让我们看一个程序,其中 append 和 pop 函数作为定义堆栈的推送和弹出操作:
计费示例:
Python 码:
#initialize the stack GGGstack = [] print("Adding item to the list",GGGstack.append(100)) print("Adding item to the list",GGGstack.append(2333)) print("Adding item to the list",GGGstack.append(50000)) print("the base list after adding elements,",GGGstack) print("base list after calling pop",GGGstack.pop()) print("base list after calling pop",GGGstack.pop()) print("base list after calling pop",GGGstack.pop()) print("base list after calling pop",GGGstack.pop())
输出:
Adding item to the list None Adding item to the list None Adding item to the list None the base list after adding elements, Stack([100, 2333, 50000]) base list after calling pop 50000 base list after calling pop 2333 base list after calling pop 100 Empty stack base list after calling pop None
代码说明:
- 定义了一个堆栈 GGGStack
- 使用 append 方法添加项目
- 每个项目都会从原始列表中逐一弹出。
- 当列表为空时,它会抛出索引错误。
扩展方法是什么 Python?
Extend 函数允许向可迭代列表添加新元素。可迭代列表的示例包括字典、元组和字符串。这些属性可帮助您修改可迭代列表的元素。
请注意: 此函数执行后不返回任何值。
以下是扩展函数的语法:-
语法:
List.extend(iterable list)
扩展和追加之间的区别 Python
- 附加函数 Python 仅向原始列表添加一个元素,而 extend 函数允许添加多个项目。
- 附加列表仅接受一个参数,而扩展函数接受可迭代列表,例如元组和字典。
结语
- 附加功能有助于在原始列表末尾添加项目。
- For 循环可与 append 函数一起使用,将多个项目添加到列表中。