Python lặp qua danh sách và tách chuỗi

Cho một Chuỗi bao gồm nhiều từ được phân tách bằng dấu cách, hãy viết chương trình Python để lặp lại các từ này của chuỗi

ví dụ

Đầu vào. str = “GeeksforGeeks là một cổng thông tin khoa học máy tính dành cho các Chuyên viên máy tính”
Đầu ra.
GeeksforGeek

a
máy tính
khoa học
portal
for
Geeks

Đầu vào. str = “Geek for Geek”
Đầu ra.
Geeks
dành cho
Geeks

Phương pháp 1. Sử dụng

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
0

Sử dụng hàm

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
0, chúng ta có thể chia chuỗi thành một danh sách các từ và là phương pháp chung nhất và được đề xuất nếu một người muốn hoàn thành tác vụ cụ thể này. Nhưng nhược điểm là nó bị lỗi trong các trường hợp chuỗi chứa dấu câu




The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
2

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
3

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
4

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
5

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
6

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
7
The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
0
The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
1

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
5

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
3

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
4
The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
5
The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
6
The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
8

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
5

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
4

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
01

________ 302 ________ 00 ________ 304

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
5

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
06

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
4
The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
5
The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
09
The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
00

________ 301 ________ 302 ________ 303 ________ 304

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
05
The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
4
The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
07

đầu ra

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
2

Phương pháp 2. Sử dụng

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
08

Trong các trường hợp chứa tất cả các ký tự đặc biệt và dấu chấm câu, như đã thảo luận ở trên, phương pháp tìm từ thông thường trong chuỗi bằng cách sử dụng phân tách có thể không thành công và do đó cần có các biểu thức chính quy để thực hiện tác vụ này. Hàm

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
09 trả về danh sách sau khi lọc chuỗi và trích xuất các từ bỏ qua dấu chấm câu

Trong Python, nhiệm vụ phổ biến là lặp qua một chuỗi để lấy từng ký tự hoặc chia nó thành nhiều đoạn hoặc thậm chí lặp qua chuỗi đó theo thứ tự ngược lại

Làm cách nào để lặp qua một chuỗi trong Python? . Chúng ta cũng có thể sử dụng RANGE với vòng lặp for để lặp qua một loạt các ký tự trong một chuỗi. Một cách khác chúng tôi sử dụng INDEX là trả về từng ký tự của một chỉ mục cụ thể. Chúng ta cũng có thể chia chuỗi thành các đoạn có số lượng ký tự xác định

Bài viết này sẽ giải thích việc lặp qua chuỗi trong Python bằng cách đưa ra nhiều ví dụ thực tế để bạn hiểu rõ hơn

cú pháp

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
3

Tham số đầu vào

  • iterator_element. biến sẽ giữ từng giá trị của một chuỗi đã cho
  • chuỗi_giá_trị/chuỗi_biến_tên. Các chuỗi mà chúng tôi muốn lặp qua nó

Chuỗi lặp Python Sử dụng vòng lặp for

Ví dụ 1]

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
4

đầu ra

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
5

Trong ví dụ trên, chúng tôi đã tìm nạp phần tử 'python' có giá trị chuỗi và in nó thành các dòng riêng biệt bằng cách sử dụng phép lặp

Chuỗi lặp Python có chỉ mục Sử dụng vòng lặp for và phạm vi []

Ví dụ [2]

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
6

đầu ra

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
7

Trong ví dụ trên, sử dụng vòng lặp for với rang, chúng ta lặp qua phạm vi của chuỗi “mystring” và sau đó in từng giá trị chuỗi trong một dòng riêng biệt

Chuỗi lặp Python có chỉ mục Sử dụng vòng lặp while và phạm vi []

Ví dụ [3]

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
8

đầu ra

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
9

Trong ví dụ trên, sử dụng phép lặp với vòng lặp while, chúng tôi đã tìm nạp phần tử của chuỗi [“Xin chào Oraask”] và in chúng cùng nhau trong các dòng riêng biệt

Python lặp qua các ký tự của một chuỗi

Sử dụng chuỗi [vị trí bắt đầu. vị trí dừng. bước tăng dần], chúng ta có thể lặp qua một phần của biến chuỗi bằng cách tạo một lát chuỗi nhỏ bằng cách sử dụng toán tử chuỗi []

Ví dụ [4]

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
0

đầu ra

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
1

Trong ví dụ trên, bằng cách sử dụng phép lặp với toán tử cắt, chúng tôi đã tìm nạp các phần tử từ vị trí 6 đến vị trí 12 của một chuỗi ["Xin chào Oraask"] là một từ ["Oraask"], sau đó in chúng cùng nhau trong các dòng riêng biệt

Chuỗi lặp Python ngược

Sử dụng chuỗi [vị trí bắt đầu [-]. vị trí dừng [-]. bước tăng dần [-]], chúng ta có thể lặp qua một phần của biến chuỗi ngược lại bằng cách tạo một lát nhỏ của chuỗi bằng cách sử dụng toán tử chuỗi []

Ví dụ [5]

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
2

đầu ra

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
40

Trong ví dụ trên, sử dụng phép lặp với toán tử cắt nhưng có dấu âm cho vị trí bắt đầu, kết thúc và tăng dần, chúng tôi đã tìm nạp các phần tử từ vị trí -1 đến vị trí -7 của một chuỗi [“Xin chào Python”] là một

Python Lặp lại chuỗi 2 ký tự cùng một lúc

Sử dụng chuỗi [vị trí bắt đầu. vị trí dừng. bước tăng dần ], chúng ta có thể lặp qua một biến chuỗi nhưng bỏ qua hai ký tự đồng thời

Ví dụ [6]

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
41

đầu ra

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
42

Trong ví dụ trên, sử dụng vị trí gia tăng để chỉ ra cách trình vòng lặp hoạt động giống như chuỗi lặp hai ký tự cùng một lúc, chúng tôi đã nhảy hai ký tự cùng một lúc qua chuỗi [“HloOas”] bằng cách cưỡi các ký tự [“el rak”]

Chuỗi lặp Python theo khối

Bằng cách sử dụng tính năng Hiểu danh sách với chuỗi, chúng ta có thể chia chuỗi thành các đoạn có độ dài cụ thể rồi trả về kết quả dưới dạng một mảng

Ví dụ [7]

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
43

đầu ra

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
44

Trong ví dụ trên, chúng tôi đã tạo một danh sách mới có tên là các khối để giữ các giá trị mới dưới dạng các khối gồm 4 ký tự, mỗi ký tự trong số các biến “mystring”. Bạn có thể thay đổi giá trị biến “checksplit” để cho biết có bao nhiêu ký tự sẽ được chia thành nhiều phần

Cách lặp qua bảng chữ cái trong Python

Trước tiên, chúng ta có thể lặp qua bảng chữ cái trong python bằng cách sử dụng vòng lặp for đơn giản, chúng ta phải tạo biến bảng chữ cái để có thể lặp qua nó

Ví dụ [8]

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
45

đầu ra

The original string is : GeeksforGeeks is a computer science portal for Geeks!!!

The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
46

Giải trình

Trong ví dụ trên, chúng tôi đã nhập mô-đun chuỗi, sau đó, bằng cách sử dụng chuỗi, chúng tôi đã tạo biến alphabetsVar;

Bạn có thể tách một chuỗi trong danh sách Python không?

Phương thức tách chuỗi Python[] . Bạn có thể chỉ định dấu phân cách, dấu phân cách mặc định là bất kỳ khoảng trắng nào. Ghi chú. Khi maxsplit được chỉ định, danh sách sẽ chứa số phần tử đã chỉ định cộng với một. The split[] method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Phương thức split[] trả về từ danh sách các từ là gì?

Mô tả. Phương thức chuỗi Python split[] trả về danh sách tất cả các từ trong chuỗi , sử dụng str làm dấu phân tách [tách trên tất cả khoảng trắng nếu không được chỉ định], .

Làm. phân chia công việc trên chuỗi?

Split được sử dụng để ngắt một chuỗi được phân tách thành các chuỗi con . Bạn có thể sử dụng mảng ký tự hoặc mảng chuỗi để chỉ định không hoặc nhiều ký tự hoặc chuỗi phân tách.

Chủ Đề