'Python - Need to extract the first and last letter from a word string

I've been stuck with this for a while. basically the get_text_value(text) code below is passed a string of words and it extracts the first and last letter of each word, matches it up with letter_values and gives the final value based on the sum of the numbers the first and last words matched up with. So far I've only been able to "extract" the first and last letter of the whole string and not each individual word. I know I'm meant to convert the string into a list, but I'm not entirely sure how to go about it. Any help would be greatly appreciated. Thanks!

def get_text_value(text):
     letters = ['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']
     letter_values = [1, 4, 2, 3, 1, 4, 3, 4, 1, 7, 7, 4, 6, 6, 1, 3, 9, 2, 2, 2, 1, 8, 5, 9, 9, 9]
     text_value = 0
     text = text.lower()
     for i in text:
          if text[0] and text[-1] in letters:
               letters_index_1 = letters.index(text[0])
               letters_index_2 = letters.index(text[-1])
               text_value = letter_values[letters_index_1] + letter_values[letters_index_2]

     return text_value

def test_get_text_value():
     print("1. Text value:", get_text_value("abracadabra")) 
     print("2. Text value:", get_text_value("a b"))
     print("3. Text value:", get_text_value("enjoy Today"))
     print("4. Text value:", get_text_value(""))

     text = "Be yourself everyone else is already taken"
     text_value = get_text_value(text)
     print("6. Text:", text)
     print("6. Text value:", text_value)


Solution 1:[1]

First of all: store the values in a dictionary, that's way less work.

Then, just iterate over the words in the text and add the values:

def get_text_value(text):
    lv = {'a': 1, 'b': 4, 'c': 2, 'd': 3, 'e': 1, 'f': 4, 'g': 3, 'h': 4, 'i': 1, 'j': 7, 'k': 7, 'l': 4, 'm': 6, 'n': 6, 'o': 1, 'p': 3, 'q': 9, 'r': 2, 's': 2, 't': 2, 'u': 1, 'v': 8, 'w': 5, 'x': 9, 'y': 9, 'z': 9}
    return sum(
              lv.get(word[:1], 0) + lv.get(word[-1:], 0)
              for word in text.lower().split(" ")
              )

Solution 2:[2]

Looks like you're just getting started with Python. Welcome! Couple things that might be worth getting more familiar with.

  1. Dictionaries

Dictionaries are good for storing a key value pair. In your case, a dictionary would work really well as you have a letter (your key) and a numerical value (the value). As mentioned in the other answer, a dictionary is going to significantly clean up your code.

  1. String methods.

The function you need to go from getting the first and last letter of the entire sentence to the first and last of each word is split. It will split one string into a list of strings where it finds a space.

>>> "enjoy Today".split(" ")
['enjoy', 'Today']
>>> "abracadabra".split(" ")
['abracadabra']

You don't have to just split on spaces.

>>> "abracadabra".split("b")
['a', 'racada', 'ra']
  1. List comprehension

Just google it as there are lots of articles that will do a better job explaining then I will here.

These 3 methods are a few of the key building blocks that will make Python enjoyable to write and your code concise and easy to read.

Solution 3:[3]

do like this:

def get_text_value2(text):
    letters = [chr(i) for i in range(ord('a'), ord('z')+1)]
    letter_values = [1, 4, 2, 3, 1, 4, 3, 4, 1, 7, 7, 4, 6, 6, 1, 3, 9, 2, 2, 2, 1, 8, 5, 9, 9, 9]
    alpha_value_dict = dict(zip(letters, letter_values))
    formatted_text = text.lower()
    return alpha_value_dict.get(text[0], 0) + alpha_value_dict.get(text[-1], 0)

or if you want keep the same logic of return value with your origional function, you can do:

def get_text_value2(text):
    letters = [chr(i) for i in range(ord('a'), ord('z')+1)]
    letter_values = [1, 4, 2, 3, 1, 4, 3, 4, 1, 7, 7, 4, 6, 6, 1, 3, 9, 2, 2, 2, 1, 8, 5, 9, 9, 9]
    alpha_value_dict = dict(zip(letters, letter_values))
    formatted_text = text.lower()
    try:
        return alpha_value_dict[text[0]] + alpha_value_dict[text[-1]]
    except:
        return 0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 L3viathan
Solution 2 Liyan Chang
Solution 3