Python program to find the sum of Characters ascii values in String List
Last Updated :
08 Feb, 2024
Given the string list, the task is to write a Python program to compute the summation value of each character's ASCII value.
Examples:
Input : test_list = ["geeksforgeeks", "teaches", "discipline"]
Output : [133, 61, 100]
Explanation : Positional character summed to get required values.
Input : test_list = ["geeksforgeeks", "discipline"]
Output : [133, 100]
Explanation : Positional character summed to get required values.
Method 1 : Using ord() + loop
In this, we iterate each character in each string and keep on adding positional values to get its sum. The summed value is appended back to the result in a list.
Python3
# Python3 code to demonstrate working of
# Characters Positions Summation in String List
# Using ord() + loop
# initializing list
test_list = ["geeksforgeeks",
"teaches", "us", "discipline"]
# printing original list
print("The original list is : " + str(test_list))
res = []
for sub in test_list:
ascii_sum = 0
# getting ascii value sum
for ele in sub :
ascii_sum += (ord(ele) - 96)
res.append(ascii_sum)
# printing result
print("Position Summation List : " + str(res))
Output:
The original list is : ['geeksforgeeks', 'teaches', 'us', 'discipline'] Position Summation List : [133, 61, 40, 100]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using list comprehension + sum() + ord()
In this, we get summation using sum(), ord() is used to get the ASCII positional value, and list comprehension offers one-liner solution to this problem.
Python3
# Python3 code to demonstrate working of
# Characters Positional Summation in String List
# Using list comprehension + sum() + ord()
# initializing list
test_list = ["geeksforgeeks", "teaches",
"us", "discipline"]
# printing original list
print("The original list is : " + str(test_list))
# sum() gets summation, list comprehension
# used to perform task in one line
res = [sum([ord(ele) - 96 for ele in sub]) for sub in test_list]
# printing result
print("Positional Summation List : " + str(res))
Output:
The original list is : ['geeksforgeeks', 'teaches', 'us', 'discipline'] Position Summation List : [133, 61, 40, 100]
The time and space complexity for all the methods are the same:
Time Complexity : O(n2)
Auxiliary Space : O(n)
Method 3 : Using map and lambda
Here's another approach using map() function to calculate the summation of ASCII values of each character:
Python3
# Python3 code to demonstrate working of
# Characters Positional Summation in String List
# Using map() function
# initializing list
test_list = ["geeksforgeeks", "teaches",
"us", "discipline"]
# printing original list
print("The original list is : " + str(test_list))
# map() function to calculate sum of ASCII values
res = list(map(lambda x: sum(map(lambda y: ord(y) - 96, x)), test_list))
# printing result
print("Positional Summation List : " + str(res))
OutputThe original list is : ['geeksforgeeks', 'teaches', 'us', 'discipline']
Positional Summation List : [133, 61, 40, 100]
This method also has the same time and space complexity as the other two methods, i.e.,
Time Complexity: O(n^2)
Auxiliary Space: O(n)
Method 4 : using the reduce function from the functools module along with a lambda function.
Step-by-step approach:
- Import the functools module.
- Initialize the list containing the strings.
- Define a lambda function that takes two arguments a and b, and returns the sum of their ordinal values (ASCII value - 96).
- Use the reduce function along with the lambda function to get the positional summation of the characters in each string.
- Print the resultant list.
Python3
import functools
# initializing list
test_list = ["geeksforgeeks", "teaches", "us", "discipline"]
# printing original list
print("The original list is : " + str(test_list))
# using reduce() + lambda to compute positional summation
res = [functools.reduce(lambda a, b: a + ord(b) - 96, sub, 0) for sub in test_list]
# printing result
print("Positional Summation List : " + str(res))
OutputThe original list is : ['geeksforgeeks', 'teaches', 'us', 'discipline']
Positional Summation List : [133, 61, 40, 100]
Time complexity: O(n*m), where n is the number of strings in the list and m is the maximum length of any string.
Auxiliary space: O(n), as we are storing the positional summation of each string in a list.
Similar Reads
Python Program to Find ASCII Value of a Character Given a character, we need to find the ASCII value of that character using Python. ASCII (American Standard Code for Information Interchange) is a character encoding standard that employs numeric codes to denote text characters. Every character has its own ASCII value assigned from 0-127. Examples:
2 min read
Python program to calculate the number of words and characters in the string We are given a string we need to find the total number of words and total number of character in the given string.For Example we are given a string s = "Geeksforgeeks is best Computer Science Portal" we need to count the total words in the given string and the total characters in the given string. I
3 min read
Ways to Convert List of ASCII Value to String - Python The task of converting a list of ASCII values to a string in Python involves transforming each integer in the list, which represents an ASCII code, into its corresponding character. For example, with the list a = [71, 101, 101, 107, 115], the goal is to convert each value into a character, resulting
3 min read
Python program to extract characters in given range from a string list Given a Strings List, extract characters in index range spanning entire Strings list. Input : test_list = ["geeksforgeeks", "is", "best", "for", "geeks"], strt, end = 14, 20 Output : sbest Explanation : Once concatenated, 14 - 20 range is extracted.Input : test_list = ["geeksforgeeks", "is", "best",
4 min read
Python program to find sum of elements in list Finding the sum of elements in a list means adding all the values together to get a single total. For example, given a list like [10, 20, 30, 40, 50], you might want to calculate the total sum, which is 150. Let's explore these different methods to do this efficiently.Using sum()sum() function is th
3 min read
Python program to print words from a sentence with highest and lowest ASCII value of characters Given a string S of length N, representing a sentence, the task is to print the words with the highest and lowest average of ASCII values of characters. Examples: Input: S = "every moment is fresh beginning"Output:Word with minimum average ASCII values is "beginning".Word with maximum average ASCII
5 min read
Convert String List to ASCII Values - Python We need to convert each character into its corresponding ASCII value. For example, consider the list ["Hi", "Bye"]. We want to convert it into [[72, 105], [66, 121, 101]], where each character is replaced by its ASCII value. Let's discuss multiple ways to achieve this.Using List Comprehension with o
2 min read
Python Program to Count characters surrounding vowels Given a String, the task is to write a Python program to count those characters which have vowels as their neighbors. Examples: Input : test_str = 'geeksforgeeksforgeeks' Output : 10 Explanation : g, k, f, r, g, k, f, r, g, k have surrounding vowels. Input : test_str = 'geeks' Output : 2 Explanation
3 min read
Python program to calculate the number of digits and letters in a string In this article, we will check various methods to calculate the number of digits and letters in a string. Using a for loop to remove empty strings from a list involves iterating through the list, checking if each string is not empty, and adding it to a new list.Pythons = "Hello123!" # Initialize cou
3 min read
Iterate over characters of a string in Python In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Letâs explore this approach.Using for loopThe simplest way to iterate over the characters in
2 min read