Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sum char word dictionary
#1
I have a dictionary that gives me a letter of the alphabet as a key and a number that corresponds to the position of the letter of the alphabet.
Quote:{'a':1 'b':2 ... 'z':26}
I would like that if I take a string, make the sum of the individual values.
I created the dictionary to do it, but I don't know how to do the sum inside the dictionary because it's not a string.


To better understand what I want:

Quote: example : l + o + v + e = 54
list_alphabet = {} 
alpha = 'a' #first letter in alphabet
for i in range(0,26):
    list_alphabet[alpha] = i +1
    alpha = chr(ord(alpha)+1)
print(list_alphabet)

# for break word --> use split() ?

Never mind I resolved!!!
def wordsToMarks(word):
  res = 0
  list_alphabet = {} 
  alpha = 'a' #first letter in alphabet
  for i in range(0,26):
    list_alphabet[alpha] = i +1
    alpha = chr(ord(alpha)+1)
  
  for letter in word:
    res += list_alphabet.get(letter,0)
  return res

word = 'love'
print(wordsToMarks(word))
Reply
#2
What about uppercase chars?

One can take advantage of fact that lowercase ascii letters codepoints are 97-122. Just deduct 96 from codepoint value:

>>> sum(ord(char)-96 for char in 'love')
54
Of course one should be defensive (what should happen if letters not in this range are encountered like uppercase etc)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
I don't know maybe I should check it but how do I do it?
Reply
#4
One way is to check range (just ignoring everything else):

>>> sum(ord(char) - 96 for char in 'love' if ord(char) in range(97, 123))
54
As one can observe we do ord(char) twice, so in Python 3.8 we can take advantage of walrus operator:

>>> sum(codepoint - 96 for char in 'love' if (codepoint := ord(char)) in range(97, 123))
54
Alternatively we can move calculation into if condition:

>>> sum(codepoint for char in 'love' if (codepoint := ord(char) - 96) in range(1, 27))
54
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,455 Aug-12-2021, 04:25 PM
Last Post: palladium
  How to replace on char with another in a string? korenron 3 2,294 Dec-03-2020, 07:37 AM
Last Post: korenron
  How to remove char from string?? ridgerunnersjw 2 2,486 Sep-30-2020, 03:49 PM
Last Post: ridgerunnersjw
  Python Speech recognition, word by word AceScottie 6 15,866 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  print a word after specific word search evilcode1 8 4,725 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  The use of escape char \ hishamzero1 2 2,341 Aug-12-2019, 10:20 PM
Last Post: hishamzero1
  First non-repeating char and some errors. blackknite 1 2,238 Jan-06-2019, 02:19 PM
Last Post: stullis
  json.dumps save some unicode chars as code, not char buran 1 2,882 Aug-02-2018, 04:02 PM
Last Post: buran
  pyserial char by char io fcagney 4 6,176 May-15-2018, 10:45 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020