Hướng dẫn extract first element of list python - Trích xuất yếu tố đầu tiên của danh sách Python

Đô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]
91
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]
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
4
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
7

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]
91
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]
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
4
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
7

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]
91
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]
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
4
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
7

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, 3

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]
381,
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]
381,
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]
381,
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]
381,
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)


Làm thế nào để bạn trích xuất phần tử đầu tiên của một danh sách?

Để chỉ trích xuất phần tử đầu tiên từ một danh sách, chúng ta có thể sử dụng hàm SAPPLY và truy cập phần tử đầu tiên bằng dấu ngoặc vuông. Ví dụ: nếu chúng ta có một danh sách gọi là Danh sách chứa 5 phần tử, mỗi phần tử chứa 20 phần tử thì phần tử phụ đầu tiên có thể được trích xuất bằng cách sử dụng lệnh saply (danh sách, "[", 1).use sapply function and access the first element with double square brackets. For example, if we have a list called LIST that contains 5 elements each containing 20 elements then the first sub-element can be extracted by using the command sapply(LIST,"[[",1).

Làm thế nào để bạn trích xuất phần tử đầu tiên trong danh sách các bộ dữ liệu?

Sử dụng lập chỉ mục để có được phần tử đầu tiên của mỗi Tuple Sử dụng vòng lặp để lặp mặc dù một danh sách các bộ dữ liệu.Trong vòng lặp for, sử dụng Tuple Cú pháp lập chỉ mục [0] để truy cập phần tử đầu tiên của mỗi Tuple và Danh sách cuộc gọi.nối (đối tượng) với đối tượng là phần tử đầu tiên của Tuple để nối từng phần tử đầu tiên để liệt kê. Use a for-loop to iterate though a list of tuples. Within the for-loop, use the indexing syntax tuple[0] to access the first element of each tuple , and call list. append(object) with object as the tuple's first element to append each first element to list .

Làm cách nào để trích xuất một mục cụ thể từ một danh sách trong Python?

6 cách dễ dàng để trích xuất các yếu tố từ danh sách Python..
Xây dựng vấn đề và tổng quan về giải pháp ..
Phương pháp 1: Sử dụng cắt ..
Phương pháp 2: Sử dụng chỉ số danh sách ..
Phương pháp 3: Sử dụng khả năng hiểu danh sách đơn giản ..
Phương pháp 4: Sử dụng danh sách hiểu với điều kiện ..
Phương pháp 5: Sử dụng liệt kê ..
Phương pháp 6: Sử dụng mảng numpy ().
Summary..

Làm cách nào để trích xuất phần tử đầu tiên và cuối cùng từ một danh sách trong Python?

Giải pháp cơ bản.Để có được một lát chỉ là yếu tố đầu tiên và cuối cùng của một số tôi (ví dụ: danh sách hoặc một chuỗi), hãy sử dụng biểu thức cắtcủa Len (của tôi) -1.use the slicing expression my[::len(my)-1] with default start and stop operands and the step size of len(my)-1 .