- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Map function and Dictionary in Python to sum ASCII values
We want to calculate the ASCII sum for each word in a sentence and the sentence as a whole using map function and dictionaries. For example, if we have the sentence −
"hi people of the world"
The corresponding ASCII sums for the words would be : 209 645 213 321 552
And their total would be : 1940.
We can use the map function to find the ASCII value of each letter in a word using the ord function. Then using the sum function we can sum it up. For each word, we can repeat this process and get a final sum of ASCII values.
Example
sent = "hi people of the world" words = sent.split(" ") result = {} # Calculate sum of ascii values for every word for word in words: result[word] = sum(map(ord,word)) totalSum = 0 # Create an array with ASCII sum of words using the dict sumForSentence = [result[word] for word in words] print ('Sum of ASCII values:') print (' '.join(map(str, sumForSentence))) print ('Total of all ASCII values in sentence: ',sum(sumForSentence))
Output
This will give the output −
Sum of ASCII values: 209 645 213 321 552 Total of all ASCII values in a sentence: 1940
- Related Articles
- How to sum values of a Python dictionary?
- Sum 2D array in Python using map() function
- How to map two lists into a dictionary in Python?
- Append Dictionary Keys and Values (In order ) in dictionary using Python
- Map function and Lambda expression in Python to replace characters
- Sort Dictionary key and values List in Python
- How to replace values in a Python dictionary?
- Accessing Values of Dictionary in Python
- How to update a Python dictionary values?
- Python – Inverse Dictionary Values List
- How to sort a dictionary in Python by values?
- Convert key-values list to flat dictionary in Python
- How to create Python dictionary from list of keys and values?
- Extract Unique dictionary values in Python Program
- How to replace values of a Python dictionary?

Advertisements