简体   繁体   English

在Python中将2个字符置换成固定长度的字符串,每个字符的数目相等

[英]Permutations of 2 characters in Python into fixed length string with equal numbers of each character

I've looked through the 2 questions below, which seem closest to what I am asking, but don't get me to the answer to my question. 我已经仔细阅读了以下两个问题,这些问题似乎与我要问的问题最接近,但是却无法帮助我找到问题的答案。

Permutation of x length of 2 characters x长度为2个字符的排列

How to generate all permutations of a list in Python 如何在Python中生成列表的所有排列

I am trying to find a way to take 2 characters, say 'A' and 'B', and find all unique permutations of those characters into a 40 character string. 我试图找到一种方法来接受两个字符,例如“ A”和“ B”,并找到这些字符的所有唯一排列成40个字符串。 Additionally - I need each character to be represented 20 times in the string. 另外-我需要每个字符在字符串中表示20次。 So all resulting strings each have 20 'A's and 20 'B's. 因此,所有生成的字符串每个都有20个“ A”和20个“ B”。

Like this: 像这样:

'AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBB'
'AAAAAAAAAAAAAAAAAAABABBBBBBBBBBBBBBBBBBB'
'AAAAAAAAAAAAAAAAAABAABBBBBBBBBBBBBBBBBBB'

etc... 等等...

All I really need is the count of unique combinations that follow these rules. 我真正需要的是遵循这些规则的独特组合的数量。

y=['A','A','A','A','B','B','B','B']
comb = set(itertools.permutations(y))
print("Combinations Found: {:,}".format(len(comb)))

This works, but it doesn't scale well to an input string of 20 'A's and 20 'B's. 这可以工作,但不能很好地缩放到20'A和20'B的输入字符串。 The above code takes 90 seconds to execute. 上面的代码执行需要90秒。 Even just scaling up to 10 'A's and 10 'B's ran for 20 minutes before I killed it. 在我杀死它之前,甚至只是扩大到10'A和10'B都跑了20分钟。

Is there more efficient way to approach this given the parameters I've described? 给定我描述的参数,是否有更有效的方法来解决此问题?

If all you need is the count, this can be generalized to n choose k . 如果您只需要计数,则可以将其推广为n选择k Your total size is n and the number of elements of "A" is k . 您的总大小为n ,而"A"的元素数为k So, your answer would be: 因此,您的答案将是:

(n choose k) = (40 choose 20) = 137846528820 (n选择k)=(40选择20)= 137846528820

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一个字符串中的数字与另一个等长字符串相乘,然后在Python中添加每个乘积 - How to Multiply the numbers within a string with another string of equal length and then add each product in Python 查找固定长度数字的所有可能排列以达到给定的总和 - Finding all possible permutations of a fixed length of numbers to reach a given sum 具有固定长度和一些字符的 Python 随机字符串/文本/代码生成器 - Python random string/text/code generator with fixed length and some characters Python 正则表达式用于固定字符长度 - Python regex for fixed character length 组号(如果它们在Python中彼此置换) - Group numbers if they are permutations of each other in Python Python:如何在固定字符串中的特定字符之前排除一组字符 - Python: How to exclude a group of characters before a specific character in a fixed string 如何在 Python 中将字节转换为相同长度的字符串(每个字节 1 个字符)? - How to convert bytes to a string of the same length in Python (1 character for each byte)? Python - 使字符串长度相等 - Python - make string equal length 从出现的给定字符中生成固定长度的随机字符串,发生次数相等 - Generate random strings of fixed length from given characters with equal occurance Python-CSV-每行数字到元组的所有排列 - Python - CSV - All the permutations of each row of numbers into tuples
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM