import itertools
def _yield_sample():
it = iter(itertools.combinations('ABCD', 2))
it2 = iter(itertools.combinations('EFGH', 3))
itc = itertools.chain(it,it2)
for x in itc:
yield x
def main():
for x in _yield_sample():
print x
Run Code Online (Sandbox Code Playgroud)
这适用于打印组合.
>>>
('A', 'B')
('A', 'C')
('A', 'D')
...
Run Code Online (Sandbox Code Playgroud)
但是这个:
def __position_combination(_count = [2,3,4,5]):
its = []
for ct in _count:
it = iter(itertools.combinations('ABCDEFG', ct))
its.append(it)
itc = itertools.chain(its)
for x in itc:
yield x
def main():
for x in __position_combination():
print x
Run Code Online (Sandbox Code Playgroud)
不会,它会打印出来
>>>
<itertools.combinations object at 0x02179210>
<itertools.combinations object …Run Code Online (Sandbox Code Playgroud) 假设我有两个列表(或numpy.arrays):
a = [1,2,3]
b = [4,5,6]
Run Code Online (Sandbox Code Playgroud)
如何检查每个元素a是否小于b同一索引的相应元素?(我假设指数从0开始)即
at index 0 value of a = 1 < value of b = 4
at index 1 value of a = 2 < value of b = 5
at index 2 value of a = 3 < value of b = 6
Run Code Online (Sandbox Code Playgroud)
如果a等于[1,2,7],则那将是不正确的,因为索引2的值a大于b.如果a长度比任何长度小b,它应该只比较指数a和b.
例如这对a,b
a = [1,2] …Run Code Online (Sandbox Code Playgroud) 我正在使用Python 2.7.
我有一个列表,我想要所有可能的有序组合.
import itertools
stuff = ["a","b","c", "d"]
for L in range(1, len(stuff)+1):
for subset in itertools.combinations(stuff, L):
print( ' '.join(subset))
Run Code Online (Sandbox Code Playgroud)
这将给出以下输出:
a
b
c
d
a b
a c <-- not in correct order
a d <-- not in correct order
b c
b d <-- not in correct order
c d
a b c
a b d <-- not in correct order
a c d <-- not in correct order
b c d
a b c d …Run Code Online (Sandbox Code Playgroud) 假设我有以下代码:
a = [1,2,3]
b = [2,4,6]
c = [3,5,7]
for i in a:
for j in b:
for k in c:
print i * j * k
Run Code Online (Sandbox Code Playgroud)
有没有办法可以在一行中合并迭代器而不是嵌套?
在阅读python文档时,我遇到了这个itertools.groupby()
函数.这不是很简单所以我决定在stackoverflow上查找一些信息.我从如何使用Python的itertools.groupby()中找到了一些东西?.
在这里和文档中似乎没有关于它的信息,所以我决定发表我的意见以征求意见.
谢谢
我想在0-9之间生成4个随机整数.我可以使用 for循环轻松实现这一点:
digits = [random.randrange(10) for i in range(4)]
Run Code Online (Sandbox Code Playgroud)
假设有可能,我想知道其他方法产生相同的结果,也许没有直接使用for循环.我正在尝试使用itertools,这不起作用:
digits = itertools.repeat(random.randrange(10), 4)
Run Code Online (Sandbox Code Playgroud)
我的问题的主要目的是让自己暴露于其他方法.谢谢.
我有3个numpy阵列,需要在它们之间形成笛卡尔积.阵列的尺寸不固定,因此它们可以采用不同的值,一个例子可以是A =(10000,50),B =(40,50),C =(10000,50).
然后,我执行一些处理(如a + bc)以下是我用于产品的功能.
def cartesian_2d(arrays, out=None):
arrays = [np.asarray(x) for x in arrays]
dtype = arrays[0].dtype
n = np.prod([x.shape[0] for x in arrays])
if out is None:
out = np.empty([n, len(arrays), arrays[0].shape[1]], dtype=dtype)
m = n // arrays[0].shape[0]
out[:, 0] = np.repeat(arrays[0], m, axis=0)
if arrays[1:]:
cartesian_2d(arrays[1:], out=out[0:m, 1:, :])
for j in range(1, arrays[0].shape[0]):
out[j * m:(j + 1) * m, 1:] = out[0:m, 1:]
return out
a = [[ 0, -0.02], [1, -0.15]]
b …Run Code Online (Sandbox Code Playgroud) python numpy out-of-memory cartesian-product python-itertools
我有一个.csv包含3列.PersonX,PersonY和PersonZ.有7000行名称和不同的组合.我的目标是看看3对和3对是最高匹配.我无法在excel中找到能够实现这一目标的公式.我确定python能够与itertools组合,但我还没那么高级.名称可以是任何顺序,只是想看看那些2或3个人在同一行中的次数.任何建议都会有很大的帮助,谢谢!
数据的小例子.
PersonX PersonY PersonZ
Aaron Ekblad Keith Yandle Vincent Trocheck
Aaron Ekblad Denis Malgin Mike Matheson
Aaron Ekblad Denis Malgin Mike Matheson
Aaron Ekblad Jonathan Huberdeau Keith Yandle
Aaron Ekblad Jonathan Huberdeau Keith Yandle
Aaron Ekblad Jamie McGinn Keith Yandle
Aaron Ekblad Aleksander Barkov Jonathan Huberdeau
Aaron Ekblad
Adam Erne Andrej Sustr Vladislav Namestnikov
Adam Erne Anthony Cirelli
Adam Erne
Adam Henrique Rickard Rakell Ryan Getzlaf
Adam Henrique Brandon Montour Ryan Getzlaf
Adam Henrique Corey Perry …Run Code Online (Sandbox Code Playgroud) 我试图找出一个整数列表是连贯的还是"在一个范围内",这意味着两个相邻元素之间的差异必须是一个,并且数字必须单调递增.我找到了一个简洁的方法,我们可以按列表中的数字减去列表中元素的位置进行分组 - 当数字不连贯时,这种差异会发生变化.显然,当序列不包含间隙或重复时,应该只有一个组.
测试:
>>> l1 = [1, 2, 3, 4, 5, 6]
>>> l2 = [1, 2, 3, 4, 5, 7]
>>> l3 = [1, 2, 3, 4, 5, 5]
>>> l4 = [1, 2, 3, 4, 5, 4]
>>> l5 = [6, 5, 4, 3, 2, 1]
>>> def is_coherent(seq):
... return len(list(g for _, g in itertools.groupby(enumerate(seq), lambda (i,e): i-e))) == 1
...
>>> is_coherent(l1)
True
>>> is_coherent(l2)
False
>>> is_coherent(l3)
False
>>> is_coherent(l4)
False …Run Code Online (Sandbox Code Playgroud) 我正在创建这个数组:
A=itertools.combinations(range(6),2)
Run Code Online (Sandbox Code Playgroud)
我必须用 numpy 操作这个数组,比如:
A.reshape(..
Run Code Online (Sandbox Code Playgroud)
如果维度是 A 高,则命令list(A)太慢。
更新1:我已经尝试过hpaulj的解决方案,在这种特定情况下会慢一点,知道吗?
start=time.clock()
A=it.combinations(range(495),3)
A=np.array(list(A))
print A
stop=time.clock()
print stop-start
start=time.clock()
A=np.fromiter(it.chain(*it.combinations(range(495),3)),dtype=int).reshape (-1,3)
print A
stop=time.clock()
print stop-start
Run Code Online (Sandbox Code Playgroud)
结果:
[[ 0 1 2]
[ 0 1 3]
[ 0 1 4]
...,
[491 492 494]
[491 493 494]
[492 493 494]]
10.323822
[[ 0 1 2]
[ 0 1 3]
[ 0 1 4]
...,
[491 492 494]
[491 493 494]
[492 493 494]]
12.289898
Run Code Online (Sandbox Code Playgroud) python-itertools ×10
python ×9
numpy ×3
list ×2
python-3.x ×2
chain ×1
combinations ×1
excel ×1
iterator ×1
nested-loops ×1
random ×1
sequence ×1