3

I'm working on a project where I need to change numerical values into alphabetical characters, currently, I have this:

if finposb == "2":
    finposb = "a"
if finposb == "3":
    finposb = "b"
if finposb == "4":
    finposb = "c"
if finposb == "5":
    finposb = "d"
if finposb == "6":
    finposb = "e"
if finposb == "7":
    finposb = "f"
if finposb == "8":
    finposb = "g"
if finposb == "9":
    finposb = "h"

I would like to know if there's a way to reduce that to a shorter code line, thanks!

1
  • 3
    make a dictionary for this Commented May 27, 2017 at 15:33

9 Answers 9

7
letters='abcdefghijklmnopqrstuvwxyz'
finposb=letters[int(finposb)-2]

This should work, no dictionaries needed. If you want it even shorter:

finposb='abcdefghijklmnopqrstuvwxyz'[int(finposb)-2]
Sign up to request clarification or add additional context in comments.

1 Comment

You can index a string; no list is necessary.
7

You don't need any intermediate data structures; use the ASCII value (or Unicode code point in Python 3) of finposb.

# ord("a") - ord("2") == 47
finposb = chr(ord(finposb) + 47)

If you didn't have a nice, implicit rule like this, you can use string.maketrans to make a translation table, and string.translate to apply that table to your input.

>>> tbl = string.maketrans("23456789", "abcdefgh")
>>> string.translate("2", tbl)
'a'

translate acts as an identity function if the first argument does not appear in the translation table:

>>> string.translate("z", tbl)
'z'

Comments

3

Use a dictionary:

DIGIT2LETTER = {
    "2": "a", "3": "b", "4": "c", "5": "d", "6": "e", "7": "f", "8": "g", "9": "h"
}
finposb = DIGIT2LETTER[finposb]

Comments

3

In this case, a dictionary is probably what you're looking for.

finposb = {
    "2": "a",
    ...
    "9": "h"
}

>>> print(finposb["2"])
a

An advantage of a dictionary is that you may map several keys to the same value, for example if you wanted both "2" and 2 to map to "a", you could say

finposb["2"] = "a"  # string
finposb[2] = "a"    # numeric

Further, there are two reasonable ways of acquiring your value from a key (such as "2" to "a").

finposb[key]  # raise KeyError if key is not in the dictionary
finposb.get(key, None)  # default to None if the key is missing

The first is convenient because it throws a useful error and can be certain that the key is not in your dictionary, while the second has many other conveniences, such as being able to return itself if the key is missing.

finposb.get(key, key)  # returns key if key does not map to a value

Classically, this type of table is used to look up a character set, such as ASCII where many characters may be stored in a compact way (how else would you express a letter to a computer than as a number?) and later interpreted by a program.

A more modern form of this is called unicode, which can be used to describe a very great number of different characters beyond the "normal" Latin Alphabet.

Comments

2
>>> pool = {"2": "a", "3": "b", "4": "c", "5": "d", "6": "e", "7": "f", "8": "g", "9": "h"}
>>> finposb = '2'  # get any digit here
>>> if str(finposb) in pool.keys():
    finposb = pool[str(finposb)]


>>> finposb
'a'

if you use digits as string in dictionary then represent it as string in whole snippet.

2 Comments

You don't need to keep calling str; all the values are already strings.
I have used str() method for digit to be check in case if it is in integer format then program execution don't break.
2

You can abuse of chr() built-in and get:

finposb = chr(int(finposb)+95)

Without hard-coding a list for it.

Comments

2
import string
finposb = string.ascii_lowercase[int(finposb)-2]

2 Comments

removed characters hardcoding
string.ascii_lowercase[...] is legal; no need to build a list first.
1

Use this

import string
finposb.translate(string.maketrans("".join([str(i) for i in range(2,10)]), "abcdefgh"))

or simpler

import string
finposb.translate(string.maketrans("23456789", "abcdefgh"))

Comments

0

I would use Dict Comprehensions like this:

alphabet = 'abcdefghijklmnopqrstuvwxyz'
finposd = {letter:alphabet.index(letter) + 2 for letter in alphabet}

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.