Let’s say we have this data structure:
class Lock:
def __init__(self):
self.data1 = ['a', 'd', 'e', 'l', 's']
self.data2 = ['s', 'i', 'r', 't', 'n']
self.data3 = ['b', 'o', 'e', 'm', 'k']
self.data4 = ['f', 'y', 'u', 'n', 'g']
Alternatively,
d = {'1': ['a', 'd', 'e', 'l', 's'], '2': ['s', 'i', 'r', 't', 'n'], '3': ['b', 'o', 'e', 'm', 'k'], '4': ['f', 'y', 'u', 'n', 'g'] }
I want to find every possible combination of letters, given that each letter is selected from a different key or array. Order matters, so that the first letter always has to be from ‘data1’, second has to be from ‘data2’, etc.
The purpose is to then check these against a dictionary to see which ones are english-valid words. I assumed getting a list of all the combinations, and then doing the check would be the fastest, but if that’s not the case, I’d like some input.
Use
itertools.product():or:
Demo: