首页 > 编程语言 >python中生成指定序列的反向互补序列

python中生成指定序列的反向互补序列

时间:2022-08-18 07:44:05浏览次数:59  
标签:py 互补 python PC1 test result 序列 home root

 

001、方法1:

root@PC1:/home/test# ls
test.py
root@PC1:/home/test# cat test.py               ## 测试程序
#!/usr/bin/python

out_file = open("result.txt", "w")
str1 = "AAAACCCGGT"                            ## 转换序列
str1 = str1.upper()[::-1]
result = []
for i in str1:
    if i == "A":
        result.append("T")
    elif i == "T":
        result.append("A")
    elif i == "C":
        result.append("G")
    elif i == "G":
        result.append("C")
print("".join(result), end = "\n", file = out_file)

out_file.close()
root@PC1:/home/test# python test.py
root@PC1:/home/test# ls
result.txt  test.py
root@PC1:/home/test# cat result.txt             ## 转换结果
ACCGGGTTTT

 

002、方法2

root@PC1:/home/test# ls
test.py
root@PC1:/home/test# cat test.py              ## 测试程序
#!/usr/bin/python
out_file = open("result.txt", "w")
str1 = "AAAACCCGGT"
str1 = str1.upper()[::-1]

trantab = str.maketrans("ATCG", "TAGC")
result = str1.translate(trantab)                 ## 利用字符串的内置函数 translate函数实现
out_file.write(result + "\n")

out_file.close()
root@PC1:/home/test# python test.py
root@PC1:/home/test# ls
result.txt  test.py
root@PC1:/home/test# cat result.txt              ## 转换结果
ACCGGGTTTT

 

3、方法3

root@PC1:/home/test# ls
test.py
root@PC1:/home/test# cat test.py                         ## 测试程序
#!/usr/bin/python

out_file = open("result.txt", "w")
str1 = "AAAACCCGGT"
str1 = str1.upper()[::-1]
dict1 = {"A":"T", "T":"A", "C":"G", "G":"C"}

result = "".join([dict1[k] for k in str1])
out_file.write(result + "\n")

out_file.close()

root@PC1:/home/test# python test.py                      ## 执行程序
root@PC1:/home/test# ls
result.txt  test.py
root@PC1:/home/test# cat result.txt                      ## 运行结果
ACCGGGTTTT

 

4、方法4

root@PC1:/home/test# ls
test.py
root@PC1:/home/test# cat test.py                     ## 测试程序
#!/usr/bin/python

out_file = open("result.txt", "w")
result = ""
str1 = "AAAACCCGGT"
dict1 = {"A":"T", "T":"A", "C":"G", "G":"C"}
for i in str1:
    result = dict1[i] + result

print(result, end = "\n", file = out_file)

out_file.close()
root@PC1:/home/test# python test.py                  ## 执行程序
root@PC1:/home/test# ls
result.txt  test.py
root@PC1:/home/test# cat result.txt                  ## 执行结果
ACCGGGTTTT

 

标签:py,互补,python,PC1,test,result,序列,home,root
From: https://www.cnblogs.com/liujiaxin2018/p/16597460.html

相关文章

  • 微信的消息序列号生成实践
    学习一下微信是怎么搞定序列号的。从seqsvr申请的、用作数据版本号的sequence,具有两种基本的性质:1. 递增的64位整型变量2.每给用户有自己独立的64位sequence空间。......
  • Python学习
    常用快捷键ctrl+alt+s:打开软件设置ctrl+d:复制当前行代码shift+alt+上\下:将当前行代码上移或下移ctrl+shift+F10:运行当前代码文件shift+......
  • P5080 Tweetuzki 爱序列 题解
    题目传送门更好地阅读体验题目大意Tweetuzki有一个长度为\(n\)的序列\(a_1,a_2,\cdots,a_n\)。他希望找出一个最大的\(k\),满足在原序列中存在一些数\(b_1,b......
  • python学习Day47
    Day47今日内容概要操作表的SQL语句补充表查询关键字select与fromwhere筛选groupby分组having过滤distinct去重orderby排序limit分页regexp正则......
  • python装饰器
    1.简介​ Python中的装饰器是一种可以装饰其它对象的工具。类似于装饰模式,实际是对原有对象进行功能上的增强(原有对象可以是对象、函数、类)。其使用像java的注解。​......
  • 关于python如何引用excel文件
    关于python如何引用excel文件importpandasaspd#引用pandas库,as:将pandas简写为pdNowcoder=pd.read_excel("1.xlsx")#使用read_XXX函数,()内的是文件名.#需要注......
  • python序列-列表
    列表-LISTpython中的列表是有一系列按特定顺序排列的元素组成,列表是属于python内置的可变序列。语法上列表的元素都是放在一对中括号‘[]’中,两个相邻的元素之间使用......
  • python 代码测试(pytest)
    前话代码测试用于检验代码运行结果是否符合预期。优势一:编写测试函数,更规范,高效的核对代码运行结果,当被测试对象进行了调整和重构的时候,可以节省大量人工排查问题的时间......
  • Python抓取汇率并绘制成折线图
    公司的一个小工作,需要抓取美元、欧元汇率并绘制成折线图。很小的功能,背后却涉及很多Python知识,甚至一些冷门的知识点。知识点总结如下:1.python抓取网页数据,利用pandas.rea......
  • # Conda虚拟环境中的pip,python 等路径是base环境而非本虚拟环境
    现象一次运行项目发现,原本可以正常运行的项目,突然提示有个包不存在,但是经过piplist发现在我的虚拟环境中是存在这个包的,并且此时我是正常的位于我的虚拟环境中。报错......