[英]Permutations of a list with 16 integers but only if 4 conditions are fulfilled
[英]Permutations with multiple list and Conditions
我想生成兩個列表的每 5 個字符排列的所有可能字符串的列表
List 1: [A-Z] all caps Alphabets
List 2: [0-9] Digits
字符串的條件
示例輸出:
B9B61
6F084
7C9DA
9ECF9
E7ACF
我試過了,但我知道到目前為止我無法應用條件,現在很困惑,請幫忙
from itertools import permutations
perm = [''.join(p) for p in permutations('ABCDEFGHIJKLMOPQRSTUVWXYZ0123456789', 5)]
for i in list(perm):
with open("list.txt", "a+") as file:
file.write(i)
file.write("\n")
以下將打印出所有大寫字母和數字的所有 5 個字符組合,任何字符或數字不超過 3 個。
import string
import itertools
possible_combinations = itertools.permutations(string.ascii_uppercase * 3 + string.digits * 3, 5)
for possible_str in (''.join(chars) for chars in possible_combinations):
if possible_str.isnumeric() or possible_str.isalpha():
continue
else:
print(possible_str)
為了獲得不超過 3 個重復的字母和數字的所有組合,我們首先查看itertools.permutations
。 給定一個可迭代的字符(一個字符串),它將返回給定長度的這些字符的所有組合
>>> [''.join(x) for x in itertools.permutations('ABC', 3)]
['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']
如果我們傳遞每個字符的 2 個,排列將返回每個字符最多 2 個的所有組合
>>> [''.join(x) for x in itertools.permutations('AABBCC', 3)]
['AAB', 'AAB', 'AAC', 'AAC', 'ABA', 'ABB', 'ABC', 'ABC', 'ABA', 'ABB', 'ABC', 'ABC', 'ACA', 'ACB', 'ACB', 'ACC', 'ACA', 'ACB', 'ACB', 'ACC', 'AAB', 'AAB', 'AAC', 'AAC', 'ABA', 'ABB', 'ABC', 'ABC', 'ABA', 'ABB', 'ABC', 'ABC', 'ACA', 'ACB', 'ACB', 'ACC', 'ACA', 'ACB', 'ACB', 'ACC', 'BAA', 'BAB', 'BAC', 'BAC', 'BAA', 'BAB', 'BAC', 'BAC', 'BBA', 'BBA', 'BBC', 'BBC', 'BCA', 'BCA', 'BCB', 'BCC', 'BCA', 'BCA', 'BCB', 'BCC', 'BAA', 'BAB', 'BAC', 'BAC', 'BAA', 'BAB', 'BAC', 'BAC', 'BBA', 'BBA', 'BBC', 'BBC', 'BCA', 'BCA', 'BCB', 'BCC', 'BCA', 'BCA', 'BCB', 'BCC', 'CAA', 'CAB', 'CAB', 'CAC', 'CAA', 'CAB', 'CAB', 'CAC', 'CBA', 'CBA', 'CBB', 'CBC', 'CBA', 'CBA', 'CBB', 'CBC', 'CCA', 'CCA', 'CCB', 'CCB', 'CAA', 'CAB', 'CAB', 'CAC', 'CAA', 'CAB', 'CAB', 'CAC', 'CBA', 'CBA', 'CBB', 'CBC', 'CBA', 'CBA', 'CBB', 'CBC', 'CCA', 'CCA', 'CCB', 'CCB']
如果我們增加字符的數量以及我們重復它們的數量,情況也是如此。 所以我們有string.ascii_uppercase * 3 + string.digits * 3
它返回一個字符串,其中所有大寫字母重復 3 次,所有數字重復 3 次
最后一件事是過濾掉沒有數字或字母的字符串。 如果所有字符都是數字,則 string.isnumeric() 返回 true,如果所有字符都是字母,則 string.isalpha() 返回 true
您可以通過應用一些檢查來進行permutations
:
from itertools import permutations
from collections import Counter
for i in permutations('ABCDEFGHIJKLMOPQRSTUVWXYZ0123456789', 5):
combo = "".join(i)
result = Counter(i)
if any(s>=3 for s in result.values()) \
or sum(c.isdigit() for c in i)<1 \
or sum(c.isalpha() for c in i)<1:
continue
print (combo)
import random
output = [] #always initiate as empty
alphanum = ['A','B','C','D','E','F','G','H','I','J','K','L','M','O','P','Q','R','S','T','U','V','W','X','Y','Z',0,1,2,3,4,5,6,7,8,9]
while len(output) < 5:#5 is max
output.append(random.choice(alphanum)) #pick random from list
counter_i = counter_s = 0 #counter for integers and characters
for element in output:
if isinstance(element,int):
counter_i +=1
elif isinstance(element,str):
counter_s +=1
if (counter_i > 3 or counter_s >3) : #max 3 for numbers or strings
output.pop() #remove from list if it exceeds the limits
print(output)
['M', 'Z', 4, 'B', 9]
result = ''
for l in output:
result +=str(l)
print(result)
MZ4B9
你可以試試這個功能。 它返回唯一且至少一個字母和一位數字。
import random
alphabets = "ABCDEFGHIJKLMOPQRSTUVWXYZ"
digits = "0123456789"
def generate_code(length):
alp_len = random.randint(1,length-1)
dig_len = length-alp_len
selected = list(random.sample(alphabets,alp_len))
selected.extend (random.sample(digits,dig_len))
random.shuffle ( selected )
return ''.join(selected)
print (generate_code(5))
random.sample(人口,k)
返回從種群序列中選擇的唯一元素的 ak 長度列表。 用於不放回的隨機抽樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.