Về cơ bản, tôi cần trợ giúp trong việc tạo ra các số từ một danh sách mà tôi đã tạo trong Python:
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, ...]
Tôi đã thử một vài phương pháp khác nhau, nhưng mỗi lần tôi in, có những con số lẻ được trộn lẫn với Evens!
Tôi biết làm thế nào để tạo số chẵn/số lẻ nếu tôi thực hiện phạm vi 0-100, tuy nhiên, chỉ nhận được các số chẵn từ danh sách được đề cập trước đó đã khiến tôi bối rối!
P.S. Tôi chỉ mới sử dụng Python trong một vài ngày, nếu điều này trở nên cực kỳ đơn giản, cảm ơn trước!
Chỉnh sửa: Cảm ơn tất cả các câu trả lời, với sự giúp đỡ của bạn, tôi đã vượt qua vấn đề nhỏ này. Dưới đây là những gì tôi đã kết thúc để hoàn thành một chút tập thể dục yêu cầu tổng hợp các số lượng chẵn của chuỗi Fibonacci:
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]39
Input: list2 = [12, 14, 95, 3] Output: [12, 14]0
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]41
Input: list2 = [12, 14, 95, 3] Output: [12, 14]2
Input: list2 = [12, 14, 95, 3] Output: [12, 14]3
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]76
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1____36Input: list2 = [12, 14, 95, 3] Output: [12, 14]7
Example:
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]
Input: list2 = [12, 14, 95, 3] Output: [12, 14]
Không gian phụ trợ: O [1]Using for loop
Phương pháp: Sử dụng Numpy.Array
Python3
Is
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
01F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1 F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
03Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]5
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]6
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]7
Phương pháp 4: Sử dụng biểu thức Lambda & NBSP;
Input: list2 = [12, 14, 95, 3] Output: [12, 14]8
Input: list2 = [12, 14, 95, 3] Output: [12, 14]9
10, 4, 660
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
110, 4, 662
10, 4, 663
Output:
10, 4, 66
Phương pháp 2: Sử dụng trong khi Loop & NBSP;Using while loop
Python3
Các
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]6
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1 Input: list2 = [12, 14, 95, 3] Output: [12, 14]6
Even numbers in the list: [10, 4, 66]2
Even numbers in the list: [10, 4, 66]3
Even numbers in the list: [10, 4, 66]4
Even numbers in the list: [10, 4, 66]5
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]9
Input: list2 = [12, 14, 95, 3] Output: [12, 14]0
Even numbers in the list: [10, 4, 66]8
Input: list2 = [12, 14, 95, 3] Output: [12, 14]2
Input: list2 = [12, 14, 95, 3] Output: [12, 14]3
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1 Input: list2 = [12, 14, 95, 3] Output: [12, 14]6
Input: list2 = [12, 14, 95, 3] Output: [12, 14]7
Input: list2 = [12, 14, 95, 3] Output: [12, 14]8
Input: list2 = [12, 14, 95, 3] Output: [12, 14]9
Even numbers in the list: [10, 4, 66]7
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
110, 4, 662
10, 4, 663
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]9
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]6
Even numbers in the list: 10 4 663
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1 Even numbers in the list: 10 4 665
Output:
10, 4, 66
Phương pháp 3: Sử dụng danh sách hiểu & nbsp;Using list comprehension
Python3
Is
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
01F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1 F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
03Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]5
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]6
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]7
Input: list2 = [12, 14, 95, 3] Output: [12, 14]9
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
17F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
18F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
19
Output:
Even numbers in the list: [10, 4, 66]
Phương pháp 4: Sử dụng biểu thức Lambda & NBSP;Using lambda expressions
Python3
Các
Is
Input: list2 = [12, 14, 95, 3] Output: [12, 14]9
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
17F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
18F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
19Đầu ra
Even numbers in the list: [10, 4, 66]
Phương pháp 5: Sử dụng đệ quyUsing Recursion
Python3
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
55 F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
56F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
39F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
58F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1Input: list2 = [12, 14, 95, 3] Output: [12, 14]6
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
61Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]9
Input: list2 = [12, 14, 95, 3] Output: [12, 14]0
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
64___Input: list2 = [12, 14, 95, 3] Output: [12, 14]8
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
72Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]9
Input: list2 = [12, 14, 95, 3] Output: [12, 14]0
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
39F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
76Input: list2 = [12, 14, 95, 3] Output: [12, 14]2
Input: list2 = [12, 14, 95, 3] Output: [12, 14]3
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1____11Input: list2 = [12, 14, 95, 3] Output: [12, 14]6
Input: list2 = [12, 14, 95, 3] Output: [12, 14]7
Input: list2 = [12, 14, 95, 3] Output: [12, 14]8
Input: list2 = [12, 14, 95, 3] Output: [12, 14]9
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
17F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
39F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
87F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
110, 4, 662
10, 4, 663
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]9
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
56F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
39F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
58Even numbers in the list: 10 4 663
Even numbers in the list: 10 4 665
10, 4, 663
Is
Input: list2 = [12, 14, 95, 3] Output: [12, 14]9
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
17Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]15
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]16
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
110, 4, 662
10, 4, 663
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]20
Đầu ra
Even numbers in the list: 10 4 66
Phương pháp 5: Sử dụng đệ quy
Python3
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
55 F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
56F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
39F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
58F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1Input: list2 = [12, 14, 95, 3] Output: [12, 14]6
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
61Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]9
Input: list2 = [12, 14, 95, 3] Output: [12, 14]0
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
64___Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]9
Input: list2 = [12, 14, 95, 3] Output: [12, 14]0
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
39F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
76Input: list2 = [12, 14, 95, 3] Output: [12, 14]2
Input: list2 = [12, 14, 95, 3] Output: [12, 14]3
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1____11Input: list2 = [12, 14, 95, 3] Output: [12, 14]6
Input: list2 = [12, 14, 95, 3] Output: [12, 14]7
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]9
Input: list2 = [12, 14, 95, 3] Output: [12, 14]9
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]50
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
110, 4, 662
10, 4, 663
Phương pháp: Sử dụng chức năng liệt kê & nbsp;
Python3
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
55 F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
56F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
39F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
58F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1Input: list2 = [12, 14, 95, 3] Output: [12, 14]6
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
61Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]9
Input: list2 = [12, 14, 95, 3] Output: [12, 14]0
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
64___Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]9
Input: list2 = [12, 14, 95, 3] Output: [12, 14]0
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
39F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
76Input: list2 = [12, 14, 95, 3] Output: [12, 14]2
Input: list2 = [12, 14, 95, 3] Output: [12, 14]3
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1____11Input: list2 = [12, 14, 95, 3] Output: [12, 14]6
Input: list2 = [12, 14, 95, 3] Output: [12, 14]7
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]9
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]81
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]39
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]83
Input: list2 = [12, 14, 95, 3] Output: [12, 14]7
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]9
Input: list2 = [12, 14, 95, 3] Output: [12, 14]9
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]50
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
110, 4, 662
10, 4, 663
Phương pháp: Sử dụng chức năng liệt kê & nbsp;
Is
Python3
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]5
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]35
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]7
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]37
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]38
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]39
Input: list2 = [12, 14, 95, 3] Output: [12, 14]0
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]41
Input: list2 = [12, 14, 95, 3] Output: [12, 14]2
Input: list2 = [12, 14, 95, 3] Output: [12, 14]3
F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1F = [1, 2]
while F[-1] < 4000000
F.append[F[-1] + F[-2]]
sum[F[1::3]]
4613732
1____36Input: list2 = [12, 14, 95, 3] Output: [12, 14]7
Phương pháp: Sử dụng Pass & NBSP;
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]5
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]68
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]7
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14]8
Input: list2 = [12, 14, 95, 3] Output: [12, 14]9
Input: list2 = [12, 14, 95, 3] Output: [12, 14]19
Output:
[ 2 64 14]