我正在尝试将一些使用 Python迭代器工具的代码移植到 Node,但是我没有看到任何类似的东西。
我正在专门寻找 itertools.combination() 的替代品
我看过这个,但它不完整且过时:node-intertools
我想使用 itertools 编写一个无限生成器,它结合了 thecount和repeat迭代器,在递增之前重复每个数字n多次。有没有比我想出的更简洁的方法?
from itertools import *
def repeating_counter(times):
c = count()
while True:
for x in repeat(next(c), times):
yield x
>>> rc = repeating_counter(2)
>>> next(rc)
0
>>> next(rc)
0
>>> next(rc)
1
>>> next(rc)
1
Run Code Online (Sandbox Code Playgroud) 我正在创建大量带有替换(产品)的排列,它需要大量的计算时间。让我们使用像这样的简单函数:
def permutations(li):
return [p for p in itertools.product(li, repeat=20)]
permutation(range(11))
Run Code Online (Sandbox Code Playgroud)
我只是不确定如何将此函数拆分为多个进程(使用“Pool”类或类似类)。我从未在 Python 中使用过多重处理,我想寻求帮助。
有什么提示吗?
谢谢!
我有一些列表中的所有对组合:
a = [1,2,3,4,5,6]
pairs = [pair for pair in itertools.combinations(a,2)]
print pairs
>>>[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]
Run Code Online (Sandbox Code Playgroud)
如果我只知道它的索引,我如何知道我到底在使用哪个对?也许有一些公式?问题是我的列表太大了,我无法记住所有对。我只需要一些可以通过索引识别的特定对。
有什么建议么?
谢谢!
我知道很多帖子都有类似的问题,并且已经浏览过其中很多。但是,我无法做我需要做的事情。
我有列表 L=[0,1,2,3,4,5] 我想将其分成一对元组。例如:
[(0,1,2),(3,4,5)]
[(0,1,3),(2,4,5)]
[(0,1,4),(2,4,5)]
...
Run Code Online (Sandbox Code Playgroud)
每个元组需要包含原始列表中一半的元素(在本例中为 3 个,共 6 个)。解决方案需要使用 3 个元素生成元组的每种组合。
我可以使用以下命令轻松找到列表中所有可能的元组
list(itertools.combinations(L, 3))
Run Code Online (Sandbox Code Playgroud)
[(0, 1, 2),
(0, 1, 3),
(0, 1, 4),
(0, 1, 5),
(0, 2, 3),
(0, 2, 4),
...]
是否itertools也为此提供任何解决方法?
有时你应该嵌套合并到合并列表(它类似于np.flatten())。当列表的列表如下所示时,您应该将其展平
a = [[j for j in range(0, 10)] for i in range(0, 10000)]
Run Code Online (Sandbox Code Playgroud)
你有两种解决方案来解决它。itertools.chain.from_iterable和functools.reduce。
%timeit list(itertools.chain.from_iterable(a))
%timeit reduce(lambda x, y: x+y, a)
Run Code Online (Sandbox Code Playgroud)
你认为哪一个更快,比其他东西快多少?
itertools.chain.from_iterable快 1000 倍或更多(当列表的长度较大时)。
如果有人知道为什么会发生这种情况,请告诉我。
始终感谢您的支持和帮助。
我正在尝试生成列表的第一个元素与所有其他元素的组合。通过首先获取所有可能的组合,然后丢弃我不需要的组合,用一个小列表执行此操作并不是问题。例子:
import numpy as np
import itertools
lst = ['a','b','c','d','e','f']
element_of_interest = 'a'
all_combinations = np.array(list(itertools.combinations(lst, 3)))
idx = np.where(all_combinations == element_of_interest)
combinations_of_interest = all_combinations[idx[0]]
print(combinations_of_interest)
>>> [['a' 'b' 'c']
['a' 'b' 'd']
['a' 'b' 'e']
['a' 'b' 'f']
['a' 'c' 'd']
['a' 'c' 'e']
['a' 'c' 'f']
['a' 'd' 'e']
['a' 'd' 'f']
['a' 'e' 'f']]
Run Code Online (Sandbox Code Playgroud)
但在我的应用程序(290 个元素的列表,r = 4)中,这种方法还不够好,因为效率非常低。
有人能想出更好的方法吗?
我看过在线课程,他们有如下示例:
from itertools import count
# creates a count iterator object
iterator =(count(start = 0, step = 2))
# prints an even list of integers
print("Even list:",
list(next(iterator) for _ in range(5)))
Run Code Online (Sandbox Code Playgroud)
...您可以使用range或 来编写np.arange。这是另一个例子:
# list containing some strings
my_list =["x", "y", "z"]
# count spits out integers for
# each value in my list
for i in zip(count(start = 1, step = 1), my_list):
print(i)
Run Code Online (Sandbox Code Playgroud)
...这基本上只是enumerate。所以我的问题是:您能否举一个使用 无法完成(或必须更加笨拙地完成)的itertools.count示例?itertools.islicerange
当我尝试在 Conda 版本 4.9.2 中的 python 3 中运行我的程序时,出现该错误
Traceback (most recent call last): File "mosaic.py", line 1, in <module> from itertools import pairwise ImportError: cannot import name 'pairwise' from 'itertools' (unknown location)
我有下面的示例词典,
errors = [{'PartitionKey': '34', 'RowKey': '14', 'Component': 'mamba', 'Environment': 'QA', 'Error': '404 not found', 'Group': 'Test', 'Job': 'cutting', 'JobType': 'automated'}, {'PartitionKey': '35', 'RowKey': '15', 'Component': 'mamba', 'Environment': 'QA', 'Error': '404 not found', 'Group': 'Test', 'Job': 'cutting', 'JobType': 'automated'}, {'PartitionKey': '36', 'RowKey': '16', 'Component': 'mamba', 'Environment': 'Dev', 'Error': '404 not found', 'Group': 'random', 'Job': 'moping', 'JobType': 'manual'}, {'PartitionKey': '37', 'RowKey': '17', 'Component': 'mamba', 'Environment': 'QA', 'Error': '404 not found', 'Group': 'Test', 'Job': 'cutting', 'JobType': 'automated'}, {'PartitionKey': '38', 'RowKey': '18', 'Component': …Run Code Online (Sandbox Code Playgroud) python-itertools ×10
python ×9
python-3.x ×3
combinations ×1
functools ×1
node.js ×1
permutation ×1
product ×1
python-3.8 ×1
reduce ×1