Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding average length of strings in a list

I have a large list containing a lot of data which is read from a csv file. For simplicity I will give you a dummy list which will have far less data in it.

list1 = ['foo', 'bar', 'bob', 'jess', 'google', 'alphabet']

I want to find the average length of strings within the list. I am currently doing it like this:

all_lengths = []
num_of_strings = len(list1)

for item in list1:
    string_size = len(item)
    all_lengths.append(string_size)
    total_size = sum(all_lengths)
ave_size = float(total_size) / float(num_of_strings)

The problem is that because the real list is so large it is taking an incredibly long time to perform this.

Is there a more optimal or elegant way to perform this.

Also, for what it's worth, using Python2.7

like image 469
Joe Smart Avatar asked Dec 04 '22 02:12

Joe Smart


1 Answers

total_avg = sum( map(len, strings) ) / len(strings)

The problem in your code is in this line of code : total_size = sum(all_lengths) There's no need to calculate this in each loop of the cycle. Better make this after cycle.

like image 180
ig-melnyk Avatar answered Dec 13 '22 20:12

ig-melnyk