Esteb37
Esteb37

Reputation: 67

How to reduce similar if statements python?

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!

Upvotes: 3

Views: 1926

Answers (9)

BorrachaFox
BorrachaFox

Reputation: 1

I would use Dict Comprehensions like this:

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

Upvotes: 0

chepner
chepner

Reputation: 532303

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'

Upvotes: 7

ti7
ti7

Reputation: 18920

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.

Upvotes: 3

onur güngör
onur güngör

Reputation: 715

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"))

Upvotes: 1

whackamadoodle3000
whackamadoodle3000

Reputation: 6748

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

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

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

Upvotes: 7

pramod
pramod

Reputation: 1493

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

Upvotes: 2

Mr. Xcoder
Mr. Xcoder

Reputation: 4795

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

finposb = chr(int(finposb)+95)

Without hard-coding a list for it.

Upvotes: 2

Gahan
Gahan

Reputation: 4213

>>> 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.

Upvotes: 2

Daniel
Daniel

Reputation: 42778

Use a dictionary:

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

Upvotes: 3

Related Questions