当前位置:网站首页>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 )
边栏推荐
- Qtcharts notes (V) scatter diagram qscatterseries
- NLP pre training technology development
- Gossip about redis source code 75
- Correlation analysis summary
- After the Lunar New Year and a half
- Solution to the impact of Remote Code Execution Vulnerability of log4j2 component on December 9, 2021
- Development and application of fcitx functional plug-ins
- Sorry, Tencent I also refused
- Global and Chinese markets for instant saliva testing devices 2022-2028: Research Report on technology, participants, trends, market size and share
- [2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
猜你喜欢

What is the potential of pocket network, which is favored by well-known investors?

Alibaba cloud container service differentiation SLO hybrid technology practice
![[source code] VB6 chat robot](/img/89/46b67f627c8257eaddc70a247c9ba5.jpg)
[source code] VB6 chat robot

Recommendation of knowledge base management system

STM32 key light
![[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!](/img/28/fc05f2e8d53cf81fd061c799090022.jpg)
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!

MySQL is installed as a Windows Service
![Docking Alipay process [pay in person, QR code Payment]](/img/30/665580241020ee3adb872e725f0624.jpg)
Docking Alipay process [pay in person, QR code Payment]

Self study software testing. To what extent can you go out and find a job?

Data mining vs Machine Learning: what is the difference between them? Which is more suitable for you to learn
随机推荐
Anomalies seen during the interview
Is user authentication really simple
Five high-frequency questions were selected from the 200 questions raised by 3000 test engineers
NLP pre training technology development
Version rollback revert don't reset better reset must be forced
ISBN number
Celebrate the new year | Suihua fire rescue detachment has wonderful cultural activities during the Spring Festival
Shell script three swordsman sed
Qtcharts notes (V) scatter diagram qscatterseries
Smart fan system based on stm32f407
URL (data:image/png; Base64, ivborw0k... Use case
Gossip about redis source code 80
Deep learning ----- using NN, CNN, RNN neural network to realize MNIST data set processing
[source code] VB6 chat robot
CSP window
Self study software testing. To what extent can you go out and find a job?
Alibaba cloud container service differentiation SLO hybrid technology practice
Analysis on the scale of China's smart health industry and prediction report on the investment trend of the 14th five year plan 2022-2028 Edition
Gossip about redis source code 81
Distributed transaction -- middleware of TCC -- selection / comparison