Viết chương trình Python để tách [các] chuỗi đã cho thành các chuỗi nếu trong chuỗi có khoảng trắng, ngược lại tách trên dấu phẩy nếu có dấu phẩy, ngược lại trả về danh sách các chữ cái viết thường có thứ tự lẻ [thứ tự của a = 0, . ]
Input: a b c d Split the said string into strings if there is a space in the string, otherwise split on commas if there is a comma, Output: ['a', 'b', 'c', 'd'] Input: a,b,c,d Split the said string into strings if there is a space in the string, otherwise split on commas if there is a comma, Output: ['a', 'b', 'c', 'd'] Input: abcd Split the said string into strings if there is a space in the string, otherwise split on commas if there is a comma, Output: ['b', 'd']
Trình bày bằng hình ảnh
Giải pháp mẫu
Mã Python
#License: //bit.ly/3oLErEI
def test[s]:
if " " in s:
return s.split[" "]
if "," in s:
return s.split[","]
return [c for c in s if c.islower[] and ord[c] % 2 == 0]
strs = "a b c d"
print["Original string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "a,b,c,d"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "abcd"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
Đầu ra mẫu
Original string: a b c d Split the said string into strings if there is a space in the string, otherwise split on commas if there is a comma, otherwise return the list of lowercase letters with odd order: ['a', 'b', 'c', 'd'] Original string: a,b,c,d Split the said string into strings if there is a space in the string, otherwise split on commas if there is a comma, otherwise return the list of lowercase letters with odd order: ['a', 'b', 'c', 'd'] Original string: abcd Split the said string into strings if there is a space in the string, otherwise split on commas if there is a comma, otherwise return the list of lowercase letters with odd order: ['b', 'd']
Sơ đồ
Trực quan hóa việc thực thi mã Python
Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực thi chương trình nói trên
Trình chỉnh sửa mã Python
Có một cách khác để giải quyết giải pháp này?
Trước. Tìm các chỉ số của tất cả các lần xuất hiện của mục tiêu trong ma trận không đồng đều.
Tiếp theo. Xác định chiều ['tăng' hoặc 'giảm'] của các dãy số đơn điệu.
Mức độ khó của bài tập này là gì?
Dễ dàng trung bình khóKiểm tra kỹ năng Lập trình của bạn với bài kiểm tra của w3resource
con trăn. Lời khuyên trong ngày
Điều kiện "Khác" bên trong vòng lặp "for"
Mặc dù tất cả các mã Python mà bạn đã thấy cho đến nay, rất có thể bạn đã bỏ lỡ phần "để làm gì khác" sau đây mà tôi cũng đã thấy lần đầu tiên cách đây vài tuần
Đây là một phương pháp lặp qua danh sách "for-else", trong đó mặc dù có một lần lặp qua danh sách, bạn cũng có một điều kiện "khác", điều này khá bất thường
Đây không phải là điều mà tôi từng thấy ở các ngôn ngữ lập trình khác như Java, Ruby hay JavaScript
Hãy xem một ví dụ về giao diện của nó trong thực tế
Giả sử rằng chúng ta đang cố kiểm tra xem không có số lẻ nào trong danh sách hay không
Hãy lặp lại nó
numbers = [2, 4, 6, 8, 1] for number in numbers: if number % 2 == 1: print[number] break else: print["No odd numbers"]
Trường hợp tìm được số lẻ thì số đó sẽ được in ra do ngắt sẽ được thực hiện và nhánh khác sẽ bị bỏ qua
Trong hướng dẫn này, chúng ta sẽ xem xét cách tách một chuỗi thành một danh sách các chuỗi khi xuất hiện dấu phẩy trong Python với sự trợ giúp của các ví dụ
Làm cách nào để tách một chuỗi trong Python?
Bạn có thể sử dụng hàm chuỗi
#License: //bit.ly/3oLErEI
def test[s]:
if " " in s:
return s.split[" "]
if "," in s:
return s.split[","]
return [c for c in s if c.islower[] and ord[c] % 2 == 0]
strs = "a b c d"
print["Original string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "a,b,c,d"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "abcd"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
5 của Python để tách một chuỗi [bằng dấu phân cách] thành một danh sách các chuỗi. Để tách một chuỗi bằng dấu phẩy trong Python, hãy chuyển ký tự dấu phẩy #License: //bit.ly/3oLErEI
def test[s]:
if " " in s:
return s.split[" "]
if "," in s:
return s.split[","]
return [c for c in s if c.islower[] and ord[c] % 2 == 0]
strs = "a b c d"
print["Original string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "a,b,c,d"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "abcd"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
6 làm dấu phân cách cho hàm #License: //bit.ly/3oLErEI
def test[s]:
if " " in s:
return s.split[" "]
if "," in s:
return s.split[","]
return [c for c in s if c.islower[] and ord[c] % 2 == 0]
strs = "a b c d"
print["Original string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "a,b,c,d"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "abcd"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
5Sau đây là cú pháp –
# split string s by comma s.split[","]
Nó trả về một danh sách các chuỗi kết quả từ việc tách chuỗi ban đầu trên các lần xuất hiện của
#License: //bit.ly/3oLErEI
def test[s]:
if " " in s:
return s.split[" "]
if "," in s:
return s.split[","]
return [c for c in s if c.islower[] and ord[c] % 2 == 0]
strs = "a b c d"
print["Original string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "a,b,c,d"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "abcd"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
6Hãy xem xét một số ví dụ
Tách chuỗi bằng dấu phẩy
Ở đây, chúng tôi chuyển dấu phẩy
#License: //bit.ly/3oLErEI
def test[s]:
if " " in s:
return s.split[" "]
if "," in s:
return s.split[","]
return [c for c in s if c.islower[] and ord[c] % 2 == 0]
strs = "a b c d"
print["Original string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "a,b,c,d"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "abcd"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
6 làm dấu phân cách cho hàm chuỗi #License: //bit.ly/3oLErEI
def test[s]:
if " " in s:
return s.split[" "]
if "," in s:
return s.split[","]
return [c for c in s if c.islower[] and ord[c] % 2 == 0]
strs = "a b c d"
print["Original string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "a,b,c,d"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "abcd"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
5# string with commas s = "Yes, they backed Steve, Emma, and Rohan in the meeting." # split string by comma ls = s.split[","] print[ls]
đầu ra
['Yes', ' they backed Steve', ' Emma', ' and Rohan in the meeting.']
Danh sách kết quả chứa các từ do việc tách chuỗi gốc khi xuất hiện dấu phẩy
Sửa số lần chia
Bạn cũng có thể chỉ định số lần phân tách tối đa được thực hiện bằng cách sử dụng tham số
# string with commas s = "Yes, they backed Steve, Emma, and Rohan in the meeting." # split string by comma ls = s.split[","] print[ls]3. Theo mặc định, hàm chuỗi
#License: //bit.ly/3oLErEI
def test[s]:
if " " in s:
return s.split[" "]
if "," in s:
return s.split[","]
return [c for c in s if c.islower[] and ord[c] % 2 == 0]
strs = "a b c d"
print["Original string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "a,b,c,d"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "abcd"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
5 thực hiện tất cả các phân tách có thểHãy chỉ chia chuỗi trên thành hai phần khi xuất hiện dấu phẩy,
#License: //bit.ly/3oLErEI
def test[s]:
if " " in s:
return s.split[" "]
if "," in s:
return s.split[","]
return [c for c in s if c.islower[] and ord[c] % 2 == 0]
strs = "a b c d"
print["Original string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "a,b,c,d"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "abcd"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
6bắt đầu từ bên trái. Để tách chuỗi thành hai phần, # string with commas s = "Yes, they backed Steve, Emma, and Rohan in the meeting." # split string by comma ls = s.split[","] print[ls]3 phải là
# string with commas s = "Yes, they backed Steve, Emma, and Rohan in the meeting." # split string by comma ls = s.split[","] print[ls]7, vì chúng tôi chỉ thực hiện một lần tách dẫn đến hai chuỗi
#License: //bit.ly/3oLErEI
def test[s]:
if " " in s:
return s.split[" "]
if "," in s:
return s.split[","]
return [c for c in s if c.islower[] and ord[c] % 2 == 0]
strs = "a b c d"
print["Original string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "a,b,c,d"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "abcd"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
0đầu ra
#License: //bit.ly/3oLErEI
def test[s]:
if " " in s:
return s.split[" "]
if "," in s:
return s.split[","]
return [c for c in s if c.islower[] and ord[c] % 2 == 0]
strs = "a b c d"
print["Original string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "a,b,c,d"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "abcd"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
1Bạn có thể thấy rằng danh sách kết quả chỉ có hai chuỗi
Hãy xem một ví dụ khác
Hãy chia chuỗi ban đầu thành ba phần, ở đây chúng tôi vượt qua
# string with commas s = "Yes, they backed Steve, Emma, and Rohan in the meeting." # split string by comma ls = s.split[","] print[ls]8
#License: //bit.ly/3oLErEI
def test[s]:
if " " in s:
return s.split[" "]
if "," in s:
return s.split[","]
return [c for c in s if c.islower[] and ord[c] % 2 == 0]
strs = "a b c d"
print["Original string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "a,b,c,d"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "abcd"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
3đầu ra
#License: //bit.ly/3oLErEI
def test[s]:
if " " in s:
return s.split[" "]
if "," in s:
return s.split[","]
return [c for c in s if c.islower[] and ord[c] % 2 == 0]
strs = "a b c d"
print["Original string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "a,b,c,d"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
strs = "abcd"
print["\nOriginal string:"]
print[strs]
print["Split the said string into strings if there is a space in the string, \notherwise split on commas if there is a comma, \notherwise return the list of lowercase letters with odd order:"]
print[test[strs]]
4Danh sách kết quả chỉ có ba chuỗi
Bạn cũng có thể quan tâm đến -
- Python – Tách chuỗi bằng dấu gạch dưới
- Python – Xóa nhiều khoảng trắng khỏi một chuỗi
- Loại bỏ ngắt dòng khỏi chuỗi trong Python
Đăng ký nhận bản tin của chúng tôi để biết thêm thông tin và hướng dẫn.
Chúng tôi không gửi thư rác và bạn có thể chọn không tham gia bất kỳ lúc nào.
Tác giả
Piyush Raj
Piyush là một chuyên gia dữ liệu đam mê sử dụng dữ liệu để hiểu mọi thứ tốt hơn và đưa ra quyết định sáng suốt. Trước đây, anh ấy từng là Nhà khoa học dữ liệu cho ZS và có bằng kỹ sư của IIT Roorkee. Sở thích của anh ấy bao gồm xem cricket, đọc sách và làm việc trong các dự án phụ
Làm cách nào để tách một chuỗi bao gồm cả khoảng trắng?
Để chia một chuỗi thành nhiều dấu cách, gọi phương thức split[], truyền cho nó một biểu thức chính quy, e. g. str. cắt []. tách[/\s+/] . Biểu thức chính quy sẽ chia chuỗi trên một hoặc nhiều khoảng trắng và trả về một mảng chứa các chuỗi con.Tách có xóa khoảng trắng Python không?
Phương pháp 1. Bằng cách sử dụng phương pháp dải[] . Những điểm chính. dải[]. Hàm này loại bỏ các khoảng trắng ở đầu và cuối khỏi một mảng bao gồm các tab [\ t]. lstrip[]. Hàm này loại bỏ khoảng trắng ở đầu bên trái của chuỗi. The split[] function basically removes the leading and the trailing spaces of the string. Key points: strip[]: This function removes the leading and the trailing spaces from an array including the tabs[\t]. lstrip[]: This function removes spaces from the left end of the string.