Python tách chuỗi bằng dấu cách hoặc dấu phẩy

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

Sau đâ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]]
6

Hã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]]
1

Bạ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]]
4

Danh sách kết quả chỉ có ba chuỗi

Bạn cũng có thể quan tâm đến -

  1. Python – Tách chuỗi bằng dấu gạch dưới
  2. Python – Xóa nhiều khoảng trắng khỏi một chuỗi
  3. 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ả

Chủ Đề