alphabet = 'abcdefghijklmnopqrstuvwxyz'
def unpack(arg):
d = {}
for index, item in enumerate(arg):
d[item] = d.get(item, index) + 1
return d
alphabet_unpacked = unpack(alphabet)
def alphabet_position(text):
text_lowercase = text.lower()
result = ''
i = 0
while i < len(text_lowercase):
if text_lowercase[i] not in alphabet:
continue
result += str(alphabet_unpacked.get(text_lowercase[i]))
i += 1
return result
if text_lowercase[i] not in alphabet:
continue
alphabet
? if text_lowercase[i] not in alphabet:
continue
Расскажите о своем мнении, как сделать код лучше?
alphabet = 'abcdefghijklmnopqrstuvwxyz'
text = "The sunset sets at twelve o'clock."
def alphabet_position(text):
text = ' '.join(char for char in text.lower() if char.isalpha())
return text.translate({ord(c): str(i) for i,c in enumerate(alphabet,1)})
print(alphabet_position(text))
In [4]:
20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11
alphabet = set('abcdefghijklmnopqrstuvwxyz')
def alphabet_position(text):
lower_text = text.lower()
result = ''
for char in lower_text:
if char not in alphabet:
result += char
return result