+ 2
how to have two different keys for the same value in a dictionary in python
I was just trying to program a code to get the position of the alphabet wordval = { "a" or "A": 1, } print(wordval["A"]) but when i try it myself then there comes an error Pls. guide me how tackle this problem
6 odpowiedzi
+ 3
This is not a direct answer to your question, but since you only want the value you don't need a dict at all, you can do it like this:
def pos(x):
if ord(x)>ord('Z'):
return ord(x)-ord('a')+1
return ord(x)-ord('A')+1
+ 3
smiley the code I wrote doesn't give you the ASCII value, it gives you the exact result as your dictionnary. pos('a') and pos('A') will return 1, pos('b') and pos('B') will return 2, and so on.
+ 2
just assign both to the same value:
dict = {
"a": 1,
"A": 1,
}
If this doesn't work you could also use the string.lower() method when trying to access the dictionary:
dict = {
"a": 1
}
print(dict["A".lower()])
+ 1
Aymane Boukrouh thanks sir
0
Aymane Boukrouh sir I don't want the ASCII value of the alphabet but wanted to print the place value of the alphabet
Like I did here
https://code.sololearn.com/cXdJ3uL74z0X/?ref=app
Please help me !!
0
Something like this?...
https://code.sololearn.com/capk5h8FWMHt/?ref=app