Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet459/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   455   456   457   458   459   460   461   462   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Operation/Method

Description

Example

len(s)


Determines

the number

of items

within a set;

its size.

s = {1,2,3,2,1}

len(s) # 3 – no repeats

len(set()) # 0 - Empty

x in s

x not in s



Determines

whether an

item is

within a set,

or not.

s = {'G','C','A','T'}

'G' in s # True

'G' not in s # False

s.isdisjoint(other)

Determines

whether one

set has no

items in

common


with

s = {1,2,3}

t = {4,5,6}

s.isdisjoint(t) # True




another.

s.issubset(other)

or

set <= other



Determines

whether all

of the set’s

items are

contained

within


another set.

s = {1,2,3,4}

t = {2,4}

s.issubset(t) # False

t <= s # True

s <= s # True

set < other

Determines

whether all

of the set’s

items are

contained

within

another set



and the sets

are not the

same.

s = {1,2,3,4}



t = {2,4}

t < s # True

s < s # False

s.issuperset(other)

or

set >= other



Determines

whether all

the items of

another set

are

contained in



this set.

s = {1,2,3,4}

t = {2,4}

s.issuperset(t) # True

t >= s # False

s >= s # True

set > other

Determines

whether all

the items of

another set

are


contained in

this set and

the sets are

not the


same.

s = {1,2,3,4}

t = {2,4}

s > t # True

t > s # False

s > s # False

s.union(other, …)

or

set | other | ...



Generates a

new set that

contains all

the items

that are

present in

a = {1,2}

b = a.union({2,3},{3,4})

c = {1,2} | {2,3} | {3,4}

# b,c both {1,2,3,4}




any of a

group of


sets.

s.intersection(other, …)

or

set & other & …



Generates a

new set that

contains

only items

that are

present in

all the sets

in a group.

{1,2,3}.intersection({2,3,4})

# {2,3}


{1,2} & {2,3} & {3,4}

# set() – Empty

s.difference(other, …)

or

set - other - …



Creates a

copy of a

set with any

items that

are

common to



other sets

removed.


{0,1,2,3}.difference({2,3,4})

# {0,1}


{1,2,3,4,5} - {2,5} - {2,3}

# {1, 4}


s.symmetric_difference(other)

or

set ^ other



Generates a

new set


from two

sets by


selecting

only items

that appear

in one but

not both of

the sets.

{0,1,2,3} ^ {2,3,4} ^ {2,5}

# {0, 1, 2, 4, 5}

s.copy()

Creates a

copy of a

set; a


separate

Python


collection

with the


same items.

Same as s2

= set(s1).

s = {1,2,3}

t = s.copy()

t.add(4)


# t is {1, 2, 3, 4}

s.update(other, …)

set |= other | …

Adds any


items which

s = {0}



are not

already


present

from one or

more other

sets.


s.update({1,2})

s |= {2,4,9}

# {0, 1, 2, 4, 9}

s.intersection_update(other, …)

or

set &= other & …



Modifies a

set by


adding any

items that

are

common to



all the sets

in a group.

s = {1,2,4,9}

s &= {1,2,3,4}

# s is {1, 2, 4}

s.difference_update(other, …)

or

set -= other | …



Modifies a

set by


removing

any items

that are

present in a

group of

other sets.

s = {1,2,4,9}

s -= {1,2,3,4}

# s is {9}

s.symmetric_difference_update(other)

or

set ^= other



Modifies a

set so it

contains

items that

are only

present in

one, but not

both, of the

two sets.

s = {1,2,4,9}

s ^= {1,2,3,4}

# s is {9,3}

s.add(obj)

Adds a


single item

to a set, if it

is not

already


present in

the set.


s = {1,2,3,4}

s.add(5)


s.add(3)

# s is {1, 2, 3, 4, 5}

s.remove(obj)

Removes a

single item

s = {1,2,3,4}

s.remove(2)



from a set,

assuming it

is present in

the set;


gives a

KeyError if

not.

# s is {1, 3, 4}



s.discard(obj)

Removes a

single item

from a set,

if it is

present in

the set. If

the item is

not in the

set there is

no error.

s = {1,2,4,9,16}

s.discard(5) # No effect

s.discard(1)

# s is {16, 9, 2, 4}

s.pop()


Removes

and passes

back a

single,


arbitrary

item from a

set, making

the set


smaller. If

the set is

empty it

gives a


KeyError.

s = {1,2,4,9,16}

s.pop()

# 16(for example)

# s becomes {9, 2, 4, 1}

s.clear()

Removes

all the items

from a set,

generating

an empty

set.


s = {1,2,4,9,16}

s.clear()

# s is set() – empty

Dictionary operations

The  following  table  lists  the  common  operations  that  may  be  used  with  dictionaries.

Dictionaries may be created from other collections (containing key, value pairs) using the

inbuilt dict(collection) or explicitly using the curly brace notation {k1: v1, k2:v2}. Using




dict() with no arguments creates an empty dictionary, as does {}. It should be noted that

only hashable objects, which do not allow modification of their innate value, can be used

as  dictionary  keys;  this  excludes  lists,  sets  and  other  dictionaries  but  includes  tuples,

frozen sets, strings, integers, floating point numbers and most other Python objects.



Operation

Description

Example

len(d)


Determines the number of

(key:value) pairs in a dictionary

d1 =

{'G':3,'C':3,'A':2,'T':2}



len(d1) # 4

d2 = {'pi':3.141,

'e':2.718}

len(d2) # 2

d[key]

Retrieves the value from a



dictionary that is associated with a

given key, giving a KeyError if the

key is not present.

d =


{'FR':33,'DE':49,'GB':44}

d['GB'] # 44

d['DE'] # 49

d[key] =


value

Sets the value for a specified key.

d =

{'FR':33,'DE':49,'GB':44}



d['ES'] = 34

# d is {'FR': 33, 'DE':

49,

# 'GB': 44, 'ES': 34}



del d[key]

Removes a specified (key:value)

pair from the dictionary.

d = {1:'G', 2:'C',

3:'A', 4:'T'}

del d[4]


# d is {1:'G', 2:'C',

3:'A'}


key in d

key not in

d

Determines whether a key is used



by a dictionary, or not.

d = {1:'G', 2:'C',

3:'A', 4:'T'}

1 in d # True

1 not in d # False

'A' in d # False – not a

key

iter(d)


Creates an iterator object from a

dictionary, which provides an

alternative way of looping through

all the keys.

d = {'G':3,'C':3,

'A':2,'T':2}

iterObj = iter(d)

for key in iterObj:

print(key)



Method

Description

Example

d.clear()

Removes all key:value

pairs from a dictionary,

leaving an empty

dictionary.

d = {'pi':3.141,

'e':2.718}

d.clear()

# {} - Empty

d.copy()

Makes a copy of a

dictionary; a new, separate

Python object with the

same key:value pairs.

Same as d2 = dict(d1).

d1 = {'pi':3.141,

'e':2.718}

d2 = d1.copy()

d2['r2'] = 1.414

# d2 is {'pi': 3.141,

# 'e':2.718, 'r2':

1.414}

d =


dict.fromkeys(seq,

value)

Generates a new dictionary

using keys from a specified

collection. All values will

be set to None or an

optional default.

d = dict.fromkeys('ABC',

0)

# {'A': 0, 'C': 0, 'B':



0}

d.get(key, default)

Retrieves the value from a

dictionary that is

associated with a given

key, and if the key is not in

the dictionary then it gives

None or the specified

optional default.

d = {'pi':3.141,

'e':2.718}

d.get('pi') # 3.141

d.get('mu') # None

d.get('mu', 0.0) # 0.0

d.has_key(key)

Determines whether a

specified key is used in a

dictionary. Deprecated,

use key in dict instead. Not

available in Python 3.

[Deprecated, use "key in

dict" instead.]

d.items()

In Python 2, generates a

list of tuples containing

(key, value) pairs from a

dictionary. In Python 3,

gives an iterable view

object instead of a list.

d =


{'G':3,'C':3,'A':2,'T':2}

d.items()

# dict_item([('A', 2),

('C', 3), ('T', 2), ('G',

3)])

d.iteritems()



Generates an iterator

d = {'G':3,'C':3,




object that can loop

through (key, value) pairs

from a dictionary. Not

available in Python 3, use

d.items() instead.

'A':2,'T':2}

iterObj = d.iteritems()

for key, val in iterObj:

print(key, val)

d.iterkeys()

Generates an iterator

object that can loop

through all keys from a

dictionary. The method

dict1.iterkeys() does the

same as iter(dict1). Not

available in Python 3, use

d.keys() instead.

d = {'G':3,'C':3,

'A':2,'T':2}

iterObj = d.iterkeys()

for key in iterObj:

print(key)

d.itervalues()

Generates an iterator

object that can loop

through all values from a

dictionary. Not available in

Python 3, use d.values()

instead.


d = {'G':3,'C':3,

'A':2,'T':2}

iterObj = d.itervalues()

for value in iterObj:

print(value)

d.keys()


In Python 2, generates a

list containing the keys

from a dictionary. The

items in the list are in no

particular order. In Python

3, gives an iterable view

object instead of a list.

d = {'pi':3.141,

'e':2.718}

k = d.keys()

print(k)

# dict_keys(['pi', 'e'])

d.pop(key, default)

Passes back a value

associated with a specified

key and removes the

(key:value) pair from the

dictionary, substituting an

optional default value if a

key is not present.

d = {'pi':3.141,

'e':2.718}

d.pop('e') # 2.718

d.pop('mu', 0.0) # 0.0

# d is {'pi': 3.141}

d.popitem()

Removes and passes back

an arbitrary key:value pair,

as a tuple, from the

dictionary.

d = {'G':3,'C':3,

'A':2,'T':2}

item = d.popitem()

# d might be

{'C':3,'T':2,'G':3}

# item is then ('A', 2)

d.setdefault(key,

Retrieves the value from a




default)

dictionary that is

associated with a given key

and if a key is not present

adds it to the dictionary

with a value of None or

optional default.

d = {'G':3,'C':3}

d.setdefault('A') # None

d.setdefault('G') # 3

# {'A':None,'C':3,'G':3}

d.update(otherDict)

Adds all the (key:value)

pairs from one dictionary

to another, replacing any

values for keys that are

already present.

d = {'G':3,'C':3}

d.update({'A':2})

# {'A':2, 'C':3, 'G':3}

d.values()

In Python 2, generates a

list containing the values

from a dictionary. The

items in the list are in no

particular order. In Python

3, gives an iterable view

object instead of a list.

d = {'pi':3.141,

'e':2.718}

v = d.values()

print(v)


# dict_values([3.141,

2.718])


File objects

The following table lists commonly used methods and attributes of file objects, which are

generally  created  with  open(fileSystemPath,  readWriteMode)  in  order  to  read  data  from

and/or write data to a file system, e.g. hard disk, DVD etc.




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   455   456   457   458   459   460   461   462   ...   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