Содержание
- 2. Finding the Mean The mean is a common and intuitive way to summarize a set of
- 3. >>> shortlist = [1, 2, 3] >>> sum(shortlist) 6 We can use the len() function to
- 4. When we use the len() function on the list, it returns 3 because there are three
- 5. ''' Calculating the mean ''' def calculate_mean(numbers): u s = sum(numbers) v N = len(numbers) #
- 6. First, we define a function, calculate_mean(), that accepts the argument numbers, which is a list of
- 7. The calculate_mean() function will calculate the sum and length of any list, so we can reuse
- 8. Finding the Median The median of a collection of numbers is another kind of average. To
- 9. Now assume—just for this example—that we have another donation total for the 13th day so that
- 10. >>> samplelist = [4, 1, 3] >>> samplelist.sort() >>> samplelist [1, 3, 4]
- 11. Now we can write our next program, which finds the median of a list of numbers:
- 12. ''' Calculating the median ''' def calculate_median(numbers): u N = len(numbers) v numbers.sort() # Find the
- 13. The overall structure of the program is similar to that of the earlier program that calculates
- 14. We cannot use a floating point number as an index in a list, so we use
- 15. Finding the Most Common Elements Finding the most common number in a data set can be
- 16. Here, we start off with a list of five numbers and import Counter from the collections
- 17. >>> c.most_common(1) [(4, 2)] If you call the method again with 2 as an argument, you’ll
- 18. The most_common() method returns both the numbers and the number of times they occur. What if
- 19. Finding the Mode We’re ready to write a program that finds the mode for a list
- 20. The rest of the program applies the calculate_mode function to the list of test scores we
- 21. What if you have a set of data where two or more numbers occur the same
- 22. ''' Calculating the mode when the list of numbers may have multiple modes ''' from collections
- 23. When you execute the preceding program, you should see the following output: The mode(s) of the
- 24. Creating a Frequency Table Let’s consider the list of test scores again: 7, 8, 9, 2,
- 25. Here’s the program: ''' Frequency table for a list of numbers ''' from collections import Counter
- 26. The function frequency_table() prints the frequency table of the list of numbers passed to it. At
- 27. Number Frequency 9 5 6 3 1 2 5 2 7 2 8 2 10 2
- 28. Here, you can see that the numbers are listed in decreasing order of frequency because the
- 29. ''' Frequency table for a list of numbers Enhanced to display the table sorted by the
- 30. Here, we store the list returned by the most_common() method in numbers_freq at u, and then
- 31. Number Frequency 1 2 2 1 4 1 5 2 6 3 7 2 8 2
- 32. Measuring the Dispersion The next statistical calculations we’ll look at measure the dispersion, which tells us
- 33. Finding the Range of a Set of Numbers Once again, consider the list of donations during
- 34. The next program finds the range of the preceding list of donations: ''' Find the range
- 36. Скачать презентацию