Home > Enterprise >  Assign value numbers for alphabet in Python
Assign value numbers for alphabet in Python

Time:12-04

I have alphabets that I want to assign as follows:

lowercase items a-z have value of 1-26 uppercase items A-Z have value of 27-52

What is the shortest way to implement this

[a,B,h,R] Expected Output: [1,28,8,44]

How can we go about doing this in Python

Thank you

CodePudding user response:

The python string module is perfect for this.

from string import ascii_letters
print([ascii_letters.index(letter)   1 for letter in ["a", "B", "h", "R"]])

CodePudding user response:

This is a way that you can implement what you want:

print([ord(item) - 38 if ord(item) < 97 else ord(item) - 96 for item in ['a','B','h','R']])

converting each item into an int value and finding which positioning they are in (Capitalized letters come before lowercase)

https://appdividend.com/2022/06/15/how-to-convert-python-char-to-int/

  • Related