Sử dụng biểu thức máy phát:
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[num for num in myList if not num%2]
60
Sử dụng filter[]
:
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
Sử dụng vòng lặp thủ công:
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
Bài viết sau đây cho thấy cách đưa ra một danh sách số nguyên, chúng ta có thể tạo ra tổng của tất cả các chữ số lẻ và thậm chí của nó.
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.
Phương pháp 1: Sử dụng vòng lặp, str [] và int []Using loop, str[] and int[]
Trong đó, trước tiên chúng tôi chuyển đổi từng phần tử thành chuỗi và sau đó lặp lại cho từng phần tử của nó và thêm vào tổng hợp tương ứng bằng cách chuyển đổi sang số nguyên.
Python3
Các
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
3>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
4>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
5 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
7>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
8>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
9>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1 Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.1
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.2
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1 Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.1
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.5
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.6
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.7
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.8
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.9
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.5
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.1
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.7
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
7Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.4
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.5
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.6
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.7
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.8
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.9
The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 400
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 405
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.2
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1 Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.7
The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 400
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.5
The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 402
The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 404
The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 405
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
9>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1 Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.7
The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 400
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
3>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
4Odd digit sum : 36 Even digit sum : 402
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
7Odd digit sum : 36 Even digit sum : 405
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
3>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
4Odd digit sum : 36 Even digit sum : 408
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
7Odd digit sum : 36 Even digit sum : 401
Đầu ra
The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 40
Làm thế nào để bạn tìm thấy tổng số số chẵn trong một danh sách?Using loop and sum[]
Công thức để tìm tổng số số chẵn là n [n+1], trong đó n là số tự nhiên. Xác định giá trị của 'n' từ danh sách đã cho và thay thế giá trị của nó trong tổng công thức số chẵn.
Python3
Các
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
3>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
4>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
5 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
7>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
8>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
9>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1 Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.1
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.2
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1 Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.1
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.5
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.6
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.7
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.8
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.9
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.5
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.1
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.7
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
7Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.4
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.5
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.6
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.7
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.8
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.9
The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 400
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
3>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
4Odd digit sum : 36 Even digit sum : 402
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
7Odd digit sum : 36 Even digit sum : 405
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
3>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
4Odd digit sum : 36 Even digit sum : 408
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
7Odd digit sum : 36 Even digit sum : 401
Đầu ra
The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 40
Làm thế nào để bạn tìm thấy tổng số số chẵn trong một danh sách? Using list comprehension
Python3
Các
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
9>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1 Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.1
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.2
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1 Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.1
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.5
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.6
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.7
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.8
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.9
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.5
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.1
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.7
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
7Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.4
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.5
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.6
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.7
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.8
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.9
The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 400
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 405
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.2
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1 Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.7
The original list is : [345, 893, 1948, 34, 2346] Odd digit sum : 36 Even digit sum : 400
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
3>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
4Odd digit sum : 36 Even digit sum : 402
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
7Odd digit sum : 36 Even digit sum : 405
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
3>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
4Odd digit sum : 36 Even digit sum : 408
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
7Odd digit sum : 36 Even digit sum : 401
Đầu ra
Odd digit sum : 36 Even digit sum : 40
Phương pháp 4: Sử dụng hàm liệt kê Using the enumerate function
Python3
Các
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
9>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1 Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.1
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.2
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1 Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.1
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
9>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
15>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
16__>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
93Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.5
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.1
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.7
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.2
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
1 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
15>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> sum[filter[lambda x: not x%2, myList]]
60
16__>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
19Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.5
Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.1
Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.7
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
3>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
4Odd digit sum : 36 Even digit sum : 402
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
7Odd digit sum : 36 Even digit sum : 405
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
3>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
4Odd digit sum : 36 Even digit sum : 408
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
6 >>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for item in myList:
... if not item%2:
... result += item
...
>>> result
60
7Odd digit sum : 36 Even digit sum : 401
Đầu ra
Odd digit sum : 36 Even digit sum : 40