我正在做一个蛮力的方法试图找到一个拼图的扩展组合.
我试图获得大量的组合,然后测试每个组合,看看它们是否符合某些标准.我使用Python优秀的itertools生成组合,基本上这给了我一个迭代器,我可以过去测试每一个.
这很快就会返回并给我91390种组合来检查:
itertools.combinations(range(1, 40), 4)
Run Code Online (Sandbox Code Playgroud)
这需要几分钟时间,并给我198792594组合测试:
itertools.combinations(range(1, 122), 5)
Run Code Online (Sandbox Code Playgroud)
当我进入下一个级别时,我需要答案:
itertools.combinations(range(1, 365), 6)
Run Code Online (Sandbox Code Playgroud)
当我进入一组364的6路组合时......需要很长时间.年龄.我天生就要求大量的组合吗?它如何扩展?
我有一个清单:
nums = [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
我想获得分割列表的所有可能性1 - 3:
[
( 1, (2, 3, 4) ),
( 2, (1, 3, 4) ),
( 3, (1, 2, 4) ),
( 4, (1, 2 ,3) )
]
Run Code Online (Sandbox Code Playgroud)
现在我能找到的最好的就是使用itertools.combinations(num, 3),但它只会给出每个项目的第二部分,这意味着[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)].我可以使用其他方法吗?
我希望使用itertools从字母表中的每个字母获得所有3个字母的排列.这回来是空白的:
import itertools
def permutations(ABCDEFGHIJKLMNOPQRSTUVWXYZ, r=3):
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
for indices in product(range(n), repeat=r):
if len(set(indices)) == r:
yield tuple(pool[i] for i in indices)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我有一个小脚本,从python中的给定字符生成一个单词列表.但是在执行后总会得到一个MemoryError.为什么它存储在ram中?是否有更好的代码方法不使用ram但提供工作输出?
from itertools import product
chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k',
'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3',
'4', '5', '6', '7', '8', '9']
length = 8
result = ["".join(item) for item in product(*[chars]*length)]
for item in result:
print(item)
Run Code Online (Sandbox Code Playgroud) 我有一个元素列表:[ 3, 3, 6, 6, 6, 5, 5, 8 ]需要按元素的频率对其进行排序才能得到这个:[ 6, 6, 6, 3, 3, 5, 5, 8 ]几个元素具有相同的频率按值排序.你能找到比这更短的路吗?
import collections
from operator import itemgetter, attrgetter
def freq_sort(arr):
counter=collections.Counter(arr)
com = sorted(counter.most_common(), key=itemgetter(1,0), reverse=True)
com = map(lambda x: [x[0]] * x[1], com)
return [item for sublist in com for item in sublist]
Run Code Online (Sandbox Code Playgroud) 除此之外:
from itertools import combinations
def brute_force(x):
for l in range (1,len(x)+1):
for f in list(combinations(range(0,len(x)),l)):
yield f
x = range(1,18)
len(list(brute_force(x)))
Run Code Online (Sandbox Code Playgroud)
[OUT]:
131071
Run Code Online (Sandbox Code Playgroud)
我怎样才能在数学上计算出所有可能组合的数量?
有没有办法在不计算可能的组合的情况下以计算方式进行计算?
我有N个位置,每个位置可以是0或1.我有1个固定数,我想在这N个位置置换这些固定数量的1.
from itertools import permutations
p = [0 for k in xrange(6)]
for k in xrange(0,3):
p[k] = 1
print(list(permutations(p)))
Run Code Online (Sandbox Code Playgroud)
但是上面的结果在列表中包含四个[0,0,0,1,1,1].我只想要其中一个.我怎样才能摆脱这些重复?
我有这样的数组,
a = [3,2,5,7,4,5,6,3,8,4,5,7,8,9,5,7,8,4,9,7,6]
Run Code Online (Sandbox Code Playgroud)
我想制作小于7的值列表(如下所示)
b = [[3,2,5],[4,5,6,3],[4,5],[5],[4],[6]]
Run Code Online (Sandbox Code Playgroud)
所以我使用了以下方法,
>>> from itertools import takewhile
>>> a = [3,2,5,7,4,5,6,3,8,4,5,7,8,9,5,7,8,4,9,7,6]
>>>list(takewhile(lambda x: x < 7 , a))
[3, 2, 5]
Run Code Online (Sandbox Code Playgroud)
但我只得到第一个序列.任何人都可以帮我解决这个问题吗?谢谢.
我试图找到所有可能的组合,其中3个变量可以=某个数字.
x = (10,11,12,13,14,15)
y = (10,11,12,13,14,15)
z = (10,11,12,13,14,15)
answer = 45
Run Code Online (Sandbox Code Playgroud)
我将如何使用itertools找到每个组合
x + y + z = 45
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我希望python打印(15,15,15)但是,如果答案是=不同的数字,如30,它将打印x,y,z的每个组合,当添加时将= 45.
我有一个列表[1,2,3],我希望得到以下结果:
1
1,1
1,1,1
1,2
1,2,1
1,2,2
1,2,3
1,2
1,3
1,3,3
2,1,2
2,2,1
3,1,1
etc
Run Code Online (Sandbox Code Playgroud)
我尝试过使用itertools,但我只是在不重复的情况下获得组合.
有谁知道如何获得具有所需结果的列表?
python ×10
python-itertools ×10
combinations ×3
python-3.x ×3
list ×2
algorithm ×1
math ×1
permutation ×1
sorting ×1