Tôi có thể nhận được phần tử thứ hai đến cuối cùng của danh sách với các mục sau:
>>> lst = ['a', 'b', 'c', 'd', 'e', 'f']
>>> print[lst[len[lst]-2]]
e
Có cách nào tốt hơn là sử dụng print[lst[len[lst]-2]]
để đạt được kết quả tương tự không?
- Python
- danh sách
- lập chỉ mục
Đã hỏi ngày 3 tháng 10 năm 2016 lúc 19:19Oct 3, 2016 at 19:19
PYNOOBPYNOOBPyNoob
1.1861 Huy hiệu vàng8 Huy hiệu bạc23 Huy hiệu đồng1 gold badge8 silver badges23 bronze badges
2
Thử ________ 7 ...
Ngày 3 tháng 10 năm 2016 lúc 19:21
Và vâng, nó là một bản sao; Lưu ý cách câu trả lời hàng đầu/được chấp nhận đưa ra một ví dụ cho phần tử thứ hai đến cuối cùng.
Ngày 3 tháng 10 năm 2016 lúc 19:26
1 câu trả lời
Có: Chỉ số tiêu cực:
lst[-2]
Đã trả lời ngày 3 tháng 10 năm 2016 lúc 19:21Oct 3, 2016 at 19:21
SCOTT HUNTSCOTT HUNTERScott Hunter
47.7K11 Huy hiệu vàng56 Huy hiệu bạc97 Huy hiệu đồng11 gold badges56 silver badges97 bronze badges
1
Ngay cả với kiến thức về
-1
, tôi không nghĩ sẽ trừ đi nhiều hơn ... Cảm ơn bạn! Tôi sẽ chấp nhận khi giới hạn thời gian kết thúc.Ngày 3 tháng 10 năm 2016 lúc 19:22
Đôi khi, có thể cần phải có phạm vi giữa một số nằm trong danh sách, cho các ứng dụng đó chúng tôi yêu cầu để có được yếu tố đầu tiên và cuối cùng của danh sách. Hãy để thảo luận về các cách nhất định để có được yếu tố đầu tiên và cuối cùng của danh sách. & NBSP;
Phương pháp số 1: Sử dụng chỉ mục danh sách
Sử dụng các chỉ số danh sách bên trong danh sách chính có thể thực hiện nhiệm vụ cụ thể này. Đây là phương pháp ngây thơ nhất để đạt được nhiệm vụ cụ thể này mà người ta có thể nghĩ đến. & NBSP;
Python3
test_list
lst[-2]
0 lst[-2]
1lst[-2]
2lst[-2]
3lst[-2]
4lst[-2]
3lst[-2]
6lst[-2]
3lst[-2]
8lst[-2]
3__The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]2
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]4
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]7
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]8
lst[-2]
0 The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]0
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]1
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]2
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3__12
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]2
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]8
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]1
Output:
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]
Phương pháp số 4: Sử dụng MAP và __GetItem__ Phương thức & NBSP;
Với các phương pháp này, về cơ bản chúng tôi đang truy cập phần tử tại một vị trí nhất định từ danh sách. Chúng tôi khớp một danh sách chứa một phần tử và một danh sách chứa một chỉ mục phần tử mà chúng tôi muốn và ánh xạ hai mảng và sử dụng __getItem__ để lấy phần tử từ chỉ mục được lưu trữ trong danh sách chỉ mục.
Python3
test_list
lst[-2]
0 lst[-2]
1lst[-2]
2lst[-2]
3lst[-2]
4lst[-2]
3lst[-2]
6lst[-2]
3lst[-2]
8lst[-2]
3__The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]2
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]4
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]7
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]8
lst[-2]
0 The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]0
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]1
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]2
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3__12
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]2
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]8
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]1
Output:
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]
Phương pháp số 4: Sử dụng MAP và __GetItem__ Phương thức & NBSP;
Với các phương pháp này, về cơ bản chúng tôi đang truy cập phần tử tại một vị trí nhất định từ danh sách. Chúng tôi khớp một danh sách chứa một phần tử và một danh sách chứa một chỉ mục phần tử mà chúng tôi muốn và ánh xạ hai mảng và sử dụng __getItem__ để lấy phần tử từ chỉ mục được lưu trữ trong danh sách chỉ mục.
Python3
test_list
lst[-2]
0 lst[-2]
1lst[-2]
2lst[-2]
3lst[-2]
4lst[-2]
3lst[-2]
6lst[-2]
3lst[-2]
8lst[-2]
3__The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]2
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]4
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]7
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]8
lst[-2]
0 test_list
6test_list
7 test_list
8test_list
9The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]2
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]8
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]1
Output:
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]
Phương pháp số 4: Sử dụng MAP và __GetItem__ Phương thức & NBSP;method
Với các phương pháp này, về cơ bản chúng tôi đang truy cập phần tử tại một vị trí nhất định từ danh sách. Chúng tôi khớp một danh sách chứa một phần tử và một danh sách chứa một chỉ mục phần tử mà chúng tôi muốn và ánh xạ hai mảng và sử dụng __getItem__ để lấy phần tử từ chỉ mục được lưu trữ trong danh sách chỉ mục.
JavaScript
lst[-2]
12lst[-2]
13lst[-2]
14lst[-2]
15lst[-2]
16lst[-2]
17lst[-2]
18lst[-2]
19lst[-2]
20 lst[-2]
21lst[-2]
22lst[-2]
13lst[-2]
24lst[-2]
25lst[-2]
19lst[-2]
27 lst[-2]
28Output:
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 4