相关疑难解决方法(0)

在python中重复生成排列

我知道itertools,但它似乎只能生成排列而不重复.

例如,我想为2个骰子生成所有可能的骰子.所以我需要[1,2,3,4,5,6]的大小为2的所有排列,包括重复:(1,1),(1,2),(2,1)......等等

如果可能的话,我不想从头开始实现这一点

python permutation combinatorics

69
推荐指数
3
解决办法
4万
查看次数

Python结合了两个for循环

目前我会这样做:

for x in [1,2,3]:
    for y in [1,2,3]:
        print x,y
Run Code Online (Sandbox Code Playgroud)

有办法做下面这样的事情,

for x,y in ([1,2,3],[1,2,3]):
    print x,y
Run Code Online (Sandbox Code Playgroud)

想要缩短这种循环,这会引发"太多解压缩"异常.

python

11
推荐指数
2
解决办法
2万
查看次数

Python生成所有可能的长度为n的字符串

我正在学习道德黑客攻击.我有密码的哈希密钥和密码算法(sha256/sha512).我想解析文件中的所有字符串,并检查密码的哈希值是否与文件中每个字符串的哈希值相匹配.

生成的String可能包含小写字母,大字母和数字.

任何想法如何生成所有可能包含字母和数字的长度为n的字符串?

python

5
推荐指数
2
解决办法
8987
查看次数

长度为4的字符串中所有DNA字符组合

我试图生成长度为4与四个字符的所有可能的DNA序列的列表A,T,C,G.总共有4 ^ 4(256)种不同的组合.我包括重复,这AAAA是允许的.itertools.combinations_with_replacement(iterable, r) 不过我看过,列表输出会根据输入字符串的顺序而改变

itertools.combinations_with_replacement('ATCG', 4) #diff results to...
itertools.combinations_with_replacement('ATGC', 4)
Run Code Online (Sandbox Code Playgroud)

因此,我试图itertools.combinations_with_replacement(iterable, r)与之合并itertools.permutations()

使得传递的输出itertools.permutations()来itertools.combinations_with_replacement().定义如下:

def allCombinations(s, strings):
perms = list(itertools.permutations(s, 4))
allCombos = []
for perm in perms:
    combo = list(itertools.combinations_with_replacement(perm, 4))
    allCombos.append(combo)
for combos in allCombos:
    for tup in combos:
        strings.append("".join(str(x) for x in tup))
Run Code Online (Sandbox Code Playgroud)

然而,运行allCombinations('ATCG', li)在哪里li = [],然后采取 list(set(li))仍然只进行136个独特的序列,而不是256.

必须有一个简单的方法来做到这一点,可能会产生一个电源设置,然后过滤4长度?

python combinations python-itertools

2
推荐指数
1
解决办法
1004
查看次数