Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Summation of float string list

Python – Summation of float string list

Sometimes, while working with Python list, we can have a problem in which we need to find summation in list. But sometimes, we don’t have a natural number but a floating-point number in string format. This problem can occur while working with data, both in web development and Data Science domain. Let’s discuss the ways in which this problem can be solved. 

Method #1 : Using sum() + float() + generator This problem can be solved using the sum function in which we first convert the strings into float and then pass this logic in functions in respective sum function. 

Python3




# Python3 code to demonstrate working of
# Summation of float string list
# using sum() + float() + generator
 
# initialize lists
test_list = ['4.5', '7.8', '9.8', '10.3']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Summation of float string list
# using sum() + float() + generator
res_sum = sum(float(sub) for sub in test_list)
 
# printing result
print("The summation of float string list : " + str(res_sum))


Output : 

The original list is : ['4.5', '7.8', '9.8', '10.3']
The summation of float string list : 32.400000000000006

Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(1), as the space used is constant and does not depend on the size of the input list.

Method #2 : Using loop This is a brute force method to perform this task. In this, we iterate for the list and convert and sum the list float elements during iteration. 

Python3




# Python3 code to demonstrate working of
# Summation of float string list
# Using loop
 
# initialize lists
test_list = ['4.5', '7.8', '9.8', '10.3']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Summation of float string list
# Using loop
res_sum = 0
for ele in test_list:
    res_sum += float(ele)
     
# printing result
print("The summation of float string list : " + str(res_sum))


Output : 

The original list is : ['4.5', '7.8', '9.8', '10.3']
The summation of float string list : 32.400000000000006

Time complexity: O(n), where n is the length of the input list test_list. 
Auxiliary space: O(1), which means that the amount of extra memory used by the code is constant regardless of the size of the input list.

Method #3 : Using map() and sum()
This approach uses the map() function to convert all the strings in the list to floats, and then the sum() function to find the summation of the list.

Python3




# Python3 code to demonstrate working of
# Summation of float string list
# Using map() and sum()
 
# initialize lists
test_list = ['4.5', '7.8', '9.8', '10.3']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Summation of float string list
# Using map() and sum()
res_sum = sum(map(float, test_list))
 
# printing result
print("The summation of float string list : " + str(res_sum))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is : ['4.5', '7.8', '9.8', '10.3']
The summation of float string list : 32.400000000000006

Time complexity: O(n) where n is the number of elements in the list, since we are iterating over the elements of the list once.
Auxiliary space: O(1) for all methods, as we are not creating any new data structures or storing any additional data.

Method #4: Using list comprehension and sum()

  • Define a list of float strings named “test_list” with values [‘4.5’, ‘7.8’, ‘9.8’, ‘10.3’].
  • Print the original list using the print() function.
  • Use a list comprehension to iterate over each string in the “test_list”, convert it to a float using the float() function and store the result in a new list.
  • Pass the new list to the sum() function to calculate the summation of all the float values in the list.
    Store the result in the variable “res_sum”.
  • Print the result using the print() function.
  • The program is completed and the final output will be “The summation of float string list : {sum of floats in the list}”.

Python3




# Python3 code to demonstrate working of
# Summation of float string list
# Using list comprehension and sum()
 
# initialize lists
test_list = ['4.5', '7.8', '9.8', '10.3']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Summation of float string list
# Using list comprehension and sum()
res_sum = sum([float(i) for i in test_list])
 
# printing result
print("The summation of float string list : " + str(res_sum))
# This code is contributed by ChatGPT


Output

The original list is : ['4.5', '7.8', '9.8', '10.3']
The summation of float string list : 32.400000000000006

Time Complexity: O(n), where n is the length of the list.
Auxiliary Space: O(n), since we create a new list using list comprehension.

Method #5: Using reduce() from functools module

Steps:

  • Import the reduce() function from functools module.
  • Initialize the list with float string values.
  • Print the original list.
  • Use the reduce() function with a lambda function that converts each element to float and adds them together.
  • Store the result in the res_sum variable.
  • Print the result.

Python3




# Python3 code to demonstrate working of
# Summation of float string list
# Using reduce() from functools module
 
from functools import reduce
 
# initialize list
test_list = ['4.5', '7.8', '9.8', '10.3']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Summation of float string list
# Using reduce() from functools module
res_sum = reduce(lambda x, y: float(x) + float(y), test_list)
 
# printing result
print("The summation of float string list : " + str(res_sum))
# This code is contributed by ChatGPT


Output

The original list is : ['4.5', '7.8', '9.8', '10.3']
The summation of float string list : 32.400000000000006

Time complexity: O(n)
Auxiliary space: O(1)

Method #6: Using Numpy:

Algorithm:

  1. Import the NumPy library
  2. Define the test list of float strings
  3. Convert the list of float strings to a NumPy array using list comprehension and convert each element to a float
  4. Use the numpy.sum() function to compute the sum of the array created in step 3
  5. Print the original list and the result of the summation

Python3




import numpy as np
 
# initialize list
test_list = ['4.5', '7.8', '9.8', '10.3']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Summation of float string list
# Using numpy.sum() method
res_sum = np.sum([float(i) for i in test_list])
 
# printing result
print("The summation of float string list : " + str(res_sum))
#This code is contributed by Rayudu.


Output:

The original list is : ['4.5', '7.8', '9.8', '10.3']
The summation of float string list : 32.400000000000006

Time complexity: O(n), where n is the length of the test_list. The list comprehension operation takes linear time, and the numpy.sum() function takes constant time.

Space complexity: O(n), where n is the length of the test_list. The NumPy array created in step 3 takes up memory proportional to the size of the list.

Method #7: Using heapq:

Algorithm:

  1. Initialize the list of float string numbers.
  2. Convert each string to float using the map() function.
  3. Use heapq.merge() to merge all the converted float lists into a single list.
  4. Find the sum of all the float numbers using the sum() function.

Python3




import heapq
 
# initialize list
test_list = ['4.5', '7.8', '9.8', '10.3']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Summation of float string list
# Using heapq module
res_sum = sum(float(x) for x in heapq.merge(*[map(float, lst) for lst in [test_list]]))
 
# printing result
print("The summation of float string list : " + str(res_sum))
#This code is contributed by Vinay pinjala.


Output

The original list is : ['4.5', '7.8', '9.8', '10.3']
The summation of float string list : 32.400000000000006

Time Complexity:

Converting each string to float takes O(n) time where n is the number of elements in the list. The time complexity of merging the lists using heapq.merge() is O(k log k) where k is the total number of elements in all the lists. Finally, calculating the sum of all the numbers takes O(k) time. Therefore, the overall time complexity is O(k log k).

Auxiliary Space:

We are creating two new lists to store the converted float numbers and the merged list. Therefore, the space complexity is O(k) where k is the total number of elements in all the lists.

RELATED ARTICLES

Most Popular

Recent Comments