Hướng dẫn how do you sum an even number in a list in python? - làm thế nào để bạn tính tổng một số chẵn trong một danh sách trong python?

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
7
Input : 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 : 40
0
>>> 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

The original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40
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.
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 : 40
0

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 : 40
2
The original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40
4

The original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40
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
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 : 40
0

>>> 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
Odd digit sum : 36
Even digit sum : 40
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]
>>> result = 0
>>> for item in myList:
...     if not item%2:
...             result += item
...
>>> result
60
7
Odd digit sum : 36
Even digit sum : 40
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
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
Odd digit sum : 36
Even digit sum : 40
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
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
Odd digit sum : 36
Even digit sum : 40
1

Đầ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
7
Input : 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 : 40
0
>>> 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
4
Odd digit sum : 36
Even digit sum : 40
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]
>>> result = 0
>>> for item in myList:
...     if not item%2:
...             result += item
...
>>> result
60
7
Odd digit sum : 36
Even digit sum : 40
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
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
Odd digit sum : 36
Even digit sum : 40
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
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
Odd digit sum : 36
Even digit sum : 40
1

Đầ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
7
Input : 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 : 40
0
>>> 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

The original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40
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.
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 : 40
0

>>> 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
Odd digit sum : 36
Even digit sum : 40
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]
>>> result = 0
>>> for item in myList:
...     if not item%2:
...             result += item
...
>>> result
60
7
Odd digit sum : 36
Even digit sum : 40
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
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
Odd digit sum : 36
Even digit sum : 40
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
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
Odd digit sum : 36
Even digit sum : 40
1

Đầ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
93
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

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
19
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
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
Odd digit sum : 36
Even digit sum : 40
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]
>>> result = 0
>>> for item in myList:
...     if not item%2:
...             result += item
...
>>> result
60
7
Odd digit sum : 36
Even digit sum : 40
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
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
Odd digit sum : 36
Even digit sum : 40
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
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
Odd digit sum : 36
Even digit sum : 40
1

Đầu ra

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?

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.n(n+1), where n is the natural number. Determine the value of 'n' from the given list and substitute its value in the sum of even numbers formula.

Làm thế nào để bạn in tổng số số lẻ trong danh sách trong Python?

Khoa học dữ liệu thực tế bằng cách sử dụng Python, chúng ta phải tìm tổng của tất cả các yếu tố kỳ lạ từ danh sách.Vì vậy, nếu đầu vào giống như nums = [5,7,6,4,6,9,3,6,2], thì đầu ra sẽ là 24 vì 5+7+9+3 = 24. Trả lại tổng củacác phần tử trong l bằng cách chuyển l vào hàm sum ().return the sum of elements in l by passing l into sum() function.

Làm thế nào để bạn chọn số thậm chí trong một danh sách trong Python?

Để lọc các số thậm chí từ danh sách trong Python, hãy sử dụng hàm tích hợp Filter ().Vượt qua hàm trả về đúng cho một số chẵn và danh sách các số, làm đối số cho hàm lọc ().use filter() builtin function. Pass the function that returns True for an even number, and the list of numbers, as arguments to filter() function.

Làm thế nào để bạn thêm một số chẵn vào một mảng trong Python?

Khoa học dữ liệu thực tế sử dụng Python..
Chỉ số: = I [1].
val: = i [0].
Nếu A [INDEX] là chẵn, thì SUM: = SUM - A [INDEX].
A [INDEX]: = A [INDEX] + VAL ..
Nếu A [INDEX] là chẵn, thì sum: = sum + a [index].
Sum được thêm vào res ..