1
有兩個問題我想問任何熟悉numpy的人。我看過非常相似的問題(和答案),但沒有使用numpy,我想使用它,因爲它提供了很多其他選項,我可能會在未來使用該代碼。 我試圖用python中的「隨機」生成隨機核苷酸序列的列表。因爲我想要有非統一的概率,我決定使用numpy來代替。但是,我收到錯誤消息:「ValueError:必須是1維或整數」。Python - 用Numpy生成隨機DNA序列,ValueError
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)
我是一個完整的磨砂膏,我無法弄清楚多維性在哪裏或如何發揮作用。我懷疑「.join()」,但我不確定,也不確定我可以如何取代它。 我的另一個問題是如何獲得非均勻概率。我試着用「np.random.choice('ACTG',p = 0.2,0.2,0.3,0.3)」但它不起作用。
我希望有人可以幫助。提前致謝。
問候, 伯特
很好用!包含元組的解決方案非常優雅,並且不會混淆代碼,並且可以輕鬆訪問這些值以便與它們一起玩。非常感謝! – grindbert