python全栈开发《43.列表的extend函数》

1.extend功能

将其它列表或元组中的元素一次性倒入到当前列表中。

2.extend用法

7dff16f70516e9238782a629c0eab22f.png

extend函数无返回值。它会把传入到extend函数中的列表或元组中的成员放到当前列表中。

用法:

students = ['dewei','xiaobian','xiaogang']
new_students = ('xiaogang','xiaohong')
students.extend(new_students)
print(students)

运行结果:

/Users/llq/PycharmProjects/pythonlearn/pythonlearn/python_list/bin/python /Users/llq/PycharmProjects/pythonlearn/python_list/1.py 
['dewei', 'xiaobian', 'xiaogang', 'xiaogang', 'xiaohong']

进程已结束,退出代码为 0

元组中的新生已经添加到列表的末尾了。

之前在students中已经有一个'xiaogang'了,new_students中也有一个'xiaogang',运行的结果中可以看出,并不会和之前的成员覆盖掉。

1)extend函数为什么无返回值?

在Python中,list.extend()方法用于将一个可迭代对象(列表或元组)的所有元素添加到列表的末尾。它没有返回值的原因是:

就地修改extend方法直接修改原始列表,而不是创建一个新的列表。这种设计使得操作更高效,避免了额外的内存开销。

清晰性:由于方法不返回值,调用后直接在原列表上看到变化,避免了混淆,用户知道原始列表已被更新。

因此,extend方法通过就地修改实现其功能,而不需要返回值。

2)python中返回值是什么?

在Python中,返回值是函数执行后返回给调用者的结果。通过return语句来返回值。如果没有return,函数将默认返回None

(在Python中,所有有返回值的函数确实是通过return语句来返回值。如果函数没有显式使用return语句,或者return后面没有值,那么函数会默认返回None。因此,return语句是Python中返回值的主要方式。)

list.extend()方法用于将一个可迭代对象(列表或元组)的元素添加到列表中,但它不会返回新列表,而是返回None。这是因为extend()是就地修改原列表的方法,旨在改变现有列表的内容而不是创建一个新的列表。

例如:

my_list = [1, 2, 3]
result = my_list.extend([4, 5])
print(my_list)  # 输出: [1, 2, 3, 4, 5]
print(result)   # 输出: None

这种设计使得代码更简洁,因为你不需要创建一个新的列表,而是直接修改原列表。

def add(a, b):
    return a + b

result = add(3, 5)  # result的值是8

在这个例子中,add函数返回了ab的和。返回值可以是任何数据类型,如数字、字符串、列表、字典等。

3.代码

例1:

#coding:utf-8

manhua = []
history = []
code = []

new_manhua = ('a','b','c')#为什么定义元组呢?每次上货的数据,一旦确定就不会更改了。
new_history = ('中国历史','日本历史','韩国历史')
new_code = ('python','django','flask')

manhua.extend(new_manhua)
history.extend(new_history)
code.extend(new_code)

print(manhua,history,code)

history.extend(manhua)
del manhua
print(history)

运行结果:

/Users/llq/PycharmProjects/pythonlearn/pythonlearn/python_list/bin/python /Users/llq/PycharmProjects/pythonlearn/python_list/list_extend.py 
['a', 'b', 'c'] ['中国历史', '日本历史', '韩国历史'] ['python', 'django', 'flask']
['中国历史', '日本历史', '韩国历史', 'a', 'b', 'c']

进程已结束,退出代码为 0

例2:

使用extend函数,不传入列表和元组,行不行呢?

字符串:

test = []
test.extend('asdf')
print(test)

运行结果:

/Users/llq/PycharmProjects/pythonlearn/pythonlearn/python_list/bin/python /Users/llq/PycharmProjects/pythonlearn/python_list/list_extend.py 
['a', 's', 'd', 'f']

进程已结束,退出代码为 0

字典:

test = []
test.extend({'name':'dewei'})
print(test)

运行结果:

/Users/llq/PycharmProjects/pythonlearn/pythonlearn/python_list/bin/python /Users/llq/PycharmProjects/pythonlearn/python_list/list_extend.py 
['name']

进程已结束,退出代码为 0

布尔类型:

test = []
test.extend(True)
print(test)

运行结果:

/Users/llq/PycharmProjects/pythonlearn/pythonlearn/python_list/bin/python /Users/llq/PycharmProjects/pythonlearn/python_list/list_extend.py 
Traceback (most recent call last):
  File "/Users/llq/PycharmProjects/pythonlearn/python_list/list_extend.py", line 23, in <module>
    test.extend(True)
TypeError: 'bool' object is not iterable

进程已结束,退出代码为 1

总结: 列表的extend函数里可以导入列表、元组、字符串和字典。

字典:只能获取到字典的key,value会被忽略。字符串:会将整个字符串打乱,每一个字符会生成列表中的一个元素来存储。

列表的extend函数里传入其它类型会报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清菡软件测试

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值