c++ map初始化同时赋值_[1]Python之lambda表达式&赋值与深浅拷贝相关用法

本文介绍了Python中lambda表达式的使用方法,包括结合sort、filter、map和reduce函数的应用实例。此外,还详细讲解了Python中变量的赋值机制、浅拷贝(copy)和深拷贝(deepcopy)的区别,并通过示例展示了它们的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

lambda表达式

啊lambda表达式其实就是便携版函数

原型如下,

栗子1(加法),

add=lambda x, y : x + y
print(add(3,5))
#Output:8

栗子2(lambda结合sort),

a = [(1,2),(4,1),(9,10),(13,-3)]
a.sort(key=lambda x : x[1])
print(a)
#Output:[(13,-3),(4,1),(1,2),(9,10)]

栗子3(lambda结合filter),

foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
print(filter(lambda x : x % 3 == 0, foo))
#Output:[18, 9, 24, 12, 27]

栗子4(lambda结合列表映射函数map),

foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
print(map(lambda x : x * 2 + 10, foo))
#Output:[14, 46, 28, 54, 44, 58, 26, 34, 64]

栗子5(lambda结合迭代函数reduce),

foo = range(1, 6)
print(reduce(lambda x, y : x * y, foo))
#Output:120 

*lambda的有点是代码简洁但并不会提高运行效率,缺点是易读性差

*能用for...in..if完成的就不要用lambda,比如栗子3可以用 [x for x in foo if x % 3== 0]替代

*如果用lambda要包含循环等复杂逻辑,宁愿def func来完成

*总的来说,lambda 是为了减少单行函数的定义而存在的

赋值与深浅拷贝

在高级语言中,变量是对内存及地址的抽象。python中的一切变量都是对象,变量的存储采用了引用语义的方式,变量存储的只是一个变量的值所在的内存地址而不是变量的值本身

python和c语言的变量存储区别见下图,

v2-7e025bf80546bfd92f6f836c97c97b51_b.jpg

我们可以将python中的变量数据类型大致的分为简单数据类型和复杂的数据结构,如下图,

v2-5c53438b1684778b2a81849fb85f00ee_b.jpg

由于python中的变量都是采用的引用语义,导致了python中的数据的存储情况如下图,

v2-f656c19bb21ca60e8fe0afac608e9099_b.jpg

所以当我们重复的初始化变量的时候,情况如下,

str1 = "hello world"
print(id(str1))
#Output:48672272
str1 = "new hello world"
print(id(str1))
#Output:48672224

v2-adcc2210b04b343823efb999aaecad9f_b.jpg

当我们改变复杂的数据类型的值的时候,情况如下,

lst1 = [1,2,3,4,5,6]
print(id(lst1))
#Output:48574728
lst1.append('new item')
print(lst1)
#Output:[1,2,3,4,5,6,'new item']
print(id(lst1))
#Output:48574728

lst1.pop()
print(lst1)
#Output:[1,2,3,4,5,6]
print(id(lst1))
#Output:48574728

lst1[0] = 'change test'
print(lst1)
#Output:['change test',2,3,4,5,6]
print(id(lst1))
#Output:48574728

lst1=[1,2,3,4,5]
print(id(lst1))
#Output:48221192

v2-8fc785566bfd05744e085f13acadf34e_b.jpg

有了以上先验知识,我们可以正式来讨论python的赋值操作了

简单的数据类型赋值的栗子,

str1 = 'hello world'
print(id(str1))
#Output:41863664
str2 = str1
print(id(str2))
#Output:41863664
str1 = 'new hello world'
print(str1)
#Output:new hello world
print(str2)
#Output:hello world
print(id(str1))
#Output:45133920
print(id(str2))
#Output:41863664

v2-dc5e4ee0628aa4482bd53facad53c46a_b.jpg

*赋值指向同地址,初始化/新增元素指向新地址。赋值不改变值,改变地址。

复杂的数据结构的赋值操作的栗子,

lst1 = [1,2,3,4,5,6]
lst2 = lst1
print(id(lst1))
#Output:48226504
print(id(lst2))
#Output:48226504
lst1.append('new item')
print(lst1)
#Output:[1,2,3,4,5,6,'new item']
print(lst2)
#Output:[1,2,3,4,5,6,'new item']
print(id(lst1))
#Output:48226504
print(id(lst2))
#Output:48226504

v2-a924209c7d0409e764e18b7e92304798_b.jpg

*指向同一地址的多个变量:共享资源、值变俱变

然鹅,我们常常有一种需求:将一份数据的原始内容保留一份,再去处理新数据,这时单单赋值的操作就不够明智了,所以copy和deepcopy出场

copy叫作浅拷贝,即不管多么复杂的数据结构,浅拷贝都只会copy第一层地址块,如下,

v2-a3faad7bbd5cfef8d0dd5db047dc346a_b.jpg
import copy

lst = ['str1','str2','str3','str4','str5']
sourcelist = ['str1','str2','str3','str4','str5', lst]
copylist = copy.copy(sourcelist)
print('===>sourcelist:', sourcelist)
#Output:===>sourcelist:['str1','str2','str3','str4','str5', ['str1','str2','str3','str4','str5']]
print('===>copylist:', copylist)
#Output:===>copylist:['str1','str2','str3','str4','str5', ['str1','str2','str3','str4','str5']]

sourcelist.append('sourcestr')
copylist.append('copystr')
print('===>sourcelist:', sourcelist)
#Output:===>sourcelist:['str1','str2','str3','str4','str5', ['str1','str2','str3','str4','str5'],'sourcestr']
print('===>copylist:', copylist)
#Output:===>copylist:['str1','str2','str3','str4','str5', ['str1','str2','str3','str4','str5'],'copystr']

sourcelist[0] = 'changeSource'
print('===>sourcelist:', sourcelist)
#Output:===>sourcelist:['changeSourcde','str2','str3','str4','str5', ['str1','str2','str3','str4','str5'],'sourcestr']
print('===>copylist:', copylist)
#Output:===>copylist:['str1','str2','str3','str4','str5', ['str1','str2','str3','str4','str5'],'copystr']

lst.append('testAppend')
print('===>sourcelist:', sourcelist)
#Output:===>sourcelist:['changeSourcde','str2','str3','str4','str5', ['str1','str2','str3','str4','str5','testAppend'],'sourcestr']
print('===>copylist:', copylist)
#Output:===>copylist:['str1','str2','str3','str4','str5', ['str1','str2','str3','str4','str5','testAppend'],'copystr']

v2-ab1dbd550783a772fde729756bf777d1_b.jpg

*copy只独立第一层(分开存储第一层地址),第二层依然等于赋值

想要两个变量完全的独立,就上deepcopy啰,如下,

import copy

lst = ['str1','str2','str3','str4','str5']
sourcelist = ['str1','str2','str3','str4','str5', lst]
deepcopylist = copy.deepcopy(sourcelist)

v2-544fbe70259a1182223213356ec6d9e8_b.jpg

------------------------------------------参考------------------------------------------

lambda表达式 · Python进阶​eastlakeside.gitbooks.io Python lambda介绍 - Goodpy - 博客园​www.cnblogs.com python--赋值与深浅拷贝 - Eva_J - 博客园​www.cnblogs.com
v2-dd7d825293a2a3a26e1082b410693518_180x120.jpg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值