Python Programming for Biology: Bioinformatics and Beyond



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

Positional indices

In most loops you do not have to know the positional index of the associated variable in

the list, you just need to know that the variable is set to one item after another in the list.

But sometimes you do have to know the index. For example, in geometry a vector (e.g. x,

y and z axis values to specify a position in three-dimensional space) can be implemented

in  Python  as  a  list  of  floating  point  numbers.  Suppose  that  you  wanted  to  calculate  the

inner  (or  dot)  product  between  two  vectors,  vec1  and  vec2.

1

 The  inner  product  is  the



summation of the products of the corresponding terms in each list. So in three dimensions,

for example, it would be:

vec1 = (4, 7, 0)

vec2 = (1, 3, 5)

s = vec1[0]*vec2[0] + vec1[1]*vec2[1] + vec1[2]*vec2[2] # 25

Here  the  calculation  for  the  inner  product  is  (4×1)  +  (7×3)  +  (0×5)  yielding  25.  We

could also save writing out the whole calculation and use a loop to go through all of the

required  vector  positions  (indices)  adding  the  product  for  each.  Thus,  instead  we  can

write:

s = 0


for index in [0,1,2]:

s += vec1[index] * vec2[index]

This is a good enough way of calculating the inner product if you know you are in three

dimensions, but let’s say you wanted to calculate this for vectors in an arbitrary number of

dimensions, i.e. where the length of the vectors is not known beforehand. In this case the

list  of  indices  needs  to  match  the  size  of  the  vectors.  We  can  obtain  such  a  list  using

range().

The range() function takes one, two or three integer arguments. If it has one argument,

n, then it generates a number sequence starting at 0 and finishing at n-1, in steps of 1. If it

has two arguments, n1 and n2, then it generates a sequence starting at n1 and finishing at

n2-1, in steps of 1.

range(7) # [0, 1, 2, 3, 4, 5, 6] – show as list(range(7)) in

Python 3



range(2, 7) # [2, 3, 4, 5, 6]

If it has three arguments then the first two, n1 and n2, are as before, and the third is the

step size. There is one subtlety, however: if the step size is positive then the list stops at

the greatest integer less than n2 (which might not be n2-1) and if the step size is negative

then the sequence stops at the least integer greater than n2. If you use a step size of 0 you

get an error.

range(2, 13, 3) # [2, 5, 8, 11]

range(7, 2, -2) # [7, 5, 3]

Returning to the inner product calculation, we simply need to use the length of one of

the vectors len(vec1) to get the required range of indices:

s = 0

for i in range(len(vec1)):



s += vec1[i] * vec2[i]

Here  len(vec1)  gives  the  length  of  vec1  and  so  range(len(vec1))  gives  a  list  which

exactly  contains  the  valid  (non-negative)  indices  for  vec1  and  vec2.  Note,  the  code

assumes that vec1 and vec2 are the same length, and that is something that should really

be checked somewhere.

In Python 2 there is an alternative for range(), which is xrange(). The difference is that

range()  actually  creates  a  whole  list  of  numbers  while  xrange()  does  not;  it  just  sets  the

relevant  loop  variable  (here,  n)  to  what  it  should  be  set  to,  one  after  the  other.  The

difference between range() and xrange() is normally not an issue but could be if the length

were large. In Python 3, xrange() no longer exists and range() behaves like the Python 2

xrange().

There is another, arguably neater, way we could get at the indices and that is using the

inbuilt  enumerate()  function,  which  gives  both  an  index  and  the  corresponding  item.

Specifically, it sets the loop variable to a two-tuple whose first item is the index and the

second is the list item at that index:

s = 0


for i, x in enumerate(vec1):

s += x * vec2[i]

It behaves like Python 3’s xrange() in regard to not actually creating one big list (in this

case of 2-tuples), so is reasonably efficient. It might seem possible to also use the .index()

function  to  get  the  position  of  an  item  in  the  vector.  However,  this  would  only  work  if

each term in vec1 was unique, and there is no guarantee of that:

s = 0

for x in vec1:



i = vec1.index(x) # bad idea due to repeats

s += vec1[i] * vec2[i]

Also, getting the index in this way is relatively slow. So overall it is much better to stick

with the range() or enumerate() functions.





Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   49   50   51   52   53   54   55   56   ...   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