Python reduce()
In this page:
from functools import reduce
reduce(function, iterable, initial_value)
reduce() क्या है?
reduce(function, iterable), एक two-argument function को running total और अगले element पर बार-बार, बाएँ से दाएँ, लागू करके sequence को एक single accumulated value में समेट देता है।
map() और filter() के विपरीत, यह built-in नहीं है -- इसे functools module से import करना ज़रूरी है।
उदाहरण: What is reduce()?
from functools import reduce
numbers = [1, 2, 3, 4]
total = reduce(lambda acc, x: acc + x, numbers) # folds the list into one accumulated value
print(total)
reduce() से Lists जोड़ना
reduce(lambda acc, x: acc + x, numbers) से किसी list को जोड़ना यह pattern साफ दिखाता है: accumulator पहले element से शुरू होता है, और हर step पर अगला number उसमें जुड़ता जाता है, अंत में total बनता है -- हालाँकि इस specific case के लिए Python का built-in sum() यही काम कहीं ज़्यादा सीधे तरीके से कर देता है।
उदाहरण: Summing Lists with reduce()
from functools import reduce
numbers = [1, 2, 3, 4]
total = reduce(lambda acc, x: acc + x, numbers) # accumulator pattern
print(total)
print(sum(numbers)) # built-in sum() does the same thing more directly
Maximum ढूँढना
वही accumulator pattern running accumulator की हर अगले element से तुलना करके और जो बड़ा (या छोटा) हो उसे रखते हुए maximum या minimum ढूँढता है, जो अनिवार्य रूप से यही है कि reduce() किसी भी pairwise comparison को एक single sequence-wide result में कैसे generalize करता है।
उदाहरण: Finding the Maximum
from functools import reduce
numbers = [3, 7, 2, 9, 4]
maximum = reduce(lambda acc, x: x if x > acc else acc, numbers) # keeps the larger of acc and x each step
print(maximum)
reduce() से Strings जोड़ना
reduce() सिर्फ numbers तक सीमित नहीं है -- एक string-concatenation lambda पास करने से strings की एक list एक combined string में समेट जाती है, जो दिखाता है कि accumulator pattern हर उस type के लिए काम करता है जो आपके दिए combining operation को support करता है।
उदाहरण: Concatenating Strings with reduce()
from functools import reduce
words = ["Hello", " ", "World"]
sentence = reduce(lambda acc, w: acc + w, words) # accumulator pattern works for strings too
print(sentence)
Initializer Values का इस्तेमाल
reduce() को तीसरा argument पास करने से accumulator के लिए sequence के पहले element का इस्तेमाल करने के बजाय एक explicit initial value set हो जाती है।
यह दोनों वजहों से मायने रखता है: एक खाली sequence के साथ correctness के लिए (जो वरना TypeError raise करती), और उन मामलों के लिए जहाँ आप चाहते हैं कि fold किसी specific baseline value से शुरू हो।
उदाहरण: Using Initializer Values
from functools import reduce
numbers = []
total = reduce(lambda acc, x: acc + x, numbers, 0) # explicit initial value, avoids TypeError on an empty list
print(total)
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- Python PEP 8 Style Guide
- Python Debugging Techniques
- Python Testing with unittest
- Python Common Mistakes
- Python Interview Questions
- Python map() & filter()
- Python reduce()
- Python zip() & enumerate()
- Python sorted() & key Functions
- Python Comprehensions Advanced
- Python Turtle Graphics
- Python tkinter Introduction
- Python tkinter Widgets
- Python pygame Introduction
- Python Mini Projects