Một cách đơn giản là sử dụng hoán vị iter_tools
# If you are given a list
numList = [1,2,3,4,5,6,7]
# and you are asked to find the number of three sums that add to a particular number
target = 10
# How you could come up with the answer?
from itertools import permutations
good_permutations = []
for p in permutations[numList, 3]:
if sum[p] == target:
good_permutations.append[p]
print[good_permutations]
Kết quả là:
[[1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 5, 4], [1, 6, 3], [1, 7, 2], [2, 1, 7], [2, 3,
5], [2, 5, 3], [2, 7, 1], [3, 1, 6], [3, 2, 5], [3, 5, 2], [3, 6, 1], [4, 1, 5], [4,
5, 1], [5, 1, 4], [5, 2, 3], [5, 3, 2], [5, 4, 1], [6, 1, 3], [6, 3, 1], [7, 1, 2],
[7, 2, 1]]
Lưu ý rằng vấn đề thứ tự - có nghĩa là 1, 2, 7 cũng được hiển thị là 2, 1, 7 và 7, 1, 2. Bạn có thể giảm điều này bằng cách sử dụng một tập hợp.
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
Example:
Bàn luận [12, 15, 3, 10]
Output: 40Đưa ra một danh sách các số, hãy viết một chương trình Python để tìm tổng của tất cả các yếu tố trong danh sách. [17, 5, 3, 5]
Output: 30
Đầu vào: [12, 15, 3, 10] Đầu ra: 40
Python3
Đầu vào: [17, 5, 3, 5] Đầu ra: 30
Ví dụ #1: & nbsp;
total
=
0
Các
Sum of all elements in given list: 747
Sum of all elements in given list: 746
Sum of all elements in given list: 749
Sum of all elements in given list: 740
Sum of all elements in given list: 74
2 Sum of all elements in given list: 74
3Sum of all elements in given list: 74
4 Sum of all elements in given list: 74
5Sum of all elements in given list: 74
6_______
Sum of all elements in given list: 74
Sum of all elements in given list: 74
1total
=
total
Sum of all elements in given list: 74
5 Sum of all elements in given list: 74
6 Using while[] loop
Python3
Đầu vào: [17, 5, 3, 5] Đầu ra: 30
Ví dụ #1: & nbsp;
Ví dụ #1: & nbsp;
total
0total
1
Sum of all elements in given list: 749
Sum of all elements in given list: 740
Các
Sum of all elements in given list: 742
Sum of all elements in given list: 743
Sum of all elements in given list: 744
Sum of all elements in given list: 745
Sum of all elements in given list: 746_______
Sum of all elements in given list: 747
Sum of all elements in given list: 746
Sum of all elements in given list: 749
Sum of all elements in given list: 740
Sum of all elements in given list: 74
2 Sum of all elements in given list: 74
3Sum of all elements in given list: 74
4 Sum of all elements in given list: 74
5Sum of all elements in given list: 74
6_______
Sum of all elements in given list: 74
Sum of all elements in given list: 74
1total
=
total
Sum of all elements in given list: 74
5 Sum of all elements in given list: 74
6 Recursive way
Python3
Ví dụ #1: & nbsp;
total
=
0
Các
Sum of all elements in given list: 742
Sum of all elements in given list: 743
Sum of all elements in given list: 744
Sum of all elements in given list: 745
Sum of all elements in given list: 746_______
Sum of all elements in given list: 741
[[1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 5, 4], [1, 6, 3], [1, 7, 2], [2, 1, 7], [2, 3,
5], [2, 5, 3], [2, 7, 1], [3, 1, 6], [3, 2, 5], [3, 5, 2], [3, 6, 1], [4, 1, 5], [4,
5, 1], [5, 1, 4], [5, 2, 3], [5, 3, 2], [5, 4, 1], [6, 1, 3], [6, 3, 1], [7, 1, 2],
[7, 2, 1]]
07[[1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 5, 4], [1, 6, 3], [1, 7, 2], [2, 1, 7], [2, 3,
5], [2, 5, 3], [2, 7, 1], [3, 1, 6], [3, 2, 5], [3, 5, 2], [3, 6, 1], [4, 1, 5], [4,
5, 1], [5, 1, 4], [5, 2, 3], [5, 3, 2], [5, 4, 1], [6, 1, 3], [6, 3, 1], [7, 1, 2],
[7, 2, 1]]
08Sum of all elements in given list: 741
total
=
total
Sum of all elements in given list: 745
Sum of all elements in given list: 746
Đầu ra
Sum of all elements in given list: 747
Sum of all elements in given list: 746
Sum of all elements in given list: 749
Sum of all elements in given list: 740
Sum of all elements in given list: 74
2 Sum of all elements in given list: 74
3Sum of all elements in given list: 74
4 Sum of all elements in given list: 74
5Sum of all elements in given list: 74
6_______
Sum of all elements in given list: 74
Sum of all elements in given list: 74
1total
=
total
Sum of all elements in given list: 74
5 Sum of all elements in given list: 74
6 Using sum[] method
Python3
Ví dụ #1: & nbsp;
total
=
0
Sum of all elements in given list: 747
Sum of all elements in given list: 746
Sum of all elements in given list: 749
Sum of all elements in given list: 740
Sum of all elements in given list: 74
2 Sum of all elements in given list: 74
3Sum of all elements in given list: 74
4 Sum of all elements in given list: 74
5Sum of all elements in given list: 74
6_______
Sum of all elements in given list: 74
Sum of all elements in given list: 74
1total
=
total
Sum of all elements in given list: 74
5 Sum of all elements in given list: 74
6Using add[] function of operator module
Đầu ra
Python3
Ví dụ #2: Sử dụng while [] loop & nbsp; & nbsp;
Sum of all elements in given list: 743
=
0
Sum of all elements in given list: 741
Sum of all elements in given list: 743
Sum of all elements in given list: 745
=
=
4Ví dụ #3: Cách đệ quy & nbsp; & nbsp;
list1
2 list1
3list1
4list1
5
Sum of all elements in given list: 747
[[1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 5, 4], [1, 6, 3], [1, 7, 2], [2, 1, 7], [2, 3,
5], [2, 5, 3], [2, 7, 1], [3, 1, 6], [3, 2, 5], [3, 5, 2], [3, 6, 1], [4, 1, 5], [4,
5, 1], [5, 1, 4], [5, 2, 3], [5, 3, 2], [5, 4, 1], [6, 1, 3], [6, 3, 1], [7, 1, 2],
[7, 2, 1]]
80Sum of all elements in given list: 74
1list1
7 list1
8=
__ 0
02 Using enumerate function[[1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 5, 4], [1, 6, 3], [1, 7, 2], [2, 1, 7], [2, 3,
5], [2, 5, 3], [2, 7, 1], [3, 1, 6], [3, 2, 5], [3, 5, 2], [3, 6, 1], [4, 1, 5], [4,
5, 1], [5, 1, 4], [5, 2, 3], [5, 3, 2], [5, 4, 1], [6, 1, 3], [6, 3, 1], [7, 1, 2],
[7, 2, 1]]
Python3
[[1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 5, 4], [1, 6, 3], [1, 7, 2], [2, 1, 7], [2, 3,
5], [2, 5, 3], [2, 7, 1], [3, 1, 6], [3, 2, 5], [3, 5, 2], [3, 6, 1], [4, 1, 5], [4,
5, 1], [5, 1, 4], [5, 2, 3], [5, 3, 2], [5, 4, 1], [6, 1, 3], [6, 3, 1], [7, 1, 2],
[7, 2, 1]]
03[[1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 5, 4], [1, 6, 3], [1, 7, 2], [2, 1, 7], [2, 3,
5], [2, 5, 3], [2, 7, 1], [3, 1, 6], [3, 2, 5], [3, 5, 2], [3, 6, 1], [4, 1, 5], [4,
5, 1], [5, 1, 4], [5, 2, 3], [5, 3, 2], [5, 4, 1], [6, 1, 3], [6, 3, 1], [7, 1, 2],
[7, 2, 1]]
04 0
Is
[[1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 5, 4], [1, 6, 3], [1, 7, 2], [2, 1, 7], [2, 3,
5], [2, 5, 3], [2, 7, 1], [3, 1, 6], [3, 2, 5], [3, 5, 2], [3, 6, 1], [4, 1, 5], [4,
5, 1], [5, 1, 4], [5, 2, 3], [5, 3, 2], [5, 4, 1], [6, 1, 3], [6, 3, 1], [7, 1, 2],
[7, 2, 1]]
99Sum of all elements in given list: 7400
Sum of all elements in given list: 745
=
Sum of all elements in given list: 7403
Sum of all elements in given list: 747
Sum of all elements in given list: 7405
total
=
25[[1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 5, 4], [1, 6, 3], [1, 7, 2], [2, 1, 7], [2, 3,
5], [2, 5, 3], [2, 7, 1], [3, 1, 6], [3, 2, 5], [3, 5, 2], [3, 6, 1], [4, 1, 5], [4,
5, 1], [5, 1, 4], [5, 2, 3], [5, 3, 2], [5, 4, 1], [6, 1, 3], [6, 3, 1], [7, 1, 2],
[7, 2, 1]]
Sum of all elements in given list: 74
9
27 Using list comprehension [[1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 5, 4], [1, 6, 3], [1, 7, 2], [2, 1, 7], [2, 3,
5], [2, 5, 3], [2, 7, 1], [3, 1, 6], [3, 2, 5], [3, 5, 2], [3, 6, 1], [4, 1, 5], [4,
5, 1], [5, 1, 4], [5, 2, 3], [5, 3, 2], [5, 4, 1], [6, 1, 3], [6, 3, 1], [7, 1, 2],
[7, 2, 1]]
Python3
Sum of all elements in given list: 743
=
0
Sum of all elements in given list: 741
Sum of all elements in given list: 743
Sum of all elements in given list: 745
=
=
4Sum of all elements in given list: 747
Sum of all elements in given list: 746
[[1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 5, 4], [1, 6, 3], [1, 7, 2], [2, 1, 7], [2, 3,
5], [2, 5, 3], [2, 7, 1], [3, 1, 6], [3, 2, 5], [3, 5, 2], [3, 6, 1], [4, 1, 5], [4,
5, 1], [5, 1, 4], [5, 2, 3], [5, 3, 2], [5, 4, 1], [6, 1, 3], [6, 3, 1], [7, 1, 2],
[7, 2, 1]]
47Sum of all elements in given list: 7427
Ví dụ #3: Cách đệ quy & nbsp; & nbsp; Using lambda function
Python3
Sum of all elements in given list: 743
=
0
Sum of all elements in given list: 741
Sum of all elements in given list: 743
Sum of all elements in given list: 745
=
=
4Ví dụ #3: Cách đệ quy & nbsp; & nbsp;
Python3
list1
2 list1
3list1
4list1
5
Sum of all elements in given list: 741
list1
7 list1
8=
__ 0
[[1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 5, 4], [1, 6, 3], [1, 7, 2], [2, 1, 7], [2, 3,
5], [2, 5, 3], [2, 7, 1], [3, 1, 6], [3, 2, 5], [3, 5, 2], [3, 6, 1], [4, 1, 5], [4,
5, 1], [5, 1, 4], [5, 2, 3], [5, 3, 2], [5, 4, 1], [6, 1, 3], [6, 3, 1], [7, 1, 2],
[7, 2, 1]]
02Ví dụ #3: Cách đệ quy & nbsp; & nbsp;
[[1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 5, 4], [1, 6, 3], [1, 7, 2], [2, 1, 7], [2, 3,
5], [2, 5, 3], [2, 7, 1], [3, 1, 6], [3, 2, 5], [3, 5, 2], [3, 6, 1], [4, 1, 5], [4,
5, 1], [5, 1, 4], [5, 2, 3], [5, 3, 2], [5, 4, 1], [6, 1, 3], [6, 3, 1], [7, 1, 2],
[7, 2, 1]]
99Sum of all elements in given list: 7400
=
Sum of all elements in given list: 7400
Sum of all elements in given list: 745
Sum of all elements in given list: 7473
0
Sum of all elements in given list: 7475
Sum of all elements in given list: 747
Sum of all elements in given list: 7405