Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python for循环中的扩展条件_Python_Python 2.7_Range - Fatal编程技术网

Python for循环中的扩展条件

Python for循环中的扩展条件,python,python-2.7,range,Python,Python 2.7,Range,我想在forloop中包含类似双条件的东西,为了减少计算时间,我想做如下事情: for i WHERE i % 2 == 0 in range(0,(realbignumber/2)): if i % realbignum == 0: do some stuff 我基本上不确定如何在forloop中正确执行where类型语句?我相信可能有更好的方法来减少计算时间(我正在尝试对realbignum进行素数分解),首先要得到一个除数列表,然后检查素数。一旦我得到I%2==0'where'子

我想在forloop中包含类似双条件的东西,为了减少计算时间,我想做如下事情:

for i WHERE i % 2 == 0 in range(0,(realbignumber/2)):
    if i % realbignum == 0: do some stuff

我基本上不确定如何在forloop中正确执行where类型语句?我相信可能有更好的方法来减少计算时间(我正在尝试对realbignum进行素数分解),首先要得到一个除数列表,然后检查素数。一旦我得到I%2==0'where'子句,我计划实现类似于'fori,其中I在范围内为素数….的东西。我使用的是python 2,但也可以使用python 3方法。

使用生成器表达式,如下所示

for i in (num for num in xrange(realbignumber / 2) if num % 2 == 0):
for i in xrange(0, realbignumber / 2, 2):
>>> def my_range(start, stop, step=1):
...     current = start
...     while current < stop:
...         yield current
...         current += step
... 
>>> [num for num in my_range(0, 10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from itertools import islice, count
>>> def bigxrange(start, stop, step=1):
...     return islice(count(start, step), (stop - start + step - 1))
... 
>>> list(bigxrange(0, 10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
对于这种特殊情况,实际上可以指定
step
参数本身,如下所示

for i in (num for num in xrange(realbignumber / 2) if num % 2 == 0):
for i in xrange(0, realbignumber / 2, 2):
>>> def my_range(start, stop, step=1):
...     current = start
...     while current < stop:
...         yield current
...         current += step
... 
>>> [num for num in my_range(0, 10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from itertools import islice, count
>>> def bigxrange(start, stop, step=1):
...     return islice(count(start, step), (stop - start + step - 1))
... 
>>> list(bigxrange(0, 10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
请注意,我使用了
xrange
而不是
range
函数。因为Python2.x中的
range
函数创建一个数字列表,其中as
xrange
只创建一个数字列表。因此
xrange
适用于非常长的范围,因为它具有很高的内存效率


如果输入的数字太大,无法放入Python的int中,那么您可以在生成器的帮助下滚动自己的简化范围函数,如下所示

for i in (num for num in xrange(realbignumber / 2) if num % 2 == 0):
for i in xrange(0, realbignumber / 2, 2):
>>> def my_range(start, stop, step=1):
...     current = start
...     while current < stop:
...         yield current
...         current += step
... 
>>> [num for num in my_range(0, 10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from itertools import islice, count
>>> def bigxrange(start, stop, step=1):
...     return islice(count(start, step), (stop - start + step - 1))
... 
>>> list(bigxrange(0, 10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
也许我能帮你


对于python3,根据和这个,代码应该可以工作。

Hmm,不幸的是,我的数字对于xrange来说太大了,我得到了“OverflowerError:Pythonint太大,无法转换为C long”有什么想法吗?--我知道这可能不会在合适的时间内计算,我目前正在尝试观察输出,以确保事情按预期进行,然后进行优化,这段代码仍然是新的东西=)@Abraxas您可以使用生成器函数,就像我在更新的答案中所显示的那样。@Abraxas:没有好的解决方法。您可以做的最好的事情是从
itertools
部分构建
bigxrange
:从itertools导入islice,count,
def bigxrange(start,stop,step=1):返回islice(count(start,step),(stop-start+step-1)//step)
@thefourtheye:itertools方法可能更接近
xrange
的速度,将所有工作推到C.@Abraxas Hmmm,然后您可以使用
my_range
实现或切换到Python3,其中不存在此int限制:因为您正在尝试进行素因子分解,看看这个:。