Python Summation of float string list (original) (raw)

Last Updated : 02 May, 2023

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

test_list = [ '4.5' , '7.8' , '9.8' , '10.3' ]

print ("The original list is : " + str (test_list))

res_sum = sum ( float (sub) for sub in test_list)

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

test_list = [ '4.5' , '7.8' , '9.8' , '10.3' ]

print ("The original list is : " + str (test_list))

res_sum = 0

for ele in test_list:

`` res_sum + = float (ele)

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

test_list = [ '4.5' , '7.8' , '9.8' , '10.3' ]

print ( "The original list is : " + str (test_list))

res_sum = sum ( map ( float , test_list))

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 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()

Python3

test_list = [ '4.5' , '7.8' , '9.8' , '10.3' ]

print ( "The original list is : " + str (test_list))

res_sum = sum ([ float (i) for i in test_list])

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 list.
Auxiliary Space: O(n), since we create a new list using list comprehension.

Method #5: Using reduce() from functools module

Steps:

Python3

from functools import reduce

test_list = [ '4.5' , '7.8' , '9.8' , '10.3' ]

print ( "The original list is : " + str (test_list))

res_sum = reduce ( lambda x, y: float (x) + float (y), test_list)

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)
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

test_list = [ '4.5' , '7.8' , '9.8' , '10.3' ]

print ( "The original list is : " + str (test_list))

res_sum = np. sum ([ float (i) for i in test_list])

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 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

test_list = [ '4.5' , '7.8' , '9.8' , '10.3' ]

print ( "The original list is : " + str (test_list))

res_sum = sum ( float (x) for x in heapq.merge( * [ map ( float , lst) for lst in [test_list]]))

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:

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.