Python: set(), map(), input(), sum(), len()

Calculate average.

nick3499
1 min readSep 22, 2017

Input integers:

161 182 161 154 176 170 167 171 170 174

Script calculates average:

169.375

input() appends space-separated values to list of strings:

['161', '182', '161', '154', '176', '170', '167', '171', '170', '174']

map() them maps strings to integers:

<map object at 0x7f89f547a2e8>

set() then converts map object to set of integers:

{161, 167, 170, 171, 174, 176, 182, 154}

The expression sum(ns) / len(ns) calculates the average. sum(ns) adds the numbers in the set. len(ns) becomes the length of the set, or the divisor. 1355.0 / 8:

169.375

--

--

nick3499
nick3499

Written by nick3499

coder of JavaScript and Python

No responses yet