Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet138/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   134   135   136   137   138   139   140   141   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Using Reduce


The reduce function accepts two arguments, the function to perform the execution and the data collection to iterate over. Unlike filter and map, however, reduce iterates two items at a time instead of one. The result of reduce is to always return a single result. In the following example, we want to multiply all the numbers with each other. Let’s use reduce to execute this example:

# for informational purposes this is how you use the reduce function from functools import reduce nums = [ 1, 2, 3, 4 ] result = reduce( lambda a, b : a * b, nums ) # result is 24 print(result)

1 www.artima.com/weblogs/viewpost.jsp?thread=98196
Go ahead and run the cell. The output will be 24. As the reduce function takes in two arguments, it condenses the nums list down to a single returned value. In the following, you’ll see the suggested way of executing the same procedure:
>>> total = 0 >>> for n in nums:
>>> total = total * n
For the most part, it’s easy to see why Rossum was so adamant on suggesting for loops instead, as reduce can become tough to understand when you try more complex data collections like lists within lists.

WEDNESDAY EXERCISES


  1. Mapping Names: Use a lambda and map function to map over the list of names in the following to produce the following result “[ “Ryan”, “Paul”, “Kevin Connors” ].

>>> names = [ " ryan", "PAUL", "kevin connors " ]

  1. Filter Names: Using a lambda and filter function, filter out all the names that start with the letter “a.” Make it case insensitive, so it filters out the name whether it’s uppercase or not. the output of the following list should be [ “Frank”, “Ripal” ].

>>> names = [ "Amanda", "Frank", "abby", "Ripal", "Adam" ]

today we learned about a few important built-in functions that we can use when working with data in python. Coupling map and filter with lambdas helps to improve our code readability and shorten the lines of code needed. Lastly, reduce can be helpful in a few situations; however, a for loop will generally be more readable.
ChapteR 8 advanCed topICs I: eFFICIenCy

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   134   135   136   137   138   139   140   141   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish