简体   繁体   English

Python在for循环的每次迭代中为范围函数生成随机步骤

[英]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.

相关问题 每次迭代的更改范围均始于Python“ for循环” - Change range start in Python 'for loop' for each iteration Python random.choice()与if函数一起循环-如何为每次迭代分配值 - Python random.choice() in loop with if-function - how to assign value to each iteration Python for 循环迭代未完成,跳过步骤 - Python for loop iteration not completing, skipping steps Python中如何在循环中每次生成唯一的单个随机数? - How to generate a unique single random number each time in a loop in Python? 调用函数为Python中循环的每次迭代获取新值 - Calling a function to obtain a new value for each iteration of a loop in Python 在循环的每次迭代中运行一个函数作为 python 中的一个新进程 - Running a function in each iteration of a loop as a new process in python 您如何使用范围 function 以在 for 循环中的每次迭代以 1 为增量从 0 计数到 8 - How do you use the range function to count from 0 to 8 by increments of 1 for each iteration in a for loop Python3在for循环中生成字符串,不为每次迭代重新定义类变量 - Python3 generate strings in for-loop, class variables not being redefined for each iteration 如何有条件地跳过python for循环中的迭代步骤数? - How to conditionally skip number of iteration steps in a for loop in python? 使用Python如何在Pandas数据帧中的每一行的范围内生成一个随机数? - Using Python how do I generate a random number within a range for each row in Pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM