Danh sách phân tách chuỗi python

Đầu tiên, tôi sẽ giới thiệu cho bạn cú pháp của phương thức

string.split[separator, maxsplit]
9. Sau đó, bạn sẽ thấy cách sử dụng phương thức
string.split[separator, maxsplit]
9 có và không có đối số, sử dụng các ví dụ mã trong quá trình thực hiện

Đây là những gì chúng tôi sẽ đề cập

  1. Cú pháp phương thức
    string.split[separator, maxsplit]
    
    9
    1. Làm thế nào để phương thức
      string.split[separator, maxsplit]
      
      9 hoạt động mà không có bất kỳ đối số nào?
    2. Phương thức
      string.split[separator, maxsplit]
      
      9 hoạt động như thế nào với đối số
      coding_journey = "I am learning to code for free with freeCodecamp!"
      
      # split string into a list and save result into a new variable
      coding_journey_split = coding_journey.split[]
      
      print[coding_journey]
      print[coding_journey_split]
      
      # check the data type of coding_journey_split by using the type[] function
      print[type[coding_journey_split]]
      
      # output
      # I am learning to code for free with freeCodecamp!
      # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
      # 
      
      
      2?
    3. Phương thức
      string.split[separator, maxsplit]
      
      9 hoạt động như thế nào với đối số
      coding_journey = "I am learning to code for free with freeCodecamp!"
      
      # split string into a list and save result into a new variable
      coding_journey_split = coding_journey.split[]
      
      print[coding_journey]
      print[coding_journey_split]
      
      # check the data type of coding_journey_split by using the type[] function
      print[type[coding_journey_split]]
      
      # output
      # I am learning to code for free with freeCodecamp!
      # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
      # 
      
      
      4?

Phương thức
string.split[separator, maxsplit]
9 trong Python là gì?

Bạn sử dụng phương pháp

string.split[separator, maxsplit]
9 để tách một chuỗi thành một danh sách

Cú pháp chung của phương thức

string.split[separator, maxsplit]
9 giống như sau

string.split[separator, maxsplit]

Hãy phá vỡ nó

  • coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split[]
    
    print[coding_journey]
    print[coding_journey_split]
    
    # check the data type of coding_journey_split by using the type[] function
    print[type[coding_journey_split]]
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    9 là chuỗi bạn muốn tách. Đây là chuỗi mà bạn gọi phương thức
    string.split[separator, maxsplit]
    
    9
  • Phương thức
    string.split[separator, maxsplit]
    
    9 chấp nhận hai đối số
  • Đối số tùy chọn đầu tiên là
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split[]
    
    print[coding_journey]
    print[coding_journey_split]
    
    # check the data type of coding_journey_split by using the type[] function
    print[type[coding_journey_split]]
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    2, chỉ định loại dấu tách nào sẽ được sử dụng để tách chuỗi. Nếu đối số này không được cung cấp, thì giá trị mặc định là bất kỳ khoảng trắng nào, nghĩa là chuỗi sẽ tách ra bất cứ khi nào
    string.split[separator, maxsplit]
    
    9 gặp bất kỳ khoảng trắng nào
  • Đối số tùy chọn thứ hai là
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split[]
    
    print[coding_journey]
    print[coding_journey_split]
    
    # check the data type of coding_journey_split by using the type[] function
    print[type[coding_journey_split]]
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    4, chỉ định số lượng phân tách tối đa mà phương thức
    string.split[separator, maxsplit]
    
    9 sẽ thực hiện. Nếu đối số này không được cung cấp, thì giá trị mặc định là
    coding_journey = "I love   coding"
    
    coding_journey_split = coding_journey.split[]
    
    print[coding_journey_split]
    
    # output
    # ['I', 'love', 'coding']
    
    6, nghĩa là không có giới hạn về số lần phân tách và
    string.split[separator, maxsplit]
    
    9 sẽ phân tách chuỗi trên tất cả các lần xuất hiện mà nó gặp phải
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split[]
    
    print[coding_journey]
    print[coding_journey_split]
    
    # check the data type of coding_journey_split by using the type[] function
    print[type[coding_journey_split]]
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # 
    
    
    2

Phương thức

string.split[separator, maxsplit]
9 trả về một danh sách chuỗi con mới và chuỗi ban đầu không bị sửa đổi theo bất kỳ cách nào

Phương pháp
string.split[separator, maxsplit]
9 hoạt động như thế nào mà không có bất kỳ đối số nào?

Đây là cách bạn chia một chuỗi thành một danh sách bằng cách sử dụng phương thức

string.split[separator, maxsplit]
9 mà không có bất kỳ đối số nào

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

Đầu ra cho thấy rằng mỗi từ tạo nên chuỗi hiện là một mục danh sách và chuỗi gốc được giữ nguyên

Khi bạn không chuyển một trong hai đối số mà phương thức

string.split[separator, maxsplit]
9 chấp nhận, thì theo mặc định, nó sẽ tách chuỗi mỗi khi gặp khoảng trắng cho đến khi chuỗi kết thúc

Điều gì xảy ra khi bạn không chuyển bất kỳ đối số nào cho phương thức

string.split[separator, maxsplit]
9 và nó gặp các khoảng trắng liên tiếp thay vì chỉ một?

coding_journey = "I love   coding"

coding_journey_split = coding_journey.split[]

print[coding_journey_split]

# output
# ['I', 'love', 'coding']

Trong ví dụ trên, tôi đã thêm các khoảng trắng liên tiếp giữa từ

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

04 và từ
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

05. Trong trường hợp này, phương thức
string.split[separator, maxsplit]
9 xử lý bất kỳ khoảng trắng liên tiếp nào như thể chúng là một khoảng trắng duy nhất

Phương pháp
string.split[separator, maxsplit]
9 hoạt động như thế nào với đối số
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

2?

Như bạn đã thấy trước đó, khi không có đối số

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

2, giá trị mặc định cho nó là khoảng trắng. Như đã nói, bạn có thể đặt một
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

2 khác

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

2 sẽ ngắt và chia chuỗi bất cứ khi nào gặp ký tự bạn chỉ định và sẽ trả về danh sách các chuỗi con

Ví dụ: bạn có thể làm cho chuỗi tách ra bất cứ khi nào phương thức

string.split[separator, maxsplit]
9 gặp dấu chấm,
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

63

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

0

Trong ví dụ trên, chuỗi tách ra bất cứ khi nào

string.split[separator, maxsplit]
9 gặp một
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

63

Hãy nhớ rằng tôi đã không chỉ định một dấu chấm theo sau bởi một khoảng trắng. Điều đó sẽ không hoạt động vì chuỗi không chứa dấu chấm theo sau là khoảng trắng

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

6

Bây giờ, hãy xem lại ví dụ cuối cùng từ phần trước

Khi không có đối số

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

2, các khoảng trắng liên tiếp được xử lý như thể chúng là các khoảng trắng đơn lẻ

Tuy nhiên, khi bạn chỉ định một khoảng trắng là

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

2, thì chuỗi sẽ tách ra mỗi khi gặp một ký tự khoảng trắng

string.split[separator, maxsplit]
2

Trong ví dụ trên, mỗi lần

string.split[separator, maxsplit]
9 gặp một ký tự khoảng trắng, nó sẽ tách từ đó ra và thêm khoảng trống dưới dạng một mục danh sách

Phương pháp
string.split[separator, maxsplit]
9 hoạt động như thế nào với đối số
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

4?

Khi không có đối số

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

4, sẽ không có giới hạn cụ thể về thời điểm ngừng phân tách

Trong ví dụ đầu tiên của phần trước,

string.split[separator, maxsplit]
9 tách chuỗi mỗi lần nó gặp
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

2 cho đến khi chạm đến cuối chuỗi

Tuy nhiên, bạn có thể chỉ định thời điểm bạn muốn quá trình phân tách kết thúc

Ví dụ: bạn có thể chỉ định rằng phần tách kết thúc sau khi nó gặp một dấu chấm

string.split[separator, maxsplit]
6

Trong ví dụ trên, tôi đặt

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split[]

print[coding_journey]
print[coding_journey_split]

# check the data type of coding_journey_split by using the type[] function
print[type[coding_journey_split]]

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# 

4 thành
string.split[separator, maxsplit]
25 và một danh sách được tạo với hai mục danh sách

Tôi đã chỉ định rằng danh sách sẽ tách ra khi gặp một dấu chấm. Khi nó gặp một dấu chấm, hoạt động sẽ kết thúc và phần còn lại của chuỗi sẽ là một mục danh sách của chính nó

Sự kết luận

Và bạn có nó rồi đấy. Bây giờ bạn đã biết cách tách một chuỗi trong Python bằng phương thức

string.split[separator, maxsplit]
9

Tôi hy vọng bạn thấy hướng dẫn này hữu ích

Để tìm hiểu thêm về ngôn ngữ lập trình Python, hãy xem chứng chỉ Python của freeCodeCamp

Bạn sẽ bắt đầu từ những điều cơ bản và học theo cách tương tác và thân thiện với người mới bắt đầu. Cuối cùng, bạn cũng sẽ xây dựng năm dự án để đưa vào thực tế và giúp củng cố những gì bạn đã học

Cảm ơn bạn đã đọc và chúc bạn mã hóa vui vẻ

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

Dionysia Lemonaki

Học một cái gì đó mới mỗi ngày và viết về nó

Nếu bài viết này hữu ích, hãy tweet nó

Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu

Bạn có thể tách bằng nhiều dấu phân cách Python không?

Tách chuỗi bằng nhiều dấu phân cách . Đối với điều này, chúng ta cần sử dụng mô-đun re của Python và nhập tệp re. chức năng tách []. We can also specify multiple characters as separators. For this, we need to make use of the re module of Python and import the re. split[] function.

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

Bạn có thể dễ dàng chia đôi danh sách Python bằng cách sử dụng lập chỉ mục danh sách . Như bạn đã học ở trên, bạn có thể chọn nhiều mục trong danh sách bằng cách sử dụng cắt danh sách.

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], .

Chủ Đề