python – How to create dictionary in which key and value are taken from string with condition?


I have a string of characters and numbers that I created randomly and replaced punctuations with whitespace. I want to be able to find ‘words’, more specifically, string of characters that are divided by space, that start and end with digits and make those two digits key in a dictionary and the value of this key would be that same word. If a word only starts or ends with digit then I want this digit to be a key and value to be that ‘word’. If a ‘word’ doesn’t start or end with digit I want it to be the value of ‘other’ key.

import random



alfa = ('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', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '.', ',', ':', ';')

st=''.join((random.choice(alfa) for i in range(N)))

st = (item.replace(".", " ") for item in st)
st = (item.replace(",", " ") for item in st)
st = (item.replace(":", " ") for item in st)
st = (item.replace(";", " ") for item in st)

arr = "".join(st)

I’ve tried creating function that preforms the task I want but each time I failed.