python匿名函数可以赋值给变量 您所在的位置:网站首页 lambda函数可以使用赋值语句块吗 python匿名函数可以赋值给变量

python匿名函数可以赋值给变量

2023-06-24 05:51| 来源: 网络整理| 查看: 265

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语言的变量存储区别见下图,

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

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

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

str1 = "hello world"

print(id(str1))

#Output:48672272

str1 = "new hello world"

print(id(str1))

#Output:48672224

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

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

有了以上先验知识,我们可以正式来讨论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

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

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

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

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

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

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

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']

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

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

import copy

lst = ['str1','str2','str3','str4','str5']

sourcelist = ['str1','str2','str3','str4','str5', lst]

deepcopylist = copy.deepcopy(sourcelist)

------------------------------------------参考------------------------------------------lambda表达式 · Python进阶​eastlakeside.gitbooks.ioPython lambda介绍 - Goodpy - 博客园​www.cnblogs.compython--赋值与深浅拷贝 - Eva_J - 博客园​www.cnblogs.com



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有