Python的itertools模块提供了许多关于使用生成器处理可迭代/迭代器的好东西.例如,
permutations(range(3)) --> 012 021 102 120 201 210
combinations('ABCD', 2) --> AB AC AD BC BD CD
[list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
Run Code Online (Sandbox Code Playgroud)
Ruby中的等价物是什么?
相当于,我的意思是快速和内存效率(Python的itertools模块是用C编写的).
我有两台发电机g1和g2
for line in g1:
print line[0]
Run Code Online (Sandbox Code Playgroud)
[a,a,a]
[b,b,b]
[c,c,c]
for line1 in g2:
print line1[0]
Run Code Online (Sandbox Code Playgroud)
[1,1,1]
[2,2,2]
[3,3,3]
for line in itertools.chain(g1, g2):
print line[0]
Run Code Online (Sandbox Code Playgroud)
并[a,A,A]
[B,B,B]
[C,C,C]
[1,1,1]
[2,2,2]
[3,3,3]
怎么样
得到如下输出:
[a,a,a],[1,1,1]
[b,b,b],[2,2,2]
[c,c,c],[3,3, 3]
或
并[a,A,A,1,1,1]
[B,B,B,2,2,2]
[C,C,C,3,3,3]
谢谢您的帮助.
我希望以下片段给我一个迭代器,从两个输入迭代的笛卡尔乘积产生对:
$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import itertools
>>> one = xrange(0, 10**9)
>>> two = (1,)
>>> prods = itertools.product(one, two)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MemoryError
Run Code Online (Sandbox Code Playgroud)
相反,我得到了一个MemoryError.但我认为itertools.product没有将中间结果存储在内存中,那么是什么导致了MemoryError?
我注意到itertools(在我看来)没有一个函数能够交错来自其他几个可迭代对象的元素(而不是压缩它们):
def leaf(*args): return (it.next() for it in cycle(imap(chain,args)))
tuple(leaf(['Johann', 'Sebastian', 'Bach'], repeat(' '))) => ('Johann', ' ', 'Sebastian', ' ', 'Bach', ' ')
Run Code Online (Sandbox Code Playgroud)
(编辑)我问的原因是因为我想避免不必要的拉链/展平事件.
显然,定义leaf很简单,但如果有一个预定义的函数做同样的事情,我宁愿使用它,或者一个非常清晰的生成器表达式.是否有内置的函数,在itertools中,或在其他一些着名的库中,或者是一个合适的惯用语表达式?
编辑2:可以使用更简洁的定义(使用functional包):
from itertools import *
from functional import *
compose_mult = partial(reduce, compose)
leaf = compose_mult((partial(imap, next), cycle, partial(imap, chain), lambda *args: args))
Run Code Online (Sandbox Code Playgroud) 该文件说,笛卡尔积函数
the actual implementation does not build up intermediate results in memory.
Run Code Online (Sandbox Code Playgroud)
如何用发电机做到这一点?有人能给我看一个2个发生器的有限内存消耗的例子吗?
我可以得到这样的整数排列:
myInt = 123456789
l = itertools.permutations(str(myInt))
[int(''.join(x)) for x in l]
Run Code Online (Sandbox Code Playgroud)
有没有更有效的方法在Python中获取整数排列,省去了创建字符串的开销,然后加入生成的元组?对它进行定时,元组连接过程使这个时间长3倍 list(l).
增加了支持信息
myInt =123456789
def v1(i): #timeit gives 258ms
l = itertools.permutations(str(i))
return [int(''.join(x)) for x in l]
def v2(i): #timeit gives 48ms
l = itertools.permutations(str(i))
return list(l)
def v3(i): #timeit gives 106 ms
l = itertools.permutations(str(i))
return [''.join(x) for x in l]
Run Code Online (Sandbox Code Playgroud) 我有一个服务器列表.每个服务器上都有一个名称列表.例:
server1 = ['a','b','c']
server2 = ['d','e','f']
server3 = ['g','h','i']
Run Code Online (Sandbox Code Playgroud)
我想迭代每个服务器名称而不是每个服务器.例如,在选择'a'之后server1,移动到'd'(不'b')等等.如果我要使用itertools.cycle(),我是否必须创建一个服务器列表来循环?我的预期结果是['a','d','g','b','e','h','c','f','i'].你能给我一个关于如何在多个列表中循环的简单示例.
我正在使用itertools迭代我输入参数的所有可能组合的数值模拟.在下面的示例中,我有两个参数和六种可能的组合:
import itertools
x = [0, 1]
y = [100, 200, 300]
myprod = itertools.product(x, y)
for p in myprod:
print p[0], p[1]
# run myfunction using p[0] as the value of x and p[1] as the value of y
Run Code Online (Sandbox Code Playgroud)
如何获得myprod(示例中的六个)的大小?我需要在for循环开始之前打印它.
我明白myprod不是一个清单.我可以计算len(list(myprod)),但这会消耗迭代器,因此for循环不再有效.
我试过了:
myprod2=copy.deepcopy(myprod)
mylength = len(list(myprod2))
Run Code Online (Sandbox Code Playgroud)
但这也行不通.我可以:
myprod2=itertools.product(x,y)
mylength = len(list(myprod2))
Run Code Online (Sandbox Code Playgroud)
但它不是优雅和pythonic!
我有一个256x256x256Numpy数组,其中每个元素都是一个矩阵.我需要对这些矩阵中的每一个进行一些计算,并且我想使用该multiprocessing模块来加快速度.
这些计算的结果必须存储在256x256x256与原始数组相同的数组中,以便[i,j,k]原始数组中元素的矩阵结果必须放在[i,j,k]新数组的元素中.
为此,我想制作一个可以伪方式写入的列表,[array[i,j,k], (i, j, k)]并将其传递给一个"多处理"的函数.假设这matrices是从原始数组中提取的所有矩阵的列表,并且myfunc是执行计算的函数,代码看起来有点像这样:
import multiprocessing
import numpy as np
from itertools import izip
def myfunc(finput):
# Do some calculations...
...
# ... and return the result and the index:
return (result, finput[1])
# Make indices:
inds = np.rollaxis(np.indices((256, 256, 256)), 0, 4).reshape(-1, 3)
# Make function input from the matrices and the indices:
finput = izip(matrices, inds)
pool = multiprocessing.Pool()
async_results …Run Code Online (Sandbox Code Playgroud) 可能重复:
集合python的幂集和笛卡尔积
使用Python Itertools.permutations()我希望接收和输出具有重复字符的排列.举个例子,我的下面的函数和它的当前输出.
def perm(n,i):
b = 0
while b < n:
n= n -1
from itertools import permutations as p
file.write('\n'.join([''.join(item) for item in p(i,n)]))
perm(4,'0123')
Run Code Online (Sandbox Code Playgroud)
输出是:
012
013
021
023
031
032
102
103
120
123
130
132
201
203
210
213
230
231
301
302
310
312
320
321.....
Run Code Online (Sandbox Code Playgroud)
我如何获得112或222的输出?
根据我的理解,组合不是特定于排列的特定顺序.我正在寻找的是找到所有组合然后每个组合的每个排列.这可能吗?
python-itertools ×10
python ×9
permutation ×3
generator ×2
combinations ×1
group-by ×1
integer ×1
ruby ×1