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



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

Filter with Lambdas


Let’s perform the same steps as earlier, except this time we’ll use a lambda function:

# using the filter function with lambda functions, filter out temps below 55F temps = [ 12.5, 13.6, 15, 9.2 ] filtered_temps = list( filter( lambda C : True if (9/5) * C + 32 > 55 else
False, temps) ) # type convert the filter print(filtered_temps)

Go ahead and run the cell. We’ll get the same output as we did earlier, except this time we were able to reduce the number of lines used with our lambda function. The same process that we’re performing can be found in the lines of code in the following:
>>> def convertDeg(degrees):
>>> filtered = [ ] >>> for degree in degrees:
>>> result = (9/5) * degree + 32 >>> if result > 55:
>>> filtered.append(degree)
>>> return filtered
>>> temps = [ 12.5, 13.6, 15, 9.2 ]
>>> filtered_temps = convertDeg(temps)
>>> print(filtered_temps)
Like the map function using lambdas, coupling the filter function with a lambda cuts our code down greatly.
ChapteR 8 advanCed topICs I: eFFICIenCy

The Problem with Reduce


Although I’m going to show you how to use the reduce function, you should understand that there’s a better method than using the actual function. Per the creator of Python himself:
So now reduce( ). This is actually the one I've always hated most, because, apart from a few examples involving + or , almost every time I see a reduce( ) call with a non-trivial function argument, I need to grab pen and paper to diagram what's actually being fed into that function before I understand what the reduce( ) is supposed to do. So in my mind, the applicability of reduce( ) is pretty much limited to associative operators, and in all other cases it's better to write out the accumulation loop explicitly.1
In his own words, he’s saying that reduce only serves a couple purposes, but other than that, it’s useless, so it makes more sense to use a simple for loop. Let’s look at both examples.
Note Reduce was a built-in function in python 2, since then it has been moved into the functools library.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   133   134   135   136   137   138   139   140   ...   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