Đô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
=
[
1
,
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]0____9____
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]8
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
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
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]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
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
1
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 41
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]9
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 44
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 47
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ố 2: Sử dụng Danh sách cắt
Người ta cũng có thể sử dụng kỹ thuật cắt danh sách để thực hiện nhiệm vụ cụ thể là có được yếu tố đầu tiên và cuối cùng. Chúng ta có thể sử dụng bước của toàn bộ danh sách để bỏ qua phần tử cuối cùng sau phần tử đầu tiên. & NBSP;
Python3
test_list
=
[
1
,
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]0____9____
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]8
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
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
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]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
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
1
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 41
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]9
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 44
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 47
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ố 2: Sử dụng Danh sách cắt
Người ta cũng có thể sử dụng kỹ thuật cắt danh sách để thực hiện nhiệm vụ cụ thể là có được yếu tố đầu tiên và cuối cùng. Chúng ta có thể sử dụng bước của toàn bộ danh sách để bỏ qua phần tử cuối cùng sau phần tử đầu tiên. & NBSP;
Python3
test_list
=
[
1
,
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]0____9____
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]8
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
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
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]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
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
1
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 41
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]9
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 44
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 47
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ố 2: Sử dụng Danh sách cắtmethod
Người ta cũng có thể sử dụng kỹ thuật cắt danh sách để thực hiện nhiệm vụ cụ thể là có được yếu tố đầu tiên và cuối cùng. Chúng ta có thể sử dụng bước của toàn bộ danh sách để bỏ qua phần tử cuối cùng sau phần tử đầu tiên. & NBSP;
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4=
test_list
9=
0=
1The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
91
__17
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]08
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]09
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]10
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]11
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]12
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]13
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]14
Phương pháp số 3: Sử dụng danh sách hiểu
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]18
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]09
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]20
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]21
Danh sách hiểu có thể được sử dụng để cung cấp tốc ký cho kỹ thuật vòng lặp để tìm các yếu tố đầu tiên và cuối cùng của danh sách. Phương pháp tìm kiếm ngây thơ được chuyển đổi thành một dòng bằng phương pháp này. & NBSP;
Output:
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 4
Đưa ra một danh sách các danh sách, hãy viết một chương trình Python để trích xuất phần tử đầu tiên của mỗi người phụ trong danh sách các danh sách đã cho. Ví dụ:Examples:
Input : [[1, 2], [3, 4, 5], [6, 7, 8, 9]] Output : [1, 3, 6] Input : [['x', 'y', 'z'], ['m'], ['a', 'b'], ['u', 'v']] Output : ['x', 'm', 'a', 'u']
& nbsp; Cách tiếp cận số 1: Danh sách hiểu biết & NBSP;Approach #1 : List comprehension
Python3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]25
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]26
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]27
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]28
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]29
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]31
,
3The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]36
=
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]38
1
,
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]41__
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]58
Độ phức tạp về thời gian: O [n] O[n]
Độ phức tạp không gian: O [n] [chiều dài của danh sách được đưa ra là đối số]O[n] [length of the list given as argument]
& nbsp; Cách tiếp cận #2: Sử dụng toán tử Zip và giải nén [ *] Phương thức này sử dụng ZIP với toán tử * hoặc giải nén, chuyển tất cả các mục bên trong ‘LST, làm đối số cho chức năng ZIP. Do đó, tất cả các yếu tố đầu tiên sẽ trở thành bộ tuple đầu tiên của danh sách có khóa. Trả lại phần tử 0 sẽ như vậy, giải quyết mục đích. & Nbsp;Approach #2 : Using zip and unpacking[*] operator This method uses zip with * or unpacking operator which passes all the items inside the ‘lst’ as arguments to zip function. Thus, all the first element will become the first tuple of the zipped list. Returning the 0th element will thus, solve the purpose.
Python3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]25
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]26
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]27
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]28
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]63
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]63__
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]36
=
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]38
1
,
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]41__
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]58
Độ phức tạp về thời gian: O [n] [hàm zip có độ phức tạp của O [1] và chuyển đổi thành danh sách có O [n]]O[n] [zip function has complexity of O[1] and conversion to list has O[n]]
Độ phức tạp không gian: O [n] [chiều dài của danh sách được đưa ra là đối số]O[n] [length of the list given as argument]
& nbsp; Cách tiếp cận #2: Sử dụng toán tử Zip và giải nén [ *] Phương thức này sử dụng ZIP với toán tử * hoặc giải nén, chuyển tất cả các mục bên trong ‘LST, làm đối số cho chức năng ZIP. Do đó, tất cả các yếu tố đầu tiên sẽ trở thành bộ tuple đầu tiên của danh sách có khóa. Trả lại phần tử 0 sẽ như vậy, giải quyết mục đích. & Nbsp;
Python3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]25
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]26
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]27
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]28
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]63
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]63__
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]36
=
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]38
1
,
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]41__Approach #3 : Using itemgetter[]
Python3
Độ phức tạp về thời gian: O [n] [hàm zip có độ phức tạp của O [1] và chuyển đổi thành danh sách có O [n]]
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]25
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]26
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]27
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]28
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]63
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]63__
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]36
=
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]38
1
,
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]41__
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]58
Độ phức tạp về thời gian: O [n] [hàm zip có độ phức tạp của O [1] và chuyển đổi thành danh sách có O [n]] O[n] [itemgetter has O[1] and conversion to list O[n]]
Một phương pháp khác để sử dụng zip được đưa ra dưới đây:-& nbsp; O[n]