博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python append_Python清单append()方法
阅读量:2531 次
发布时间:2019-05-11

本文共 2373 字,大约阅读时间需要 7 分钟。

python append

Python list append() method adds an element to the end of the list.

Python列表的append()方法将元素添加到列表的末尾。

Python清单append()语法 (Python List append() Syntax)

The append() method adds a single item to the existing list. The original list length is increased by 1. It is one of the most popular list methods.

append()方法将单个项目添加到现有列表中。 原始列表长度增加了1。这是最流行的列表方法之一。

The append() method syntax is:

append()方法的语法为:

list.append(element)

The append() method takes a single parameter, which is appended to the end of the list. Python list is mutable.

append()方法采用单个参数,该参数将附加到列表的末尾。 Python列表是可变的。

The element can be a number, string, object, list, etc. We can store different types of elements in a list.

元素可以是数字,字符串,对象,列表等。我们可以在列表中存储不同类型的元素。

列出append()返回值 (List append() return value)

The list append() method doesn’t return anything. You can also say that the append() method returns None.

列表append()方法不返回任何内容。 您也可以说append()方法返回None

Python清单append()示例 (Python List append() Example)

Let’s look at a simple example to add an item to the end of the list.

让我们看一个简单的示例,将一个项目添加到列表的末尾。

vowels = ['a', 'e', 'i']print(f'Original List is {vowels}')vowels.append('o')vowels.append('u')print(f'Modified List is {vowels}')

Output:

输出:

Original List is ['a', 'e', 'i']Modified List is ['a', 'e', 'i', 'o', 'u']

将列表追加到另一个列表 (Appending List to another List)

If we pass a list to the append() method, it gets added as a single item to the end of the list.

如果我们将列表传递给append()方法,则它将作为单个项添加到列表的末尾。

list_numbers = [1, 2, 3]list_primes = [2, 3, 5, 7]list_numbers.append(list_primes)print(f'List after appending another list {list_numbers}')

Output:

输出:

List after appending another list [1, 2, 3, [2, 3, 5, 7]]

Tip: If you want to append the elements of a list to another list, use the list extend() method.

提示 :如果要将列表的元素追加到另一个列表,请使用列表extend()方法。

list_numbers_odd = [1, 3, 5]list_numbers_even = [2, 4, 6, 8]list_numbers_odd.extend(list_numbers_even)print(f'List after extending from another list {list_numbers_odd}')

Output:

输出:

List after extending from another list [1, 3, 5, 2, 4, 6, 8]

结论 (Conclusion)

Python List append() method allows us to add any type of data to the end of the list. The method doesn’t return anything. The original list is modified and the size is increased by 1.

Python List的append()方法允许我们将任何类型的数据添加到列表的末尾。 该方法不返回任何内容。 原始列表被修改,并且大小增加了1。

参考资料 (References)

翻译自:

python append

转载地址:http://pmozd.baihongyu.com/

你可能感兴趣的文章
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>
POJ 3740 Easy Finding (DLX模板)
查看>>
MySQL 处理重复数据
查看>>
关于typedef的用法总结(转)
查看>>
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>
iOS语言中的KVO机制
查看>>
excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
查看>>
响应式web设计之CSS3 Media Queries
查看>>
实验三
查看>>