1

I'm trying to write a function that consumes a list of keycode values, and produces a string corresponding to these values. Each keycode contains 2 values, the first corresponding to a number of a phone and the second corresponding to a certain value from the list of values that define each number. I'm having trouble extracting these values. This is the list of values that correspond with each number:

0 == [" "]
1 == [".", ",", "?"]
2 == ["a", "b", "c"]
3 == ["d", "e", "f"]
4 == ["g", "h", "i"]
5 == ["j", "k", "l"]
6 == ["m", "n", "o"]
7 == ["p", "q", "r", "s"]
8 == ["t", "u", "v"]
9 == ["w", "x", "y", "z"]`

If the list of keyed values that I'm consuming is keypresses: [[6,3], [0, 1], [5, 2]] How can I extract certain values from the definitions above? I'm thinking about using .join(list) and map for once I have the values extracted.

1
  • a/ make a dict or an array out of the data you have. typically, [[' '], ['.',',','?'], ...]or {'0': [' '], ...}. b/ access that data using the input you have : ''.join(values[key][cnt - 1] for (key, cnt) in keypresses) Commented Jun 30, 2015 at 2:40

3 Answers 3

1

Try using a list of lists, like this:

lsts = [[" "],
        [".", ",", "?"],
        ["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"]]

Now you can access each sublist by its index:

lsts[1]
=> [".", ",", "?"]

And each element by both of its indexes:

lsts[1][2]
=> "?"

Now it's easy to extract values from a list of key presses, like those shown in the question and then join them:

keypresses = [[6, 3], [0, 1], [5, 2]]
''.join(lsts[i][j-1] for i, j in keypresses)
=> "n k"

Notice that the indexes start from zero, so I had to subtract one unit from the sample key presses given in the question.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for a solution thats easy to understand and works perfectly!!
1

This should do what your looking for.

def key_pressed(key, character):
    """
    :param key: index of keyboard button
    :param character: desired character represented by key
    :return: requested character
    """
    lookup = [[" "],
              [".", ",", "?"],
              ["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"]]

    return lookup[key][character]


print(key_pressed(6, 2))
print(key_pressed(1, 2))
print(key_pressed(5, 1))

Output:

o
?
k

Comments

0

I would suggest you define a dictionary that holds the numbers and corresponding values like so:

keyvalmap = {0: [" "], 1: [".", ",", "?"]} #and so on...

Then iterate over keypresses:

for key, times in keypresses:
    print keyvalmal[key][times-1] # or whatever you want to do

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.