当前位置:网站首页>From functools import reduce -- see the use of reduce function from typical examples
From functools import reduce -- see the use of reduce function from typical examples
2022-07-04 00:23:00 【irrationality】
from functools import reduce
1. Accumulation of integers : The integers in the list are accumulated
a=[1,3,5]
b=reduce(lambda x,y:x+y,a)
print('1. The integers in the list are accumulated ==:',b)
1. The integers in the list are accumulated ==: 9
2. Accumulation of lists : The lists in the list add up
a=[[1,3,5],[6]]
b=reduce(lambda x,y:x+y,a)
print('2. The lists in the list add up —:',b)
2. The lists in the list add up —: [1, 3, 5, 6]
a=[[["abc","123"],["def","456"],["ghi","789"]]]
b=reduce(lambda x,y:x+y , a )
print(' The lists in the list add up —:',b)
The lists in the list add up —: [['abc', '123'], ['def', '456'], ['ghi', '789']]
3. Accumulation of tuples : The tuples in the list are added
a=[("abc","123"),("def","456"),("ghi","789")]
b=reduce(lambda x,y:x+y , a )
print('3. The tuples in the list are added !!',b)
3. The tuples in the list are added !! ('abc', '123', 'def', '456', 'ghi', '789')
4. Accumulation of strings
a=['abc','def','hij']
b=reduce(lambda x,y:x+y,a)
print('4. Accumulation of strings in the list :~~',b)
4. Accumulation of strings in the list :~~ abcdefhij
a=('abc','def','hij')
b=reduce(lambda x,y:x+y,a)
print(' The accumulation of strings in Yuanzu :~~',b)
The accumulation of strings in Yuanzu :~~ abcdefhij
a=[['abc','def','hij']]
b=reduce(lambda x,y:x+y,a)
print(' The accumulation of strings in the nested list :~~',b)
The accumulation of strings in the nested list :~~ ['abc', 'def', 'hij']
# summary :
#1.functools function ;reduce decompose ;lambda Anonymous functions ;x,y:x+y expression
#2. Use functools.reduce, If it's an integer, add up ;
#3. Use functools.reduce, If string 、 list 、 Yuanzu joined together ( Quite took off a layer of coat )
边栏推荐
- [MySQL] sql99 syntax to realize multi table query
- ISBN number
- Gossip about redis source code 73
- Generic
- Zipper table in data warehouse (compressed storage)
- UTS | causal reasoning random intervention based on Reinforcement Learning
- 2022 system integration project management engineer examination knowledge points: software development model
- P1656 bombing Railway
- Data storage - interview questions
- Gossip about redis source code 76
猜你喜欢
Investment demand and income forecast report of China's building ceramics industry, 2022-2028
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
Tencent interview: can you pour water?
Solve the problem that the kaggle account registration does not display the verification code
Deep learning ----- using NN, CNN, RNN neural network to realize MNIST data set processing
[about text classification trick] things you don't know
Kubedl hostnetwork: accelerating the efficiency of distributed training communication
MySQL is installed as a Windows Service
Report on prospects and future investment recommendations of China's assisted reproductive industry, 2022-2028 Edition
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
随机推荐
Global and Chinese markets for coronary artery disease treatment devices 2022-2028: Research Report on technology, participants, trends, market size and share
Is the account opening of Guoyuan securities really safe and reliable
Selenium library 4.5.0 keyword explanation (III)
The upload experience version of uniapp wechat applet enters the blank page for the first time, and the page data can be seen only after it is refreshed again
(Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes
[PHP basics] session basic knowledge, application case code and attack and defense
Pytorch learning notes 5: model creation
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
BBS forum recommendation
Global and Chinese market of underwater bags 2022-2028: Research Report on technology, participants, trends, market size and share
Smart fan system based on stm32f407
Correlation analysis summary
Subgraph isomorphism -subgraph isomorphism
Ramble 72 of redis source code
Solution to the impact of Remote Code Execution Vulnerability of log4j2 component on December 9, 2021
2020.2.14
A Kuan food rushed to the Shenzhen Stock Exchange: with annual sales of 1.1 billion, Hillhouse and Maotai CCB are shareholders
[PHP basics] cookie basics, application case code and attack and defense
Tencent interview: can you find the number of 1 in binary?
Makefile judge custom variables