Hướng dẫn how do you find the average and total in python? - làm thế nào để bạn tìm thấy trung bình và tổng số trong python?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Examples:

    Input: [4, 5, 1, 2, 9, 7, 10, 8]
    Output:
    sum =  46
    average =  5.75
    
    Input: [15, 9, 55, 41, 35, 20, 62, 49]
    Output:
    sum =  286
    average =  35.75
    

    Bàn luận

    Đưa ra một danh sách. Nhiệm vụ là tìm tổng và trung bình của danh sách. Trung bình của danh sách được định nghĩa là tổng của các phần tử chia cho số lượng các phần tử.

    Example:

    Python3

    Phương pháp 1: Phương pháp ngây thơ

    Trong phương pháp này, chúng tôi sẽ lặp lại trong danh sách và sẽ thêm từng phần tử vào một số biến lưu trữ tổng của phần tử ITH và sau đó chia tổng với tổng số biến để tìm mức trung bình.

    L =

    sum =  46
    average =  5.75
    
    0
    sum =  46
    average =  5.75
    
    1
    sum =  46
    average =  5.75
    
    2
    sum =  46
    average =  5.75
    
    3
    sum =  46
    average =  5.75
    
    2
    sum =  46
    average =  5.75
    
    5__12

    sum =  46
    average =  5.75
    
    7=
    sum =  46
    average =  5.75
    
    9

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    0
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    1
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    2
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    3

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    Average of the list = 35.75
    7
    Average of the list = 35.75
    8

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    Average of the list = 35.75
    1
    Average of the list = 35.75
    2

    Output:

    sum =  46
    average =  5.75
    

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4
    sum =  46
    average =  5.75
    
    7
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    6=
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    8

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    9=
    Average of the list = 35.75
    1
    Average of the list = 35.75
    2
    Average of the list = 35.75
    3
    Average of the list = 35.75
    4

    Example:

    Python3

    Phương pháp 1: Phương pháp ngây thơ

    Trong phương pháp này, chúng tôi sẽ lặp lại trong danh sách và sẽ thêm từng phần tử vào một số biến lưu trữ tổng của phần tử ITH và sau đó chia tổng với tổng số biến để tìm mức trung bình.

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    0
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    1
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    2
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    3

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    Average of the list = 35.75
    7
    Average of the list = 35.75
    8

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    Average of the list = 35.75
    1
    Average of the list = 35.75
    2

    Output:

    sum =  46
    average =  5.75
    

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4=3
    sum =  46
    average =  5.75
    
    84

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75

    Bằng danh sách lặp lại

    Lặp lại danh sách bằng cách sử dụng cho vòng lặp và thực hiện các hoạt động trên từng yếu tố của danh sách.sum[] and len[] function.

    • Input : [4, 5, 1, 2, 9, 7, 10, 8]
      Output : Average of the list = 5.75
      Explanation:
      Sum of the elements is 4+5+1+2+9+7+10+8 = 46
      and total number of elements is 8.
      So average is 46 / 8 = 5.75
      
      Input : [15, 9, 55, 41, 35, 20, 62, 49]
      Output : Average of the list = 35.75
      Explanation:
      Sum of the elements is 15+9+55+41+35+20+62+49 = 286
      and total number of elements is 8.
      So average is 46 / 8 = 35.75
      4
      sum =  46
      average =  5.75
      
      18=
      sum =  46
      average =  5.75
      
      9
      : Using sum[] function we can get the sum of the list.
    • Input : [4, 5, 1, 2, 9, 7, 10, 8]
      Output : Average of the list = 5.75
      Explanation:
      Sum of the elements is 4+5+1+2+9+7+10+8 = 46
      and total number of elements is 8.
      So average is 46 / 8 = 5.75
      
      Input : [15, 9, 55, 41, 35, 20, 62, 49]
      Output : Average of the list = 35.75
      Explanation:
      Sum of the elements is 15+9+55+41+35+20+62+49 = 286
      and total number of elements is 8.
      So average is 46 / 8 = 35.75
      4
      Input : [4, 5, 1, 2, 9, 7, 10, 8]
      Output : Average of the list = 5.75
      Explanation:
      Sum of the elements is 4+5+1+2+9+7+10+8 = 46
      and total number of elements is 8.
      So average is 46 / 8 = 5.75
      
      Input : [15, 9, 55, 41, 35, 20, 62, 49]
      Output : Average of the list = 35.75
      Explanation:
      Sum of the elements is 15+9+55+41+35+20+62+49 = 286
      and total number of elements is 8.
      So average is 46 / 8 = 35.75
      0
      Input : [4, 5, 1, 2, 9, 7, 10, 8]
      Output : Average of the list = 5.75
      Explanation:
      Sum of the elements is 4+5+1+2+9+7+10+8 = 46
      and total number of elements is 8.
      So average is 46 / 8 = 5.75
      
      Input : [15, 9, 55, 41, 35, 20, 62, 49]
      Output : Average of the list = 35.75
      Explanation:
      Sum of the elements is 15+9+55+41+35+20+62+49 = 286
      and total number of elements is 8.
      So average is 46 / 8 = 35.75
      1
      Input : [4, 5, 1, 2, 9, 7, 10, 8]
      Output : Average of the list = 5.75
      Explanation:
      Sum of the elements is 4+5+1+2+9+7+10+8 = 46
      and total number of elements is 8.
      So average is 46 / 8 = 5.75
      
      Input : [15, 9, 55, 41, 35, 20, 62, 49]
      Output : Average of the list = 35.75
      Explanation:
      Sum of the elements is 15+9+55+41+35+20+62+49 = 286
      and total number of elements is 8.
      So average is 46 / 8 = 35.75
      2
      sum =  46
      average =  5.75
      
      25
      Average of the list = 35.75
      6
      Average of the list = 35.75
      3
      sum =  46
      average =  5.75
      
      28
      : len[] function is used to get the length or the number of elements in a list.

    Python3

    =0 =1

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4=3
    Average of the list = 35.75
    4=5
    Average of the list = 35.75
    2
    Average of the list = 35.75
    3=8

    =9= ____1010____1022212

    sum =  46
    average =  5.75
    
    9__12

    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    20

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    sum =  46
    average =  5.75
    
    23
    sum =  46
    average =  5.75
    
    2
    sum =  46
    average =  5.75
    
    25
    sum =  46
    average =  5.75
    
    26
    sum =  46
    average =  5.75
    
    7
    sum =  46
    average =  5.75
    
    28

    Output:

    Average of the list = 35.75

    Sử dụng giảm [] và lambda

    Chúng ta có thể sử dụng giảm [] để giảm vòng lặp và bằng cách sử dụng hàm Lambda có thể tính toán tổng hợp danh sách. Chúng tôi sử dụng Len [] để tính độ dài như đã thảo luận ở trên. & NBSP;

    Python3

    sum =  46
    average =  5.75
    
    29
    sum =  46
    average =  5.75
    
    30
    sum =  46
    average =  5.75
    
    31
    sum =  46
    average =  5.75
    
    32

    =0 =1

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4=3
    sum =  46
    average =  5.75
    
    32
    Average of the list = 35.75
    6
    sum =  46
    average =  5.75
    
    39
    sum =  46
    average =  5.75
    
    40
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    6
    sum =  46
    average =  5.75
    
    42__

    =9= ____1010____1022212

    sum =  46
    average =  5.75
    
    9__12

    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    20

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    sum =  46
    average =  5.75
    
    23
    sum =  46
    average =  5.75
    
    2
    sum =  46
    average =  5.75
    
    25
    sum =  46
    average =  5.75
    
    26
    sum =  46
    average =  5.75
    
    7
    sum =  46
    average =  5.75
    
    28

    Output:

    Average of the list = 35.75

    Sử dụng trung bình []

    Hàm trung bình [] hàm sẵn có thể được sử dụng để tính giá trị trung bình [trung bình] của danh sách. & Nbsp;

    Python3

    sum =  46
    average =  5.75
    
    29
    sum =  46
    average =  5.75
    
    77
    sum =  46
    average =  5.75
    
    31
    sum =  46
    average =  5.75
    
    79

    =0 =1

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4=3
    sum =  46
    average =  5.75
    
    84

    =9= ____1010____1022212

    sum =  46
    average =  5.75
    
    9__12

    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    20

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    sum =  46
    average =  5.75
    
    23
    sum =  46
    average =  5.75
    
    2
    sum =  46
    average =  5.75
    
    25
    sum =  46
    average =  5.75
    
    26
    sum =  46
    average =  5.75
    
    7
    sum =  46
    average =  5.75
    
    28

    Output:

    Average of the list = 35.75

    Bằng danh sách lặp lại

    Lặp lại danh sách bằng cách sử dụng cho vòng lặp và thực hiện các hoạt động trên từng yếu tố của danh sách.

    Python3

    =0 =1

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4=3
    sum =  46
    average =  5.75
    
    84

    =9= ____1010____1022212

    sum =  46
    average =  5.75
    
    9__12

    Bằng danh sách lặp lại

    Lặp lại danh sách bằng cách sử dụng cho vòng lặp và thực hiện các hoạt động trên từng yếu tố của danh sách.

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4
    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    9

    =9= ____1010____1022212

    sum =  46
    average =  5.75
    
    9__12

    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    20

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    sum =  46
    average =  5.75
    
    23
    sum =  46
    average =  5.75
    
    2
    sum =  46
    average =  5.75
    
    25
    sum =  46
    average =  5.75
    
    26
    sum =  46
    average =  5.75
    
    7
    sum =  46
    average =  5.75
    
    28

    Bằng danh sách lặp lại

    Average of the list = 35.75


    Làm thế nào để bạn tìm thấy mức trung bình trong Python?

    Trung bình Python trung bình trong Python thường được tính bằng cách thêm tất cả các số trong một danh sách và sau đó chia nó cho số lượng các phần tử trong danh sách này.adding all the numbers in a list and then dividing it by the number of elements in this list.

    Làm thế nào để bạn tìm thấy tổng trung bình?

    Trung bình Đây là giá trị trung bình số học và được tính toán bằng cách thêm một nhóm số và sau đó chia cho số lượng của các số đó.Ví dụ, trung bình của 2, 3, 3, 5, 7 và 10 là 30 chia cho 6, là 5.adding a group of numbers and then dividing by the count of those numbers. For example, the average of 2, 3, 3, 5, 7, and 10 is 30 divided by 6, which is 5.

    Có tổng số chức năng trong Python không?

    Python cung cấp một tổng số hàm sẵn [] tổng hợp các số trong danh sách.Cú pháp: Sum [có thể lặp lại, bắt đầu] có thể sử dụng được: Có thể là bất cứ thứ gì danh sách, bộ dữ liệu hoặc từ điển, nhưng quan trọng nhất là nó phải là số. which sums up the numbers in the list. Syntax: sum[iterable, start] iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers.

    Bài Viết Liên Quan

    Chủ Đề