Python List of Different Strings, add all the words together -
given list
sentence = [ "hello" , "world", "today good", "day"]
output should average of words, 1.75
i have far
for k in len(line): // here trying position 0,1,2,3etc k total += len(line[k].split()) homecoming total / len(line)
the error is: 'int' object not iterable
, did @ same problem on website, still don't understand problem. improve way of writing loop?
to iterate on positions:
for k in range(len(line)):
alternatively, iterate on sentence fragments directly:
for fragment in line: total += len(fragment.split())
or can replace loop generator expression:
total = sum(len(fragment.split()) fragment in line)
python list for-loop
No comments:
Post a Comment