我必须生成列表中所有两对项目组合。现在,我知道有两种方法可以实现此目的:嵌套 for 循环和 python 的内置itertools:
from itertools import combinations
foo = [1, 2, 3, 4]
for i in xrange(len(foo)):
for j in xrange(i + 1, len(foo)):
print foo[i], foo[j]
for c in combinations(foo, 2):
print c
Run Code Online (Sandbox Code Playgroud)
我的问题是:使用其中一种比另一种有什么显着的优势吗?
我只有两个句子我想产生变化并计算 leveshtein 距离,但是当我尝试使用 itertools 产生这个列表时,即使我的 64GB RAM 机器也会超载。
有没有办法限制这一点,即使我必须将其限制为一定数量的组合。
到目前为止,这是我的代码:
from __future__ import print_function
import itertools
import sys
in_file = sys.argv[1]
X = []
with open(in_file) as f:
lis = list(f)
X.append([' '.join(x) for x in itertools.product(*map(set, zip(*map(str.split, lis))))])
for x in X:
print x
Run Code Online (Sandbox Code Playgroud) 假设我有以下列表:
a = [True, True, True, False, False, False, False, True, True]
Run Code Online (Sandbox Code Playgroud)
怎样才能让他们最好只要么返回分组0,3,7或分组像下面这样?
[True, True, True]
[False, False, False, False]
[True, True]
Run Code Online (Sandbox Code Playgroud)
背景:我试图在我的 NumPy 数组中找到平台,虽然将导数设置为零是一个好的开始,但我仍然需要将数组排序为块。我认为这基本上归结为上述问题。
我查找了 NumPy 和 itertools(试图从问题NumPy grouping using itertools.groupby performance 中获得解决方案)但我没有成功。我想人们可能会使用itertools.takewhile和 filtfalse的组合(请参阅此处的文档),但我对此并不了解。或者也许我只是想得太复杂了。
我在 Windows 8 上安装 itertools 时遇到错误。
C:\>pip install itertools
Collecting itertools
ERROR: Could not find a version that satisfies the requirement itertools (from versions: none)
ERROR: No matching distribution found for itertools
Run Code Online (Sandbox Code Playgroud)
它应该安装 itertools 包。
我的字典中有 6 个布尔变量,我想在所有可能的迭代上运行我的代码。所以我有:
params["is_A"] = True/False
params["is_B"] = True/False
...
Run Code Online (Sandbox Code Playgroud)
然后对于所有可能的组合,我想打电话
my_func(params)
Run Code Online (Sandbox Code Playgroud)
最好的方法是什么?
我有一个列表 = [1, 2, 3, 3, 6, 8, 8, 10, 2, 5, 7, 7] 我正在尝试使用 groupby 将其转换为
1
2
3
3
6
8,8
10
2,
5
7,7
Run Code Online (Sandbox Code Playgroud)
基本上,任何大于 6 的东西,我喜欢将它们分组,否则我想让它们不分组。有关如何使用 itertool groupby 执行此操作的任何提示
我目前的代码:
for key, group in it.groupby(numbers, lambda x: x):
f = list(group)
if len(f) == 1:
split_list.append(group[0])
else:
if (f[0] > 6): #filter condition x>6
for num in f:
split_list.append(num + 100)
else:
for num in f:
split_list.append(num)
Run Code Online (Sandbox Code Playgroud) 我试图在嵌套的列表列表中随机打乱一个元素。
例如我有:
list1=[[['a', 'b', 'c', 1], ['a', 'b', 'c', 2]], [['a', 'b', 'c', 3], ['a', 'b', 'c', 4]], [['a', 'b', 'c', 5], ['a', 'b', 'c', 6]]]
Run Code Online (Sandbox Code Playgroud)
我想随机打乱每个子列表(浮点数)的第三个元素,同时保持此嵌套列表中的其余列表条目及其结构完好无损。例如:
list1=[[['a', 'b', 'c', 1], ['a', 'b', 'c', 6]], [['a', 'b', 'c', 4], ['a', 'b', 'c', 2]], [['a', 'b', 'c', 3], ['a', 'b', 'c', 5]]]
Run Code Online (Sandbox Code Playgroud)
到目前为止,我想出了以下几点:
import random
list1 = [[['a', 'b', 'c', 1], ['a', 'b', 'c', 2]], [['a', 'b', 'c', 3], ['a', 'b', 'c', 4]], [['a', 'b', 'c', 5], ['a', 'b', 'c', 6]]] …Run Code Online (Sandbox Code Playgroud) 我需要创建一个字符串列表,其中包含所有字母大写和小写的所有可能组合,以及长度为 14 的非重复字符,这很庞大,我知道这将花费大量的时间和空间。我现在的代码是这样的:
import itertools
filename = open("strings.txt", "w")
for com in itertools.permutations('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 14):
filename.write("\n"+"1_"+"".join(com)+"\n"+"0_"+"".join(com))
print ("".join(com))
Run Code Online (Sandbox Code Playgroud)
非常基本,它可以完成工作,但我还没有找到更快的方法(尝试了一种我发现似乎更快但 python 更快的 java 算法)因为这将需要很长时间,我不时需要关闭我的电脑,所以我需要能够保存我离开的地方并继续,否则每次它崩溃/关闭我的电脑/发生任何事情时我都会从头开始。有没有办法做到这一点?
我有两个清单:
list1 = ["A", "B", "C", "D", "E"]
list2 = ["AA", "BB", "CC", "DD", "EE"]
Run Code Online (Sandbox Code Playgroud)
我想创建所有可能的组合,如下所示:
A-B-C-D-E
A-BB-C-D-E
A-B-C-DD-E...etc.
Run Code Online (Sandbox Code Playgroud)
规则是两个相似的字母(如A-AA,B-BB)不能同时在组合中,并且顺序是可逆的(ABCDE和BAECD内容相同,所以我不需要它们两个都)
如何使用 itertools 管理它?
我有一个包含整数的列表,这些整数指示列表中一次会出现多少个大写字母。
x = [1, 2]
# when x == 1 then 1 capitalization per time
# when x == 2 then 2 capitalization per time
l = ['a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)
输出会像这样......
Abc
aBc
abC
ABc
AbC
aBC
Run Code Online (Sandbox Code Playgroud)
我可以正常编码,但是可以通过 itertools 完成吗?
python-itertools ×10
python ×9
python-3.x ×3
combinations ×2
performance ×2
group-by ×1
list ×1
loops ×1
numpy ×1
permutation ×1
random ×1