Danh sách chia Python làm đôi

🍎Tóm tắt. Phương pháp đơn giản nhất để tách một nửa chuỗi là cắt chuỗi ở giữa bằng cách sử dụng chỉ số giữa của nó, có thể được tính là

# Input
text = "Sun rises in the east and sets in the west"
# Expected Output
First Half:  Sun rises in the east
Second Half:  and sets in the west
0. Có rất nhiều cách khác để giải quyết vấn đề, đã được thảo luận dưới đây

Ví dụ tối thiểu

text = "abc xyz"

# Method 1
first_part, second_part = text[:len[text]//2], text[len[text]//2:]
print["First Half: ", first_part]
print["Second Half: ", second_part]

# Method 2
half, rem = divmod[len[text], 2]
print["First Half: ", text[:half + rem]]
print["Second Half: ", text[half + rem:]]

# Method 3
li = text.split[]
n = len[li]//2
res = [" ".join[li[x:x+n]] for x in range[0, len[li], n]]
print["First Half: ", res[0]]
print["Second Half: ", res[1]]


# Method 4
from itertools import accumulate
li = text.split[]
len_split = [len[li]//2]*2
results = [li[x - y: x] for x, y in zip[accumulate[len_split], len_split]]
print["First Half: ", " ".join[results[0]]]
print["Second Half: ", " ".join[results[1]]]

# Outputs:
# First Half:  abc
# Second Half: xyz

Xây dựng vấn đề

✨Sự cố. Cho một chuỗi. Bạn sẽ chia chuỗi thành hai nửa như thế nào?

Ví dụ

Đưa ra dưới đây là một chuỗi chứa mười chuỗi con. Bạn phải chia nó thành hai nửa sao cho mỗi nửa chứa chính xác năm từ

# Input
text = "Sun rises in the east and sets in the west"
# Expected Output
First Half:  Sun rises in the east
Second Half:  and sets in the west

Còn chần chừ gì nữa, hãy đi sâu vào các giải pháp

Phương pháp 1. sử dụng cắt lát

Tiếp cận. Một cách để chia chuỗi thành hai nửa là cắt nó bằng cách sử dụng chỉ số giữa của nó. Chỉ số giữa có thể được tính là

# Input
text = "Sun rises in the east and sets in the west"
# Expected Output
First Half:  Sun rises in the east
Second Half:  and sets in the west
0. Khi bạn có chỉ mục giữa, nửa đầu có thể được trích xuất bằng cách cắt chuỗi từ đầu cho đến giữa chỉ mục. Trong khi nửa thứ hai có thể được trích xuất bằng cách cắt chuỗi từ chỉ số giữa cho đến cuối

Mã số

text = "Sun rises in the east and sets in the west"
first_part, second_part = text[:len[text]//2], text[len[text]//2:]
print["First Half: ", first_part]
print["Second Half: ", second_part]

đầu ra

First Half:  Sun rises in the east
Second Half:  and sets in the west

chuyện vặt vãnh. Cắt lát là một khái niệm để cắt ra một chuỗi con từ một chuỗi đã cho. Sử dụng ký hiệu cắt lát 

# Input
text = "Sun rises in the east and sets in the west"
# Expected Output
First Half:  Sun rises in the east
Second Half:  and sets in the west
2 để truy cập mọi phần tử thứ 
# Input
text = "Sun rises in the east and sets in the west"
# Expected Output
First Half:  Sun rises in the east
Second Half:  and sets in the west
3 bắt đầu từ chỉ mục 
# Input
text = "Sun rises in the east and sets in the west"
# Expected Output
First Half:  Sun rises in the east
Second Half:  and sets in the west
4 [bao gồm] và kết thúc bằng chỉ mục 
# Input
text = "Sun rises in the east and sets in the west"
# Expected Output
First Half:  Sun rises in the east
Second Half:  and sets in the west
5 [không bao gồm]. Cả ba đối số đều là tùy chọn, vì vậy bạn có thể bỏ qua chúng để sử dụng các giá trị mặc định [
# Input
text = "Sun rises in the east and sets in the west"
# Expected Output
First Half:  Sun rises in the east
Second Half:  and sets in the west
6, 
# Input
text = "Sun rises in the east and sets in the west"
# Expected Output
First Half:  Sun rises in the east
Second Half:  and sets in the west
7, 
# Input
text = "Sun rises in the east and sets in the west"
# Expected Output
First Half:  Sun rises in the east
Second Half:  and sets in the west
8]. Ví dụ: biểu thức 
# Input
text = "Sun rises in the east and sets in the west"
# Expected Output
First Half:  Sun rises in the east
Second Half:  and sets in the west
9 từ chuỗi 
text = "Sun rises in the east and sets in the west"
first_part, second_part = text[:len[text]//2], text[len[text]//2:]
print["First Half: ", first_part]
print["Second Half: ", second_part]
0 xé ra lát cắt 
text = "Sun rises in the east and sets in the west"
first_part, second_part = text[:len[text]//2], text[len[text]//2:]
print["First Half: ", first_part]
print["Second Half: ", second_part]
1 và biểu thức 
text = "Sun rises in the east and sets in the west"
first_part, second_part = text[:len[text]//2], text[len[text]//2:]
print["First Half: ", first_part]
print["Second Half: ", second_part]
2 xé ra lát cắt _______4_______3

🌟Đọc liên quan. Giới thiệu về Slicing trong Python

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

Điều kiện tiên quyết. Hàm 

text = "Sun rises in the east and sets in the west"
first_part, second_part = text[:len[text]//2], text[len[text]//2:]
print["First Half: ", first_part]
print["Second Half: ", second_part]
4 tích hợp của Python lấy hai số nguyên hoặc số thực 
text = "Sun rises in the east and sets in the west"
first_part, second_part = text[:len[text]//2], text[len[text]//2:]
print["First Half: ", first_part]
print["Second Half: ", second_part]
5 và 
text = "Sun rises in the east and sets in the west"
first_part, second_part = text[:len[text]//2], text[len[text]//2:]
print["First Half: ", first_part]
print["Second Half: ", second_part]
6 làm đối số đầu vào và trả về một bộ 
text = "Sun rises in the east and sets in the west"
first_part, second_part = text[:len[text]//2], text[len[text]//2:]
print["First Half: ", first_part]
print["Second Half: ", second_part]
7. Giá trị bộ đầu tiên là kết quả của phép chia số nguyên 
text = "Sun rises in the east and sets in the west"
first_part, second_part = text[:len[text]//2], text[len[text]//2:]
print["First Half: ", first_part]
print["Second Half: ", second_part]
8. Bộ thứ hai là kết quả của phần còn lại, còn được gọi là phép toán modulo 
text = "Sun rises in the east and sets in the west"
first_part, second_part = text[:len[text]//2], text[len[text]//2:]
print["First Half: ", first_part]
print["Second Half: ", second_part]
9. Trong trường hợp nhập số float, _______5_______0 vẫn trả về phép chia không có phần dư bằng cách làm tròn xuống số tròn tiếp theo

Ý tưởng ở đây hoàn toàn giống với ý tưởng được sử dụng trước đây. Sự khác biệt duy nhất ở đây là - chúng tôi đang sử dụng phương pháp

First Half:  Sun rises in the east
Second Half:  and sets in the west
1 để tìm ra chỉ số giữa của chuỗi có thể được sử dụng để cắt nó thành hai nửa

Mã số

text = "Sun rises in the east and sets in the west"
half, rem = divmod[len[text], 2]
print["First Half: ", text[:half + rem]]
print["Second Half: ", text[half + rem:]]

đầu ra

First Half:  Sun rises in the east
Second Half:  and sets in the west

Phương pháp 3. Phân tách và sử dụng danh sách hiểu

Tiếp cận. Cắt chuỗi để tạo danh sách phân tách các chuỗi con. Vì các từ được phân tách bằng khoảng trắng trong trường hợp của chúng tôi, nên chúng tôi có thể chỉ cần tách chuỗi mà không cần chuyển bất kỳ dấu phân cách nào, vì chuỗi sẽ được tách khi xuất hiện bất kỳ ký tự khoảng trắng nào

Sau khi bạn có danh sách, hãy sử dụng khả năng hiểu danh sách để lặp qua độ dài của chuỗi sao cho kích thước bước lặp bằng một nửa độ dài của chuỗi. Điều này cho phép bạn chia danh sách thành hai phần bằng nhau. Sau đó, bạn có thể tham gia từng mục của từng phần của danh sách bằng cách sử dụng phương pháp

First Half:  Sun rises in the east
Second Half:  and sets in the west
2

Mã số

text = "Sun rises in the east and sets in the west"
li = text.split[]
n = len[li]//2
res = [" ".join[li[x:x+n]] for x in range[0, len[li], n]]
print["First Half: ", res[0]]
print["Second Half: ", res[1]]

đầu ra

First Half:  Sun rises in the east
Second Half:  and sets in the west

chuyện vặt vãnh.

First Half:  Sun rises in the east
Second Half:  and sets in the west
3 nối các phần tử trong một 
First Half:  Sun rises in the east
Second Half:  and sets in the west
4. Kết quả là một chuỗi, trong khi mỗi phần tử trong iterable được "dán lại với nhau" bằng cách sử dụng chuỗi mà nó được gọi là dấu phân cách

🌟Đọc liên quan. Tham gia chuỗi Python[]

Phương pháp 4. sử dụng tích lũy

Một cách khác để giải quyết vấn đề đã cho là sử dụng hàm

First Half:  Sun rises in the east
Second Half:  and sets in the west
5 từ mô-đun
First Half:  Sun rises in the east
Second Half:  and sets in the west
6

Mã số

from itertools import accumulate

text = "Sun rises in the east and sets in the west"
len_split = [len[li]//2]*2
results = [li[x - y: x] for x, y in zip[accumulate[len_split], len_split]]
print["First Half: ", " ".join[results[0]]]
print["Second Half: ", " ".join[results[1]]]

đầu ra

First Half:  Sun rises in the east
Second Half:  and sets in the west

Giải trình. Đoạn mã trên nhập thư viện 

First Half:  Sun rises in the east
Second Half:  and sets in the west
7 để gọi và sử dụng hàm 
First Half:  Sun rises in the east
Second Half:  and sets in the west
5. Tách chuỗi bằng hàm tách, tạo danh sách chứa các chuỗi con đã tách. Sử dụng danh sách này và tính toán chỉ số giữa và nhân kết quả suy ra từ danh sách hai lần thành
First Half:  Sun rises in the east
Second Half:  and sets in the west
9. Cuối cùng, sử dụng khả năng hiểu danh sách sử dụng các phương thức
text = "Sun rises in the east and sets in the west"
half, rem = divmod[len[text], 2]
print["First Half: ", text[:half + rem]]
print["Second Half: ", text[half + rem:]]
0 và
text = "Sun rises in the east and sets in the west"
half, rem = divmod[len[text], 2]
print["First Half: ", text[:half + rem]]
print["Second Half: ", text[half + rem:]]
1 để lặp qua danh sách chứa các chuỗi con đã tách và chia 
text = "Sun rises in the east and sets in the west"
half, rem = divmod[len[text], 2]
print["First Half: ", text[:half + rem]]
print["Second Half: ", text[half + rem:]]
2 thành hai [2] dựa trên phép tính đã thực hiện trước đó

Phần kết luận

Phù. Chúng tôi đã học được tới bốn cách khác nhau để chia đôi một chuỗi. Hãy sử dụng phương pháp phù hợp với nhu cầu của bạn. Tôi hy vọng hướng dẫn này đã thêm một số giá trị cho hành trình viết mã của bạn. Hãy đăng ký và theo dõi để đọc và thảo luận thú vị hơn

Shubham Sayon

Tôi là một người tạo nội dung và Blogger Python chuyên nghiệp. Tôi đã xuất bản nhiều bài báo và tạo các khóa học trong một khoảng thời gian. Hiện tại tôi đang làm việc với tư cách là một freelancer toàn thời gian và tôi có kinh nghiệm trong các lĩnh vực như Python, AWS, DevOps và Networking

Chủ Đề