Hướng dẫn how do i get one part of a list in python? - làm cách nào để lấy một phần của danh sách trong 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

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]
6
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]
6
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1__16

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
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]
3
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]
5
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
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]
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
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]
4

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

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]
6
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]
6
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1__16

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
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]
3
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]
5
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
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]
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
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]
4

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

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]
6
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]
6
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1__16

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
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]
3
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]
5
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
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]
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
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]
4

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
1
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
3
list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]
6
list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]
7
list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]
8
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]
5
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]
15

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
16

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
17

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]
19

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

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]
25

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
16

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

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

Trả về chỉ số thấp nhất trong danh sách OBJ xuất hiệnsequence. Each element of a sequence is assigned a number - its position or index. The first index is zero, the second index is one, and so forth.

list.insert [index, obj]

Chèn đối tượng obj vào danh sách tại chỉ mục bù

Xóa và trả về đối tượng cuối cùng hoặc OBJ khỏi danh sách

Xóa đối tượng OBJ khỏi danh sách

Đảo ngược các đối tượng của danh sách tại chỗ

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]

Tương tự như các chỉ số chuỗi, chỉ số danh sách bắt đầu từ 0 và danh sách có thể được cắt lát, được nối, v.v.

Truy cập các giá trị trong danh sách

Để truy cập các giá trị trong danh sách, hãy sử dụng dấu ngoặc vuông để cắt cùng với chỉ mục hoặc chỉ số để có được giá trị có sẵn tại chỉ mục đó. Ví dụ -

#!/usr/bin/python

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]

Khi mã trên được thực thi, nó sẽ tạo ra kết quả sau -

list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]

Cập nhật danh sách

Bạn có thể cập nhật một hoặc nhiều yếu tố của danh sách bằng cách đưa lát ở phía bên trái của toán tử gán và bạn có thể thêm vào các phần tử trong danh sách với phương thức append []. Ví dụ -

#!/usr/bin/python

list = ['physics', 'chemistry', 1997, 2000];
print "Value available at index 2 : "
print list[2]
list[2] = 2001;
print "New value available at index 2 : "
print list[2]

Lưu ý - phương thức append [] được thảo luận trong phần tiếp theo. − append[] method is discussed in subsequent section.

Khi mã trên được thực thi, nó sẽ tạo ra kết quả sau -

Value available at index 2 :
1997
New value available at index 2 :
2001

Cập nhật danh sách

Bạn có thể cập nhật một hoặc nhiều yếu tố của danh sách bằng cách đưa lát ở phía bên trái của toán tử gán và bạn có thể thêm vào các phần tử trong danh sách với phương thức append []. Ví dụ -

#!/usr/bin/python

list1 = ['physics', 'chemistry', 1997, 2000];
print list1
del list1[2];
print "After deleting value at index 2 : "
print list1

Lưu ý - phương thức append [] được thảo luận trong phần tiếp theo.

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
0

Xóa các yếu tố danh sách − remove[] method is discussed in subsequent section.

Để xóa phần tử danh sách, bạn có thể sử dụng câu lệnh DEL nếu bạn biết chính xác [các] phần tử nào bạn đang xóa hoặc phương thức xóa [] nếu bạn không biết. Ví dụ -

Khi mã trên được thực thi, nó sẽ tạo ra kết quả sau -

Lưu ý - Remove [] Phương thức được thảo luận trong phần tiếp theo.

Hoạt động danh sách cơ bảnDanh sách trả lời các toán tử + và * giống như chuỗi; Chúng có nghĩa là nối và lặp lại ở đây, ngoại trừ kết quả là một danh sách mới, không phải là một chuỗi.Trên thực tế, các danh sách đáp ứng tất cả các hoạt động trình tự chung mà chúng tôi đã sử dụng trên các chuỗi trong chương trước.
Biểu hiện Python3 Kết quả
Sự mô tảLen [[1, 2, 3]]Chiều dài
[1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6]Kết nối
['Hi!'] * 4['Hi!', 'Hi!', 'Hi!', 'Hi!']]Sự lặp lại
3 trong [1, 2, 3]ĐÚNG VẬYTư cách thành viên

cho x trong [1, 2, 3]: in x,

1 2 3

Lặp đi lặp lại

Lập chỉ mục, cắt và ma trậnHoạt động danh sách cơ bảnDanh sách trả lời các toán tử + và * giống như chuỗi; Chúng có nghĩa là nối và lặp lại ở đây, ngoại trừ kết quả là một danh sách mới, không phải là một chuỗi.Trên thực tế, các danh sách đáp ứng tất cả các hoạt động trình tự chung mà chúng tôi đã sử dụng trên các chuỗi trong chương trước.
Biểu hiện PythonKết quảSự mô tả
Len [[1, 2, 3]]Chiều dài[1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]Kết nối['Hi!'] * 4

['Hi!', 'Hi!', 'Hi!', 'Hi!']]

Sự lặp lại

Sr.No.3 trong [1, 2, 3]
1 ĐÚNG VẬY

Tư cách thành viên

2 len[list]

cho x trong [1, 2, 3]: in x,

3 max[list]

1 2 3

4 min[list]

Lặp đi lặp lại

5 list[seq]

Lập chỉ mục, cắt và ma trận

Bởi vì danh sách là các chuỗi, lập chỉ mục và cắt làm việc giống như cách cho các danh sách như chúng làm cho các chuỗi.

Sr.No.Giả sử đầu vào sau -
1 list.append[obj]

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1

2 list.count[obj]

L [2]

3 list.extend[seq]

THƯ RÁC!

4 list.index[obj]

Độ lệch bắt đầu từ 0

5 L [-2]

Thư rác

6 list.pop[obj=list[-1]]

Tiêu cực: Đếm từ bên phải

7 list.remove[obj]

L [1:]

8 list.reverse[]

['Spam', 'Spam!']]

9 list.sort[[func]]

Cắt lát phần

Làm thế nào để bạn tìm thấy một nửa của một danh sách trong Python?

Điều này có thể được thực hiện bằng các bước sau:..
Nhận độ dài của một danh sách bằng hàm Len [] ..
Nếu độ dài của các bộ phận không được đưa ra, thì hãy chia độ dài của danh sách cho 2 bằng toán tử sàn để lấy chỉ số giữa của danh sách ..
Cắt danh sách thành hai nửa bằng cách sử dụng [: middle_index] và [middle_index:].

Làm thế nào để bạn lấy một yếu tố từ một danh sách?

Có ba cách mà bạn có thể xóa các phần tử khỏi danh sách: sử dụng phương thức Remove []. Sử dụng phương thức pop [] của đối tượng. Sử dụng toán tử DEL.Using the remove[] method. Using the list object's pop[] method. Using the del operator.

Làm cách nào để in một yếu tố cụ thể trong danh sách Python?

Không sử dụng các vòng lặp: * Biểu tượng được sử dụng để in các phần tử danh sách trong một dòng duy nhất có không gian.Để in tất cả các phần tử trong các dòng mới hoặc được phân tách bằng dấu phẩy sử dụng sep =, \ n, hoặc sep =, tương ứng.* symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by comma use sep=”\n” or sep=”, ” respectively.

Làm thế nào để bạn cắt một danh sách trong Python?

hàm clip [] được sử dụng để clip [giới hạn] các giá trị trong một mảng.Đưa ra một khoảng, các giá trị bên ngoài khoảng thời gian được cắt vào các cạnh khoảng.Ví dụ: nếu một khoảng [0, 1] được chỉ định, các giá trị nhỏ hơn 0 trở thành 0 và các giá trị lớn hơn 1 trở thành 1.. Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.

Bài Viết Liên Quan

Chủ Đề