如何一次替换多个整数 [英] How to replace multiple integers at once

查看:85
本文介绍了如何一次替换多个整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手并且遇到了一些问题...


我想弄清楚如何一次更改多个整数所以我可以生成这个的补充DNA链...

>>> dna =""" tgaattctatgaatggactgtccccaaagaagtaggacccactaatg cagatcctgga

tccctagctaagatgtattattctgctgtgaattcgatcccactaaagat"""

(即acttaagatactt .....)


我学会了如何使用

>>>删除\ n字符dna =替换(dna,?\ n?,"?)

但我无法弄清楚如何编写一个脚本,将t改为a,a到t,g到c和c到g。


如果我单独更换它们

>>> dna =替换(dna,?a?,t?)

然后所有a'确实变为t'然后真正的t'无法区分。然后我想到把它们变成w,x,y,z,然后将它们改为完成,但这也需要花费太多时间。


有什么方法可以写

>>> dna =替换(dna,''a'',t)并替换(dna,''t'',a...)等等,所以它会一次改变所有4个???


任何帮助将不胜感激:)

谢谢,Lel

Hi, i am new to python and having some problems...

I am trying to figure out how to change multiple integers at once so i can produce the complement of this DNA strand...
>>>dna = """tgaattctatgaatggactgtccccaaagaagtaggacccactaatg cagatcctgga
tccctagctaagatgtattattctgctgtgaattcgatcccactaaagat """
(ie. acttaagatactt.....)

I have learnt how to remove \n character using
>>> dna = replace(dna, ?\n?, "?)
but i cannot figure out how to write a script that will change t to a, a to t, g to c and c to g.

If i change them individually
>>> dna = replace(dna, ?a?, "t?)
then all the a''s do become t''s then the true t''s cannot be distinguished. I then thought of making them w,x,y,z then changing them to the complent but that also takes too much time.

is there some way to write
>>> dna = replace(dna, ''a'', "t") and replace(dna, ''t'', "a")...and so on so it will change all 4 at once???

Any help would be greatly appreciated :)
Thanks, Lel

推荐答案

你的意思"字符"而不是整数?定义转换字典并使用字符串join()方法和列表推导来连续转换每个字母。字符串是一种序列。我们也可以跳过换行符和任何其他无效字符。
You meant "characters" instead of "integers"? Define a conversion dictionary and use a the string join() method and a list comprehension to convert each letter in succession. A string is a type of sequence. We can also skip the newline and any other invalid character.
展开 | 选择 | Wrap | 行号


好的,谢谢,我真的不懂dnacomp命令,但是当我进一步通过培训手册时,我会回头看看它。

作为一种简单的方法我已经发现它可以使用


>>>替换(替换(替换(替换(dna,''a') ','T"),''',A,g,C,c,G)

但我假设这种格式在我尝试编写更难的脚本时是不够的


再次感谢:)
Ok thanks, i dont really understand the dnacomp command but ill look back at it when im further through the training manual.
As a simple way ive figured out that it can be done using

>>>replace(replace(replace(replace(dna, ''a'', "T"), ''t'', "A"), ''g'', "C"), ''c'', "G")

but im assuming this format will not be sufficient when im trying to write harder scripts

Thanks again :)


这不是一个命令,而是一个赋值。我将 str 对象分配给名为 dnacomp 标识符 使用字符串方法 join() 字典 列表理解 的。这是完成同样事情的另一种方法:
It is not a command, but an assignment. I assigned a str object to an identifier named dnacomp using string method join(), a dictionary, and a list comprehension. Here''s another way to accomplish the same thing:
展开 | 选择 | Wrap | 行号


这篇关于如何一次替换多个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆