Xem thảo luận
Cải thiện bài viết
Lưu bài viết
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.750
sum = 46 average = 5.751
sum = 46 average = 5.752
sum = 46 average = 5.753
sum = 46 average = 5.752
sum = 46 average = 5.755__12
sum = 46 average = 5.757
=
sum = 46 average = 5.759
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.750
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.751
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.752
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.753
Average of the list = 35.755
Average of the list = 35.756
Average of the list = 35.757
Average of the list = 35.758
Average of the list = 35.755
Average of the list = 35.756
Average of the list = 35.751
Average of the list = 35.752
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
4sum = 46
average = 5.75
7Input : [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.759
=
Average of the list = 35.751
Average of the list = 35.752
Average of the list = 35.753
Average of the list = 35.754
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.750
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.751
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.752
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.753
Average of the list = 35.755
Average of the list = 35.756
Average of the list = 35.757
Average of the list = 35.758
Average of the list = 35.755
Average of the list = 35.756
Average of the list = 35.751
Average of the list = 35.752
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.754
=
3 sum = 46 average = 5.7584
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
4sum = 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
4Input : [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
0Input : [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
1Input : [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
2sum = 46 average = 5.75
25Average of the list = 35.75
6Average of the list = 35.75
3sum = 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.754
=
3 Average of the list = 35.754
=
5Average of the list = 35.752
Average of the list = 35.753
=
8=
9=
____1010____1022212
sum = 46 average = 5.759__12
sum = 46 average = 5.7518
=
sum = 46 average = 5.7520
Average of the list = 35.755
Average of the list = 35.756
sum = 46 average = 5.7523
sum = 46 average = 5.752
sum = 46 average = 5.7525
sum = 46 average = 5.7526
sum = 46 average = 5.757
sum = 46 average = 5.7528
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.7529
sum = 46 average = 5.7530
sum = 46 average = 5.7531
sum = 46 average = 5.7532
=
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.754
=
3 sum = 46 average = 5.7532
Average of the list = 35.756
sum = 46 average = 5.7539
sum = 46 average = 5.7540
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.756
sum = 46 average = 5.7542__
=
9=
____1010____1022212
sum = 46 average = 5.759__12
sum = 46 average = 5.7518
=
sum = 46 average = 5.7520
Average of the list = 35.755
Average of the list = 35.756
sum = 46 average = 5.7523
sum = 46 average = 5.752
sum = 46 average = 5.7525
sum = 46 average = 5.7526
sum = 46 average = 5.757
sum = 46 average = 5.7528
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.7529
sum = 46 average = 5.7577
sum = 46 average = 5.7531
sum = 46 average = 5.7579
=
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.754
=
3 sum = 46 average = 5.7584
=
9=
____1010____1022212
sum = 46 average = 5.759__12
sum = 46 average = 5.7518
=
sum = 46 average = 5.7520
Average of the list = 35.755
Average of the list = 35.756
sum = 46 average = 5.7523
sum = 46 average = 5.752
sum = 46 average = 5.7525
sum = 46 average = 5.7526
sum = 46 average = 5.757
sum = 46 average = 5.7528
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.754
=
3 sum = 46 average = 5.7584
=
9=
____1010____1022212
sum = 46 average = 5.759__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.754
sum = 46 average = 5.7518
=
sum = 46 average = 5.759
=
9=
____1010____1022212
sum = 46 average = 5.759__12
sum = 46 average = 5.7518
=
sum = 46 average = 5.7520
Average of the list = 35.755
Average of the list = 35.756
sum = 46 average = 5.7523
sum = 46 average = 5.752
sum = 46 average = 5.7525
sum = 46 average = 5.7526
sum = 46 average = 5.757
sum = 46 average = 5.7528
Bằng danh sách lặp lại
Average of the list = 35.75