[英]Python generate random steps for range function in each iteration of a for loop
I am trying to split a string into small chunks of random sizes. 我正在尝试将字符串分成随机大小的小块。 For example,
例如,
string = 'qwertyuiopp'
into ['qw','e','rty,'u','iopp']
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
I figure the problem is that the step in range function remains constant(200) inspite of modifying the value of step using randrange. 我认为问题是,尽管使用randrange修改了step的值,但range函数中的step仍然保持常量(200)。 but I dont know how I should go about this.
但我不知道该怎么做。 Thank for any help.
感谢您的帮助。
range
function creates a list as soon as the for loop is encountered. 一旦遇到for循环,
range
函数就会创建一个列表。 It does not get evaluated for every iteration. 不会为每个迭代进行评估。 You can use a
while
loop for this purpose. 您可以为此使用
while
循环。
import random
step = 1
count = 0
while count < len(record):
print count
step = random.randint(1, 5)
count += step
The range
function does not remember how it got the values it was constructed from. range
函数不记得它是如何获得其构造值的。 When you call range(0, len(record), step)
that creates either the range object range(0, 1000, 200)
(Python 3.x) or the list [0, 200, 400, 600, 800]
(Python 2.x). 当您调用
range(0, len(record), step)
创建范围对象range(0, 1000, 200)
(Python 3.x)或列表[0, 200, 400, 600, 800]
(Python 2.x)。 Either way, changing step
later isn't going to change anything. 无论哪种方式,稍后更改
step
都不会更改任何内容。
You could build a custom iterator that that lets you reset its step, or one that changes its step and iterates (start, end)
or (start, step)
pairs. 您可以构建一个自定义迭代器,该迭代器可让您重置其步骤,或更改其步骤并迭代
(start, end)
或(start, step)
对的迭代器。 But that may be a little advanced for you.* 但这对您来说可能有点高级。*
In that case, when you can't find or write an iterator to loop over, you have to go to a while
loop, and update your loop variable manually. 在这种情况下,当您找不到或编写迭代器进行循环时,必须进入
while
循环,然后手动更新循环变量。
It's worth noting that you're already trying to update your loop variable manually, with that i = i + step
. 值得注意的是,您已经尝试使用
i = i + step
手动更新循环变量。 That's usually a bad idea inside a for
loop—but inside a while
loop, it's exactly what you want. 在
for
循环内这通常是个坏主意-但在while
循环内,这正是您想要的。 So really, just change this line: 所以真的,只需更改此行:
for i in range(0, len(record),step):
… to these two lines: ……这两行:
i = 0
while i < len(record):
There are other problems in your code that you have to fix (including at least two IndentationError
s), but you can fix them once you get past this. 您的代码中还有其他问题需要修复(至少包括两个
IndentationError
),但是一旦您解决了这些问题,就可以修复它们。
One last thing: Whether you've got a for
loop or a while
loop, if you want to break out early, the way to do it is a break
statement, not by trying to change the loop conditions so the next test will fail. 最后一件事:无论您有
for
循环还是while
循环,如果您想尽早爆发,执行此操作的方式是break
语句,而不是尝试更改循环条件,因此下一个测试将失败。 So, replace that i=len(record)+1
at the end with break
as well. 因此,也请在结尾处将
i=len(record)+1
替换为break
。
* Also, the obvious implementation for such an iterator would be the same kind of while
loop anyway… *而且,这种迭代器的显而易见的实现方式还是是同一种
while
循环……
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.