001、方法1
root@PC1:/home/test3# ls test.py root@PC1:/home/test3# cat test.py ## 测试程序 #!/usr/bin/python result = "" str1 = "ACGTACGTACGTCACGTCAGCTAGAC" ## 测试字符串 for i in reversed(str1): if i == "A": result += "T" elif i == "T": result += "A" elif i == "C": result += "G" elif i == "G": result += "C" print(result) root@PC1:/home/test3# python test.py ## 程序运行结果 GTCTAGCTGACGTGACGTACGTACGT
002、方法2
root@PC1:/home/test3# ls test.py root@PC1:/home/test3# cat test.py ## 测试程序 #!/usr/bin/python str1 = "ACGTACGTACGTCACGTCAGCTAGAC" dict1 = {"A":"T", "T":"A", "C":"G", "G":"C"} result = [dict1[k] for k in reversed(str1)] print("".join(result)) root@PC1:/home/test3# python test.py ## 运行程序 GTCTAGCTGACGTGACGTACGTACGT
参考:https://www.jianshu.com/p/2475c3240a67