当前位置:网站首页>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 )
边栏推荐
- Idea set class header comments
- The interviewer's biggest lie to deceive you, bypassing three years of less struggle
- A Kuan food rushed to the Shenzhen Stock Exchange: with annual sales of 1.1 billion, Hillhouse and Maotai CCB are shareholders
- Introducing Software Testing
- Deep learning ----- using NN, CNN, RNN neural network to realize MNIST data set processing
- [2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
- Gossip about redis source code 79
- AI Challenger 2018 text mining competition related solutions and code summary
- No qualifying bean of type ‘com. netflix. discovery. AbstractDiscoveryClientOptionalArgs<?>‘ available
- ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
猜你喜欢

Enter MySQL in docker container by command under Linux

What does redis do? Redis often practices grammar every day
![Docking Alipay process [pay in person, QR code Payment]](/img/30/665580241020ee3adb872e725f0624.jpg)
Docking Alipay process [pay in person, QR code Payment]

URL (data:image/png; Base64, ivborw0k... Use case

ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt

Idea a method for starting multiple instances of a service

It is worthy of "Alibaba internal software test interview notes" from beginning to end, all of which are essence

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
![[source code] VB6 chat robot](/img/89/46b67f627c8257eaddc70a247c9ba5.jpg)
[source code] VB6 chat robot

Correlation analysis summary
随机推荐
What does redis do? Redis often practices grammar every day
[source code] VB6 chat robot
MySQL winter vacation self-study 2022 12 (2)
On the day when 28K joined Huawei testing post, I cried: everything I have done in these five months is worth it
Struct in linked list
Similarities and differences of text similarity between Jaccard and cosine
How to trade spot gold safely?
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
Subgraph isomorphism -subgraph isomorphism
MySQL is installed as a Windows Service
leetcode-43. String multiplication
UTS | causal reasoning random intervention based on Reinforcement Learning
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Collation of the most complete Chinese naturallanguageprocessing data sets, platforms and tools
Global and Chinese market of glossometer 2022-2028: Research Report on technology, participants, trends, market size and share
Generic
Unity elementary case notes of angry birds Siki college 1-6
BBS forum recommendation
Selenium library 4.5.0 keyword explanation (II)
Deep learning ----- using NN, CNN, RNN neural network to realize MNIST data set processing