当前位置:网站首页>Lambda function and for items Sort (key = lambda y:y[1], reverse = true).
Lambda function and for items Sort (key = lambda y:y[1], reverse = true).
2022-07-19 15:32:00 【Doraemon of little yellow man】
I saw this code when reading today , notice lambda It's quite ignorant , After reading a lot of materials, I realized .( It seems that there is still a lot to learn !!!)
def getText():
txt = open("hamlet.txt ", "r").read() # When using, enter the path of the file , In order to read the file
txt = txt.lower() # Full text in lowercase
for ch in '!''#$%&()*+,-./:;<=>[email protected][\\]^_`{|}~': # Put all the characters in the text , Special characters are all converted to " " ( Space )
txt = txt.replace(ch," ")
return txt
hamletTxt = getText()
words = hamletTxt.split()
counts = {} # Build a dictionary
for word in words:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key = lambda y:y[1], reverse = True) # key = lambda y:y[1] Difficult to understand
for i in range (10):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))
lambda function :
lambda by Python Medium 33 One of the reserved words , This reserved word defines a special function —— Anonymous functions , also called lambda function . Anonymous functions are not nameless , Instead, the function name is returned as the result of the function .
< Function name > = lambda < parameter list > : < expression >
Equivalent to the general form :
def < Function name > (< parameter list >):
return < expression >
The standard form is like this :sorted(d.items(), key = lambda x : x[1] )
d.items() For objects that need to be sorted
key=lambda Variable : Variable [ dimension ], among x:x[ ] The letters can be changed at will , Sort in brackets [] Sort the dimensions inside ,[0] Sort according to the first dimension ,[1] Sort according to the second dimension ,[2] Sort according to the third dimension , By analogy .
Example 1
# Sort the dictionary , The dimension of data is expressed in numbers
d = {"a":15,"b":16,"c":37,"d":24,"e":39}
b=sorted(d.items(), key=lambda x: x[1])
print(b)
The result is :
[('a', 15), ('b', 16), ('d', 24), ('c', 37),('e', 39)]
Example 2
# Sort the dictionary , The dimension of data is expressed in string
a = [{'name':'a','age':15},{'name':'b','age':31},{'name':'c','age':28}]
b=sorted(alist,key=lambda x:x['age'],reverse=True)
print(b)
The result is :
{'name': 'b', 'age': 31}, {'name': 'c', 'age': 28}, {'name': 'a', 'age': 15}]
Learning is a continuous process , When acquiring high-level knowledge, it feels like looking back at what you wrote before ~~ But this little bit is the testimony of your own gradual progress !! ( Come on, energetic workers )
边栏推荐
猜你喜欢

CloudBees CI使用Velero进行灾备(DR)概念验证

Wechat applet 6 cloud development cloud database

GYM103660H. Distance

Stack和Queue容器的系列操作( 详解 )

Impact analysis: rubygems unauthorized access vulnerability (cve-2022-29176)

OSPF learning notes (V) -- republish

vscode使用 “!“ 无效, 不出现模板问题的解决办法

Discussion on ISP noise model 1

OSPF learning notes (III)

浅谈ISP-图像噪声模型2
随机推荐
我应该怎么设计我的博客?如何搭建一个体验好的博客?
tensorflow clip对NaN、inf的效果
Wechat applet 7 cloud storage
新基民在股票账户上买基金安全吗
网络上开户买基金是否安全呢?刚接触基金,不懂求指导
股票财务信息,董事会,监事会等高管信息爬取
232. 用栈实现队列
初来乍到,多多关照(其实不是初来了^_^,Hello CSDN,我来了)
C # FTP dual network card problem
Several scenarios of technical "brick house" solving problems
2034: [蓝桥杯2022初赛] 修剪灌木
I'm new here, so please take care of me. (actually, it's not new here ^ ^, hello CSDN, I'm here.)
Is it safe for Hengtai securities to open an account online?
【golang学习笔记1.6】 golang中的堆和栈
OSPF learning notes (V) -- republish
2022-7-17
您填写的保质期截止当前已经小于10天,不允许发布,若实际保质期大于10天,请如实填写生产日期与进货日期
2020 ICPC Asia East continuous final g. Prof. Pang's sequence line segment tree / scan line
1.3.1 全排列问题
Discussion on ISP noise model 1