So I Have the str = 'Pizza' and I have list of words,for example: list = [ ['pizza'],['izza'] , ['zza'] ] how can I check all the possible combinations of "Pizza" to check if it is in the list? without using Numpy command p pi piz pizz pizza I iz izz izza etc.
Asked
Active
Viewed 72 times
-2
-
Refer to this answer [here](https://stackoverflow.com/questions/51538192/getting-all-combinations-of-a-string-and-its-substrings) – Mayank Nov 22 '20 at 12:30
3 Answers
0
def check_present_frm_string(a_word: str, check_list: list):
length = len(a_word)
i = 0
combination_list = list()
while i < length:
new_word = a_word[i:]
j = len(new_word)
while j != 0:
j_word = new_word[:j]
if j_word not in combination_list:
combination_list.append(j_word)
j -= 1
i += 1
for item in check_list:
if item in combination_list:
print(f'{item} is a valid combination')
else:
print(f'{item} is not a valid combination')
a_list = ['p', 'pi', 'piz', 'pizz', 'pizza', 'I', 'iz', 'izz', 'izza']
a_str = 'Pizza'
check_present_frm_string(a_str, a_list)
Results:
p is not a valid combination
pi is not a valid combination
piz is not a valid combination
pizz is not a valid combination
pizza is not a valid combination
I is not a valid combination
iz is a valid combination
izz is a valid combination
izza is a valid combination

Michael Fulcher
- 19
- 3
0
Actually,I Recived the word "Pizza" from a list and I Can't create another list with all the combinations like in a_list. I have to take the string - pizza and from it to create all the combinations.

Nikvu
- 19
- 1
0
def all_combinations_frm_string(a_word: str):
length = len(a_word)
i = 0
combination_list = list()
while i < length:
new_word = a_word[i:]
j = len(new_word)
while j != 0:
j_word = new_word[:j]
if j_word not in combination_list:
combination_list.append(j_word)
j -= 1
i += 1
return combination_list
print(all_combinations_frm_string('Pizza'))
Result:
['Pizza', 'Pizz', 'Piz', 'Pi', 'P', 'izza', 'izz', 'iz', 'i', 'zza', 'zz', 'z', 'za', 'a']

Michael Fulcher
- 19
- 3