我想將一個字符串拆分成隨機大小的小塊。例如,Python爲循環的每次迭代生成範圍函數的隨機步驟
string = 'qwertyuiopp'
成['qw','e','rty,'u','iopp']
for record in SeqIO.parse(args.fasta_file , "fasta"):
step = 200
for i in range(0, len(record),step):
oline = ">"+record.id+"_"+str(i+1)+"\n"
ofname.write(oline)
step = random.randrange(200,2000)
if len(record.seq[i:i+step]) >= 200:
oline= str(record.seq[i:i+step])+"\n"
ofname.write(oline)
i=i+step
else:
oline= str(record.seq[i-200:])+"\n"
ofname.write(oline)
i=len(record)+1
我的數字的問題是,在範圍函數的步驟仍然是修改使用randrange步長的值的常數(200)inspite。但我不知道我應該如何去做這件事。感謝任何幫助。
嘗試random.randint(200,2000)代替random.randrange(200,2000)。 –
爲什麼不簡單地生成隨機數的隨機索引,對生成的索引進行排序,然後構建子串? – Bakuriu
@Bakuriu:用這種方法獲得相同的分佈並不是微不足道的。 – abarnert