# 使用ROSALIND平台进行Python实战练习（一）

Python Village

# 安装Python并在命令行模式下输入import this查看Python之禅

>>>importthis

TheZenofPython,byTimPeters

Beautifulisbetterthanugly.

Explicitisbetterthanimplicit.

Simpleisbetterthancomplex.

Complexisbetterthancomplicated.

Flatisbetterthannested.

Sparseisbetterthandense.

Specialcasesaren't special enough to break the rules.

Althoughpracticalitybeatspurity.

Unlessexplicitlysilenced.

Inthefaceofambiguity,refusethetemptationtoguess.

Thereshouldbeone--andpreferablyonlyone--obviouswaytodoit.

Althoughthatwaymaynotbeobviousatfirstunlessyou're Dutch.

Nowisbetterthannever.

Althoughneverisoftenbetterthan*right*now.

Iftheimplementationiseasytoexplain,itmaybeagoodidea.

Namespacesareonehonkinggreatidea--let's do more of those!

INI2--Variables and Some ArithmeticProblemGiven: Two positive integers aa and bb, each less than 1000.Return: The integer corresponding to the square of the hypotenuse of the right triangle whose legs have lengthsaandb.

# 求边长为a和b的直角三角形的斜边的平方和

>>>a=3

>>>b=5

>>>c=a**2+b**2

>>>print(c)

34

INI3--Strings and ListsProblemGiven: A stringsof length at most 200 letters and four integersa,b,candd.Return: The slice of this string from indicesathroughbandcthroughd(with space in between),inclusively. In other words, we should include elements s[b] and s[d] in our slice.

# 从一个字符串中按指定索引位置提取子字符串

>>>s="WPedostibesprNckwovQydlBbDTX2atKZFiQWkgoppnjtPBJG8VuxVnWH5mDZIClB9Kgf3Wz5U1imodestusKXvI3Zs5gLS4W8r43ZCYQgxGeCv1eWnQveLa9PJZNo3lzjWdmDrfIN7dA1EhOC0NRVuRtHcPaHVu5snowPwjjBUNeDvH0yka33OxLCACg7d0Q."

>>>a,b,c,d=1,10,76,83

>>>print(s[a:b+1])

Pedostibes

>>>print(s[c:d+1])

modestus

INI4--Conditions and LoopsProblemGiven: Two positive integersaandb(a Return: The sum of all odd integers fromathroughb, inclusively.

# 计算a和b之间所有奇数的累加和

>>a,b=4908,9604

>>>sum=

>>>foriinrange(a,b+1):

...ifi%2==1:# 判断i是否为奇数

...sum+=i

...

>>>print(sum)

17037088

INI5--Working with FilesProblemGiven: A file containing at most 1000 lines.Return: A file containing all the even-numbered lines from the original file. Assume 1-based numbering of lines.

# 读入一个文本文件，循环输出偶数行

>>>withopen("rosalind_ini5.txt","r")asfh:

...fori,lineinenumerate(fh):

...ifi%2==1:# i是从0开始的

...print(line,end="")

...

Otherthingsjustmakeyouswearandcurse

Whenyou're chewing on life'sgristle,don't grumble give a whistle

Thiswillhelpthingsturnoutforthebest

Alwayslookonthebrightsideoflife

Alwayslookontherightsideoflife

Iflifeseemsjollyrotten,there's something you'veforgotten

Andthat's to laugh and smile and dance and sing

Whenyou're feeling in the dumps, don'tbesilly,chumps

Justpurseyourlipsandwhistle,that's the thing

So,alwayslookonthebrightsideofdeath

Justbeforeyoudrawyourterminalbreath

Life's a counterfeit and when you look at it

Life's a laugh and death'sthejoke,it's true

Yousee,it's all a show, keep them laughing as you go

Justrememberthelastlaughisonyou

Alwayslookonthebrightsideoflife

Andalwayslookontherightsideoflife

Alwayslookonthebrightsideoflife

Andalwayslookontherightsideoflife

INI6--DictionariesProblemGiven: A file containing at most 1000 lines.Return: A file containing all the even-numbered lines from the original file. Assume 1-based numbering of lines.

# 统计一个字符串中每个单词出现的频数，以空格分割

>>>s="When I find myself in times of trouble Mother Mary comes to me Speaking words of wisdom let it be And in my hour of darkness she is standing right in front of me Speaking words of wisdom let it be Let it be let it be let it be let it be Whisper words of wisdom let it be And when the broken hearted people living in the world agree There will be an answer let it be For though they may be parted there is still a chance that they will see There will be an answer let it be Let it be let it be let it be let it be There will be an answer let it be Let it be let it be let it be let it be Whisper words of wisdom let it be Let it be let it be let it be let it be Whisper words of wisdom let it be And when the night is cloudy there is still a light that shines on me Shine until tomorrow let it be I wake up to the sound of music Mother Mary comes to me Speaking words of wisdom let it be Let it be let it be let it be yeah let it be There will be an answer let it be Let it be let it be let it be yeah let it be Whisper words of wisdom let it be"

>>>mydict= {}

>>>forwordins.split(" "):

...ifwordinmydict:

...mydict[word] +=1

...else:

...mydict[word] =1

...

>>>forkey,valueinmydict.items():

...print(key+" "+str(value))

...

When1

I2

find1

myself1

in4

times1

of11

trouble1

Mother2

Mary2

comes2

to3

me4

Speaking3

words7

wisdom7

let30

it36

be41

And3

my1

hour1

darkness1

she1

is4

standing1

right1

front1

Let6

Whisper4

when2

the4

broken1

hearted1

people1

living1

world1

agree1

There4

will5

an4

For1

though1

they2

may1

parted1

there2

still2

a2

chance1

that2

see1

night1

cloudy1

light1

shines1

on1

Shine1

until1

tomorrow1

wake1

up1

sound1

music1

yeah2

# another way

>>>words=s.split(" ")

>>>forwordinwords:

...mydict[word] =words.count(word)

...

>>>forkey,valueinmydict.items():

...print(key,value)

# another way

>>>words=s.split(" ")

>>>forwordinwords:

...mydict[word] =1+mydict.get(word,)#利用字典自带的get方法

...

>>>forkey,valueinmydict.items():

...print(key,value)

...

Bioinformatics Stronghold

Counting DNA NucleotidesProblemAstringis simply an ordered collection of symbols selected from somealphabetand formed into a word; thelengthof a string is the number of symbols that it contains.An example of a length 21DNA string(whose alphabet contains the symbols 'A', 'C', 'G', and 'T') is "ATGCTTCAGAAAGGTCTTACG."Given: A DNA stringsof length at most 1000 nt.Return: Four integers (separated by spaces) counting the respective number of times that the symbols 'A', 'C', 'G', and 'T' occur ins.

# 输入一个DNA序列的字符串，统计这个DNA序列中A,C,G,T四个字符的数目，并以空格分隔输出

>>>DNA="TGAGTTCCGTATCGTGAAAAAGGAAGGTTAGCCAGCCTTCTCACACGATGGCTATGTTGATTTGGTCGTAGGGACACTAGATACGCTGTTTCTCTTCTTAAAGATGGAACGATGTAACGCTGGATGGAAACTGAACTAGATAAGCCAGTAGTGTAAATGTTCGTACCTGACATATCACTGTTGCAAAATTGGGGATCTATCGAAGATCAGCGCACGCGCTTCAATTGTGTTTATGGACAAGGTCTTGCGGTCGATAGGTCTTCAGACAGGCAGCGTTCCCGCGACCTTCCTCGCGATTCCTCGGATGTGGAGTGGTAGAGAACAGCGATTGGTGCCGACCTTCTGAGGAAGAACCGTGCCAGTTTGCGACATTCGTCCCAACAAATTAACCATATCACGTTAAGGTTAGCCCCCTACTAAGTACAAAGACTGGGCATCAGAAGGGCTGCCCATGTAGATGATTTGTTTTGTCAAACAAACGCGTCCGGTGGGGGCGACTTAACCACCTTCTGGCGGTGCACATTACTGGGTTCTTGAGGTGCTAATGAGCATTGCGTTGAGCGTATCAATAACGCGACTGGCGATACACCGACAGGGTATTCCGTCTGGCGCCCTGAGACGGGGTTGGGACTCCGATAAGACGTGGTGCGACCCACCGACGTGTAAAACGCAGAAGTCTCTCCTCTACATCATTGTTAAACCGGGCTAGACAGCTGCGGTCTTCTCTCATAGGCACCACCCCAGTACCCTGCCAATAGTGCATCGTGTTCTGTGAGCAAGTGTTGTTGCGGTCGCACTTGTTCTTTGTCATACAGAGGCTCCTCCTCGACTTAAGACATGTTGAAACTGTCTCGCACTGACACGCAAGGTCGGTTCACAGTTCGACCGAGCTTCAGAATGCCCAATAAAGCCTTTTCAAATAATACAACCCTGAATGCAGTCCACACCACTTTA"

>>>mydict= {}

>>>foriteminDNA:

...mydict[item] =1+mydict.get(item,)

...

>>>foriin"ACGT":

...print(mydict[i],end=" ")

...

237234240243

# another way

>>>count_A,count_C,count_G,count_T= [DNA.count(nuc)fornucin"ACGT"]#列表推导式，利用字符串自带的count方法进行计数

>>>print(count_A,count_C,count_G,count_T)

237234240243

# another way

>>>count_A=DNA.count("A")

>>>count_C=DNA.count("C")

>>>count_G=DNA.count("G")

>>>count_T=DNA.count("T")

>>>print(count_A,count_C,count_G,count_T)

237234240243

Transcribing DNA into RNAProblemAnRNA stringis astringformed from thealphabetcontaining 'A', 'C', 'G', and 'U'.Given aDNA stringtcorresponding to a coding strand, its transcribedRNA stringuu is formed by replacing all occurrences of 'T' intwith 'U' inu.Given: ADNA stringthavinglengthat most 1000nt.Return: The transcribed RNA string oft.

# 输入一个DNA序列的字符串,将其转换为RNA序列进行输出

>>>DNA="GTTTTGTCTAAGGCCCCGAAAATGATCTTGGACACCCCTCTCCCGATTTTCACTGCGATTTCACTTTTCATCCGGACGCCTGGTAGCGCAGGTATAAACTAAGGAGCAAGACGCTTGAAAGTTGTTAGGATAGATGCGGCCATGCCATATGCTGATATCCAGATGGTCGGTTTAACTTTATTTGGTCGAACCGTAGTACGGGGGAAACCGACCATAATGTCTATTGATTCCAGGTGTTAACCCATCAAACGCAGGCCAACATTCTCCGTATTGAGTTCACCCATCGATCTCACAGCGGTTAGGAACACGAGTGATCTAGTCGGCACTTACGCGCCGCTAGCGGGGTGCTCTACGAGTATTGCATAATGACGAGATAAAATGGGACCGTATCAATCCGGATTTCTAACGACTTGCTCCCAAAGGATACGTTACGTAATTAACTCAGGCATAGGCGTCATGGCATGTAATTTACTATGTCGGGCGCACGTAGTAATCCCTACCAAGCAGCGTAGACTAGCGAAAGTCCCCTGGTACAGTATTGTTCTTCTGAAGTCGCGTACTCCCCCAGTTCCTAGAAAAATGGGTAAATGGACTCTCGCTCGTCTCCTCGCTGGTTGGTCATGGCGTTGGGTCTGCTTTACCCCTCCAAGCCGCACGTAGCCGCCTACAGTAAATCGGGGGTGCTGGCTTACCGACGTTAAGTTGCTCCAACTACGGTGGACTGGTATCACCTACTAGTTCCCTATAGCGCGTTCTGGCTGGTTCCTAGCTGGGCACTAGGTACGGGAATTCCCCCGTACCAGACTAAAAGGAATGTAGAACGTGATGGGCATTGACACTGAACTCCTTTTTCGGGCCGGTCAGGTTGCATGGGCAACGGCCTACAACGACGTCAAACTCACCATGGCGGGGTTCCTGGGTATGCGTGCCGCTCAGGTTCTACCCTCTACCAACGAGGACGCCAACAACTGATTAC"

>>>RNA= []

>>>foriteminDNA:

...ifitem=="T":

...item="U"

...RNA.append(item)

...else:

...RNA.append(item)

...

>>>print("".join(RNA))

GUUUUGUCUAAGGCCCCGAAAAUGAUCUUGGACACCCCUCUCCCGAUUUUCACUGCGAUUUCACUUUUCAUCCGGACGCCUGGUAGCGCAGGUAUAAACUAAGGAGCAAGACGCUUGAAAGUUGUUAGGAUAGAUGCGGCCAUGCCAUAUGCUGAUAUCCAGAUGGUCGGUUUAACUUUAUUUGGUCGAACCGUAGUACGGGGGAAACCGACCAUAAUGUCUAUUGAUUCCAGGUGUUAACCCAUCAAACGCAGGCCAACAUUCUCCGUAUUGAGUUCACCCAUCGAUCUCACAGCGGUUAGGAACACGAGUGAUCUAGUCGGCACUUACGCGCCGCUAGCGGGGUGCUCUACGAGUAUUGCAUAAUGACGAGAUAAAAUGGGACCGUAUCAAUCCGGAUUUCUAACGACUUGCUCCCAAAGGAUACGUUACGUAAUUAACUCAGGCAUAGGCGUCAUGGCAUGUAAUUUACUAUGUCGGGCGCACGUAGUAAUCCCUACCAAGCAGCGUAGACUAGCGAAAGUCCCCUGGUACAGUAUUGUUCUUCUGAAGUCGCGUACUCCCCCAGUUCCUAGAAAAAUGGGUAAAUGGACUCUCGCUCGUCUCCUCGCUGGUUGGUCAUGGCGUUGGGUCUGCUUUACCCCUCCAAGCCGCACGUAGCCGCCUACAGUAAAUCGGGGGUGCUGGCUUACCGACGUUAAGUUGCUCCAACUACGGUGGACUGGUAUCACCUACUAGUUCCCUAUAGCGCGUUCUGGCUGGUUCCUAGCUGGGCACUAGGUACGGGAAUUCCCCCGUACCAGACUAAAAGGAAUGUAGAACGUGAUGGGCAUUGACACUGAACUCCUUUUUCGGGCCGGUCAGGUUGCAUGGGCAACGGCCUACAACGACGUCAAACUCACCAUGGCGGGGUUCCUGGGUAUGCGUGCCGCUCAGGUUCUACCCUCUACCAACGAGGACGCCAACAACUGAUUAC

# another way

>>>RNA=DNA.replace("T","U")#利用字符串自带的replace方法进行替换

>>>print(RNA)

Complementing a Strand of DNAProblemInDNA strings,symbols'A' and 'T' are complements of each other, as are 'C' and 'G'.Thereverse complementof aDNA stringss is the string scsc formed by reversing the symbols of ss, then taking the complement of each symbol (e.g., the reverse complement of "GTCA" is "TGAC").Given: A DNA stringsof length at most 1000bp.Return: The reverse complementscofs.

# 输入一个DNA序列的字符串,将其转换为反向互补序列进行输出

>>>DNA="TGCAGCACACTGGGTCGGGCTTTTGCTTGTATTATCTCACATAAGGGTAGCATAAACCTCTTAAATCGGTCAGTTTTCCTTGGAGTAGGTTGTTACCATGGGCAAACCATTCCCAGTGGTAAGATACCGAGCAGCGTACGCGACAGGCTGTCTGATCGGACAATCGGAGTACAGAACCACCCATTTGAAACACATGCTTTAGCGCTTGGCCCTCACGAAGCCCCATACAGTCACCACCCTCTAATCTTTGGCGTTACTGTGCAGGCGCCGAGAGATTAAACTTTCAATAGTGAGGGCCTAAAACCCTATCTTAGTGACTAACCAGATATATCGTTCATTCTCATTCCGTGCCCCCCAACCTCATTATAATTACCCGAAGAGTTAACGCCTTGAGAACATCTTCACGCGGGAGCACAAGTCCGAGACTGACACCGACGCAAATGGGTGATTCTTTGGGTCACGGATCGGGATTGCCGGAAACCAATAAATTAATTACTTATACCGTCCTCGTATTCGTTCCCCCAATGCGAGCTCAAAACTATAACTAATGAAGTTTTCCATTAACCCGGTCCATACATCATCGTTTCAGGACAAAAAATTTCAGGGAGCAGCCTCACTTGTGCTAGCCTAAAAATTACATCTTTGATACACATAGTAATTGGTGATATTTAGCTGCCTCCAGCACCGGTGGAGCACAGTCATGCCGAGTGTTAGACGCCATAAGGGAGTCGGCAATATAGACGAGCCATTTTTACAAATATGCGTTGGTCGACTACCATTGCTCGGAGGACAGGCTGTTTTTACCAAAGGGCGCTCTCATGAATCCCCACACCACTAGCGAACTTGTCGAACTTC"

>>>mydict= {"A":"T","T":"A","C":"G","G":"C"}

>>>DNA_revc= []

>>>foriteminDNA:

...ifiteminmydict:

...DNA_revc.append(mydict[item])

...

>>>DNA_revc.reverse()# 利用列表自带的reverse函数对原字符串进行反转

>>>print("".join(DNA_revc))

GAAGTTCGACAAGTTCGCTAGTGGTGTGGGGATTCATGAGAGCGCCCTTTGGTAAAAACAGCCTGTCCTCCGAGCAATGGTAGTCGACCAACGCATATTTGTAAAAATGGCTCGTCTATATTGCCGACTCCCTTATGGCGTCTAACACTCGGCATGACTGTGCTCCACCGGTGCTGGAGGCAGCTAAATATCACCAATTACTATGTGTATCAAAGATGTAATTTTTAGGCTAGCACAAGTGAGGCTGCTCCCTGAAATTTTTTGTCCTGAAACGATGATGTATGGACCGGGTTAATGGAAAACTTCATTAGTTATAGTTTTGAGCTCGCATTGGGGGAACGAATACGAGGACGGTATAAGTAATTAATTTATTGGTTTCCGGCAATCCCGATCCGTGACCCAAAGAATCACCCATTTGCGTCGGTGTCAGTCTCGGACTTGTGCTCCCGCGTGAAGATGTTCTCAAGGCGTTAACTCTTCGGGTAATTATAATGAGGTTGGGGGGCACGGAATGAGAATGAACGATATATCTGGTTAGTCACTAAGATAGGGTTTTAGGCCCTCACTATTGAAAGTTTAATCTCTCGGCGCCTGCACAGTAACGCCAAAGATTAGAGGGTGGTGACTGTATGGGGCTTCGTGAGGGCCAAGCGCTAAAGCATGTGTTTCAAATGGGTGGTTCTGTACTCCGATTGTCCGATCAGACAGCCTGTCGCGTACGCTGCTCGGTATCTTACCACTGGGAATGGTTTGCCCATGGTAACAACCTACTCCAAGGAAAACTGACCGATTTAAGAGGTTTATGCTACCCTTATGTGAGATAATACAAGCAAAAGCCCGACCCAGTGTGCTGCA

# another way

>>>mydict= {"A":"T","T":"A","C":"G","G":"C"}

>>>DNA_revc= []

>>>foriteminreversed(DNA):# 利用reversed函数对字符串进行反转，或着反向索引DNA[::-1]进行反转

...DNA_revc.append(mydict[item])

...

>>>print("".join(DNA_revc))

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20181012G0BFUP00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。

2019-10-19

2019-10-19

2019-10-19

2019-10-19

2019-10-19

2019-10-19

2019-10-19