I am trying to generate a list of possible strings of characters from input, accounting for wildcards "?" and "*". The ordering of list items should not matter, meaning if ABC already exists in the list, then I do not want to add ACB, or any other ordering (processing speed issue). The code I am using is below:
import itertools
from itertools import permutations
#####################################################
def getWordsFromTiles(tiles, word):
#####################################################
return all(word.count(i) <= tiles.count(i) for i in word)
#####################################################
Main
#####################################################
chars = A?*
wilds = [
('A','B','C','D','E','F','G','H','I','J','K',
'L','M','N','O','P','Q','R','S','T','U','V',
'W','X','Y','Z')
if char == "?" or char == "*" else (char) for char in chars]
for p in itertools.product(*wilds):
x = ''.join(p)
hits.extend([word for word in data if getWordsFromTiles(x.upper(), word) and word not in hits])
This generates a list like the following:
A,A,A
A,A,B
A,A,C
.....
A,B,A
I actually do not care about the order of these list, so I would like to not have A,B,A when I have already generated "A,A,B. Any ideas how to implement this?
actually do not care about the order of these list, so I would like to not have A,B,A when I have already generated "A,A,B".
Is this what you are looking for?
from itertools import combinations_with_replacement
s = ['A','B','C','D']
list(combinations_with_replacement(s,3))
[('A', 'A', 'A'),
('A', 'A', 'B'),
('A', 'A', 'C'),
('A', 'A', 'D'),
('A', 'B', 'B'),
('A', 'B', 'C'),
('A', 'B', 'D'),
('A', 'C', 'C'),
('A', 'C', 'D'),
('A', 'D', 'D'),
('B', 'B', 'B'),
('B', 'B', 'C'),
('B', 'B', 'D'),
('B', 'C', 'C'),
('B', 'C', 'D'),
('B', 'D', 'D'),
('C', 'C', 'C'),
('C', 'C', 'D'),
('C', 'D', 'D'),
('D', 'D', 'D')]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.