网站建设资讯

NEWS

网站建设资讯

创新互联Python教程:Pythonappend()

python 中的append()函数有助于将给定的项目添加到现有列表的末尾。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名申请、虚拟空间、营销软件、网站建设、岱岳网站维护、网站推广。

 **list.append(item)** #where item can be numbers, strings, dictionaries etc 

追加()参数:

append()函数接受一个参数。

参数 描述 必需/可选
项目 要添加到列表末尾的项目 需要

追加()返回值

这个方法不返回任何值,这意味着它不返回任何值。实际上,这个方法更新了现有的列表。

Python 中append()方法的示例

示例append()在 python 中是如何工作的?

 # flowers list
flowers = ['Rose', 'Lotus', 'Jasmine']

# 'Sunflower' is appended to the flowers list
flowers.append('Sunflower')

# Updated flowers list
print('Updated flowers list: ', flowers ) 

输出:

 Updated flowers list:  ['Rose', 'Lotus', 'Jasmine', 'Sunflower'] 

示例 2:如何使用append()向列表中添加列表

 # flowers list
flowers = ['Rose', 'Lotus', 'Jasmine']

# flowers list
flowers = ['Rose', 'Lotus', 'Jasmine']

# list of vegetables 
vegetables = ['Tomato', 'Potato']

# appending vegetables list to the flowers list
flowers.append(vegetables)

print('Updated list: ', flowers) 

输出:

 Updated list:  ['Rose', 'Lotus', 'Jasmine', ['Tomato', 'Potato']] 

网站标题:创新互联Python教程:Pythonappend()
本文URL:http://cdysf.com/article/coeeoce.html