Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet54/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   50   51   52   53   54   55   56   57   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

List comprehension

Sometimes loops are used to go through one collection of items simply to generate another

collection of (different) items. For example, here the function range(1,8) gives a sequence

[1,2,3,4,5,6,7], which is used to make a list of square numbers:

squares = []

for x in range(1,8):

squares.append(x*x)

# squares becomes [1, 4, 9, 16, 25, 36, 49]

However,  when  mapping  one  collection  to  another  in  this  way,  there  is  a  neater

alternative syntax called list comprehension. And often this will allow you to map one list

of items onto another in a single line of code. In essence a list comprehension is a way of

using a loop to build a list from the inside. Here is a simple example:

squares = [x*x for x in range(1,8)] # [1, 4, 9, 16, 25, 36, 49]

The first expression inside the square brackets (x*x) is what is placed in the resulting

list, and the remainder, from the for to the closing bracket, is what generates the values for

the loop variable x. Not only is this shorter to write than a conventional for loop, it is also

computationally quicker.


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   50   51   52   53   54   55   56   57   ...   514




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