Cải thiện bài viết
Lưu bài viết
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.75Bà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 0sum = 46 average = 5.75 1sum = 46 average = 5.75 2sum = 46 average = 5.75 3sum = 46 average = 5.75 2sum = 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.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.751Input : [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.755Average of the list = 35.756Average of the list = 35.757Average of the list = 35.758
Average of the list = 35.755Average of the list = 35.756Average of the list = 35.751Average of the list = 35.752
Output:
sum = 46 average = 5.75Input : [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.754sum = 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.756= 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.758
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.751Average of the list = 35.752Average of the list = 35.753Average of the list = 35.754
Example:
Python3
Phương pháp 2: Sử dụng phương thức Sum ()
Phương thức SUM () Trả về tổng của danh sách được truyền dưới dạng đối số của nó. Sau đó, chúng tôi sẽ chia tổng cho phương thức Len () để 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.751Input : [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.755Average of the list = 35.756Average of the list = 35.757Average of the list = 35.758
Average of the list = 35.755Average of the list = 35.756Average of the list = 35.751Average of the list = 35.752
Output:
sum = 46 average = 5.75Điều kiện tiên quyết: hàm sum (), hàm len (), hàm tròn (), giảm (), lambda và mean (). Đưa ra một danh sách các số, nhiệm vụ là tìm trung bình của danh sách đó. Trung bình là tổng của các phần tử chia cho số lượng các phần tử.
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.75Sử dụng Sum ()
Trong Python, chúng ta có thể tìm thấy trung bình của một danh sách bằng cách sử dụng hàm sum () và len ().sum() and len() function.
- SUM (): Sử dụng hàm SUM () Chúng ta có thể nhận được tổng của danh sách. : Using sum() function we can get the sum of the list.
- hàm Len (): Len () được sử dụng để có độ dài hoặc số lượng phần tử trong danh sách. : 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.754sum = 46 average = 5.75 18= sum = 46 average = 5.75 9
=9= ____1010____1022212sum = 46 average = 5.75 9__12
sum = 46 average = 5.75 18= sum = 46 average = 5.75 20
Average of the list = 35.755Average of the list = 35.756sum = 46 average = 5.75 23sum = 46 average = 5.75 2sum = 46 average = 5.75 25sum = 46 average = 5.75 26sum = 46 average = 5.75 7sum = 46 average = 5.75 28
Output:
Average of the list = 35.75Đầu ra
Điều kiện tiên quyết: hàm sum (), hàm len (), hàm tròn (), giảm (), lambda và mean (). Đưa ra một danh sách các số, nhiệm vụ là tìm trung bình của danh sách đó. Trung bình là tổng của các phần tử chia cho số lượng các phần tử.
Python3
Sử dụng Sum ()
=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.754sum = 46 average = 5.75 18= sum = 46 average = 5.75 9
=9= ____1010____1022212sum = 46 average = 5.75 9__12
sum = 46 average = 5.75 18= sum = 46 average = 5.75 20
Average of the list = 35.755Average of the list = 35.756sum = 46 average = 5.75 23sum = 46 average = 5.75 2sum = 46 average = 5.75 25sum = 46 average = 5.75 26sum = 46 average = 5.75 7sum = 46 average = 5.75 28
Output:
Average of the list = 35.75Đầu ra
Điều kiện tiên quyết: hàm sum (), hàm len (), hàm tròn (), giảm (), lambda và mean (). Đưa ra một danh sách các số, nhiệm vụ là tìm trung bình của danh sách đó. Trung bình là tổng của các phần tử chia cho số lượng các phần tử.
Python3
Sử dụng Sum ()
=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.754sum = 46 average = 5.75 18= sum = 46 average = 5.75 9
=9= ____1010____1022212sum = 46 average = 5.75 9__12
sum = 46 average = 5.75 18= sum = 46 average = 5.75 20
Average of the list = 35.755Average of the list = 35.756sum = 46 average = 5.75 23sum = 46 average = 5.75 2sum = 46 average = 5.75 25sum = 46 average = 5.75 26sum = 46 average = 5.75 7sum = 46 average = 5.75 28
Output:
Average of the list = 35.75Bằ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.754sum = 46 average = 5.75 18= 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.754Input : [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.751Input : [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 sum = 46 average = 5.75 25Average of the list = 35.756Average of the list = 35.753sum = 46 average = 5.75 28
sum = 46 average = 5.75 29sum = 46 average = 5.75 18Input : [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.75 33
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.754sum = 46 average = 5.75 18= sum = 46 average = 5.75 37Average of the list = 35.752Average of the list = 35.753=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.754=3 sum = 46 average = 5.75 43
=9= ____1010____1022212sum = 46 average = 5.75 9__12
sum = 46 average = 5.75 18= sum = 46 average = 5.75 20
Average of the list = 35.755Average of the list = 35.756sum = 46 average = 5.75 23sum = 46 average = 5.75 2sum = 46 average = 5.75 25sum = 46 average = 5.75 26sum = 46 average = 5.75 7sum = 46 average = 5.75 28
Đầu ra
Average of the list = 35.75