我想问任何熟悉numpy的人有两个问题.我见过非常相似的问题(和答案),但没有一个使用我想使用的numpy,因为它提供了许多其他选项,我可能希望将来在该代码中使用.我试图在python中使用"random"生成一个随机核苷酸序列列表.因为我想要有不统一的概率,所以我决定使用numpy.但是,我收到错误消息:"ValueError:a必须是1维或整数".
import numpy as np
def random_dna_sequence(length):
return ''.join(np.random.choice('ACTG') for _ in range(length))
with open('dna.txt', 'w+') as txtout:
for _ in range(10):
dna = random_dna_sequence(100)
txtout.write(dna)
txtout.write("\n")
print (dna)
Run Code Online (Sandbox Code Playgroud)
我是一个完整的磨砂膏,我无法弄清楚多维度在何处或如何发挥作用.我怀疑".join()",但我不确定,也不确定如何更换它.我的另一个问题是如何获得非均匀概率.我试过"np.random.choice('ACTG',p = 0.2,0.2,0.3,0.3)",但它不起作用.
我希望有人可以提供帮助.提前致谢.
问候,伯特