所以我写了一个小脚本来从网站上下载图片.它通过一个7 alpha字符值,其中第一个字符始终是一个数字.问题是如果我想停止脚本并再次启动它我必须从头开始.
我可以用我得到的最后一个值以某种方式播种itertools.product,所以我不必再次通过它们了.
感谢您的任何意见.
这是代码的一部分:
numbers = '0123456789'
alnum = numbers + 'abcdefghijklmnopqrstuvwxyz'
len7 = itertools.product(numbers, alnum, alnum, alnum, alnum, alnum, alnum) # length 7
for p in itertools.chain(len7):
currentid = ''.join(p)
#semi static vars
url = 'http://mysite.com/images/'
url += currentid
#Need to get the real url cause the redirect
print "Trying " + url
req = urllib2.Request(url)
res = openaurl(req)
if res == "continue": continue
finalurl = res.geturl()
#ok we have the full url now time to if it …Run Code Online (Sandbox Code Playgroud) 我需要一个Python函数iterate(f, x),它创建一个迭代器,返回值x,f(x),f(f(x)),f(f(f(x)))等(例如,Clojure的iterate).首先,我想知道:这是否已经存在于标准库的某个地方,我只是错过了它?当然,使用生成器实现起来很容易:
def iterate(f, x):
while True:
yield x
x = f(x)
Run Code Online (Sandbox Code Playgroud)
只是出于好奇:在Python中有没有更多功能性的方法来实现这一点,例如使用一些itertools或functools魔术?
在Python 3.3中,这可行
def iterate(f, x):
return accumulate(repeat(x), lambda acc, _ : f(acc))
Run Code Online (Sandbox Code Playgroud)
但看起来像是对我的虐待.我可以更好地做到这一点吗?
python iterator functional-programming generator python-itertools
我正在尝试在Python 3中创建一个powerset.我找到了对该itertools
模块的引用,并且我使用了该页面上提供的powerset代码.问题是:代码返回itertools.chain对象的引用,而我想访问powerset中的元素.我的问题:如何做到这一点?
非常感谢您的见解.
我正在尝试将一个字符串列表拆分为包含这些字符串的不均匀长度的元组列表,每个元组包含最初用空字符串分隔的字符串.基本上我需要可以应用于列表的参数化拆分.如果我的初始列表如下:
init = ['a', 'b', '', 'c', 'd e', 'fgh', '', 'ij', '', '', 'k', 'l', '']
Run Code Online (Sandbox Code Playgroud)
此列表的最后一个元素始终是结束''.可以有连续的''s被认为是单身.我需要的结果是:
end = [('a', 'b'), ('c', 'd e', 'fgh'), ('ij',), ('k', 'l')]
Run Code Online (Sandbox Code Playgroud)
我已经有了丑陋的代码来完成工作,并且在列表完全弹出后会超出范围:
end = []
while init[-1] == u'':
init.pop()
l = []
while init[-1] != u'':
l.append(init.pop())
end.append(tuple(l))
Run Code Online (Sandbox Code Playgroud)
我想使用理解,但是尝试解压缩参数列表,反转自引用列表,使用deque队列和各种代码气味都没有成功,我现在怀疑是否有必要寻找(嵌套)理解解决方案?
我目前正在使用类似的东西
>> import itertools
>> ABC = [a, b, c]
>> abc = itertools.cycle( ABC )
>> next( abc )
a
>> next( abc )
b
>> next( abc )
c
Run Code Online (Sandbox Code Playgroud)
我希望我的下一个电话是
>> previous( abc )
b
Run Code Online (Sandbox Code Playgroud)
itertools中有一个方法可以做到这一点吗?
Python是否支持异步迭代器上的函数式操作?我知道,我可以使用map,filter并itertools懒洋洋地转换和消耗数据与正常的发电机来:
from itertools import accumulate, takewhile
def generator():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
# create another iterator, no computation is started yet:
another_iterator = takewhile(lambda x: x < 100, accumulate(generator()))
# start consuming data:
print(list(another_iterator))
# [1, 2, 4, 7, 12, 20, 33, 54, 88]
Run Code Online (Sandbox Code Playgroud)
现在,Python 3.6的异步生成器/迭代器不支持同样的事情,因为它们当然没有实现正常的迭代器协议:
async def agenerator():
a, b = 1, 1
while True:
yield a
a, b = b, …Run Code Online (Sandbox Code Playgroud) 我正在使用itertools.product来查找资产的可能权重,因为所有权重之和总计为100。
min_wt = 10
max_wt = 50
step = 10
nb_Assets = 5
weight_mat = []
for i in itertools.product(range(min_wt, (max_wt+1), step), repeat = nb_Assets):
if sum(i) == 100:
weight = [i]
if np.shape(weight_mat)[0] == 0:
weight_mat = weight
else:
weight_mat = np.concatenate((weight_mat, weight), axis = 0)
Run Code Online (Sandbox Code Playgroud)
上面的代码可以工作,但是因为它经历了不可接受的组合,所以它太慢了,示例[50,50,50,50,50]最终测试了3125个组合,而不是121个可能的组合。有什么方法可以在循环中添加“求和”条件以加快处理速度?
Python中有没有计算重复排列数的函数?我知道使用 itertools 我可以生成它们,但我只想要一种更快的方法来进行计算并避免计算阶乘,我对生成所有可能的排列不感兴趣。
例:计算4个A、3个B、5个C可能组成的字符串。
分辨率= 12!/(4!3!5!)
或者在代码中:
from math import factorial
from functools import reduce
rep=[4,3,5]
result= factorial(sum(rep))//reduce(lambda x,y: x*y, map(factorial, rep))
Run Code Online (Sandbox Code Playgroud) 我们可以从Python 3中的itertools.product结果中获取特定索引中的元素吗?像下面这样:
xlist = itertools.product('abc', repeat=3)
element = xlist[10]
Run Code Online (Sandbox Code Playgroud)
我想从一个可迭代对象生成一个其前缀的可迭代对象(包括原始可迭代对象本身)。
for prefix in prefixes(range(5)):
print(tuple(prefix))
Run Code Online (Sandbox Code Playgroud)
应该导致
(0,)
(0, 1)
(0, 1, 2)
(0, 1, 2, 3)
(0, 1, 2, 3, 4)
Run Code Online (Sandbox Code Playgroud)
或在
()
(0,)
(0, 1)
(0, 1, 2)
(0, 1, 2, 3)
(0, 1, 2, 3, 4)
Run Code Online (Sandbox Code Playgroud)
和
for prefix in prefixes('Hello'):
print(''.join(prefix))
Run Code Online (Sandbox Code Playgroud)
应该导致
H
He
Hel
Hell
Hello
Run Code Online (Sandbox Code Playgroud)
或在
H
He
Hel
Hell
Hello
Run Code Online (Sandbox Code Playgroud)
(空前缀是否是结果的一部分对我来说并不重要,内部或外部结果迭代的确切类型也不重要。)
我能够设计出几种方法来实现这一点,但都感觉至少有点笨拙:
(如果可迭代是一个序列,则有效)
def prefixes(seq):
for i in range(len(seq)):
yield seq[:i + 1]
Run Code Online (Sandbox Code Playgroud)
或使用列表理解:
def prefixes(seq):
return [seq[:i + 1] for i …Run Code Online (Sandbox Code Playgroud) python ×10
python-itertools ×10
python-3.x ×2
download ×1
generator ×1
image ×1
iterator ×1
list ×1
nested-loops ×1
numpy ×1
python-2.7 ×1
python-3.6 ×1
seed ×1