首页 技术 正文
技术 2022年11月12日
0 收藏 635 点赞 4,840 浏览 1531 个字

1. list列表排序

 #### sort排序
nums = [,,,,,] nums.sort()
print(nums)### 结果
[, , , , , ]

######## 逆序
In []: nums.sort(reverse=True)In []: nums
Out[]: [, , , , , , ]
#####  翻转
In []: nums.reverse()In []: nums
Out[]: [, , , , , , ]

2.字典排序

 infors = [{"name":"alex","age":},{"name":"jack","age":}]
infors.sort(key=lambda x:x["age"]) #按照age排序 print(infors)

    day9 匿名函数 lambda

  把list的元素,单个元素字典,传入到x,即 x:x[‘name’] 就是    {“name”:“alex”,“age”:43} :alex

3.lambda应用:

  1)版本1:求11+22

 def test(a,b):
result = a+b
return result num = test(11,22)
print(num)

  

  2)版本2:

 def test(a,b,func):
result = func(a,b)
return result num = test(11,22,lambda x,y:x+y)
print(num)

       day9 匿名函数 lambda

  3)版本3:动态语言

 #-*- coding:utf-8 -*-         python2执行
def test(a,b,func):
result = func(a,b)
return result func_new = input("请输入一个匿名函数:") #python2 input是函数
num = test(11,22,func_new)
print(num)

      day9 匿名函数 lambda

  4)版本4:eval  去掉字符串的 “ ”

 ####  python3 执行 def test(a,b,func):
result = func(a,b)
return result #func_new = input("请输入一个匿名函数:") func_new = input("请输入一个匿名函数:")
func_new = eval(input(func_new)) #eval把字符串的“”去掉
num = test(11,22,func_new)
print(num)

    day9 匿名函数 lambda

3.面试题:交换两个变量的值

  1)版本1:空瓶子t

 a = 4
b = 5
t = 0
print("a=%s,b=%s"%(a,b))
t = a
a = b
b = t
print("a=%s,b=%s"%(a,b))

      day9 匿名函数 lambda

  2)版本2:不用第三个变量

 #### 第2种,不用第三个变量
a = a+b
b = a-b
a = a-b
print("a=%s,b=%s"%(a,b))

  3)版本3:python独有

#####  第3种
a,b = b,a
print("a=%s,b=%s"%(a,b))

    day9 匿名函数 lambda

3.num += num 与 num = num +num 的区别

  1)版本1:a = 100

 ###  不可变类型 数字 字符串  元组
a = 100
def test(num):
num += num
print(num) test(a) print(a)

      day9 匿名函数 lambda

  2)版本2:a = [100]

 #a = 100
a = [100] #list列表是可变类型
def test(num):
num += num # 直接在num变量的内存地址修改,然后还是指向 a
print(num) test(a) print(a)

      day9 匿名函数 lambda‘’

day9 匿名函数 lambda

  3) 版本3: num = num + num

 ###  python中 变量是引用的
#a = 99
a = [100]
def test(num):
#num += num
num = num + num #执行得到结果 [100,100] 然后让 num 再重新指向它
print(num) test(a) print(a)

    day9 匿名函数 lambda

  day9 匿名函数 lambda

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,136
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,300