Hướng dẫn how do i split a word into separate letters in python? - làm cách nào để tách một từ thành các chữ cái riêng biệt trong python?

Chia một từ thành danh sách các chữ cái trong Python #

Sử dụng lớp

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
4 để chia một từ thành danh sách các chữ cái, ví dụ:

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
5. Lớp

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
4 sẽ chuyển đổi chuỗi thành một danh sách các chữ cái.

Copied!

my_str = 'hello' my_list = list(my_str) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)

Lớp danh sách có một sự lặp lại và trả về một đối tượng danh sách.

Khi một chuỗi được chuyển đến lớp, nó phân tách chuỗi trên mỗi ký tự và trả về một danh sách chứa các ký tự.

Một cách tiếp cận khác là sử dụng một danh sách hiểu.

Sử dụng danh sách hiểu để chia một từ vào danh sách các chữ cái, ví dụ:

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
7. Danh sách các hệ thống được sử dụng để thực hiện một số hoạt động cho mọi yếu tố hoặc chọn một tập hợp con của các phần tử đáp ứng một điều kiện.

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)

Bạn cũng có thể lọc các chữ cái ra khỏi danh sách cuối cùng khi sử dụng phương pháp này.

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)

Chuỗi trong ví dụ có khoảng trắng.

Thay vì nhận các mục trong danh sách có chứa một không gian, chúng tôi gọi phương thức

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
8 trên mỗi chữ cái và xem kết quả có phải là sự thật không.

Phương thức str.strip trả về một bản sao của chuỗi với khoảng trắng dẫn đầu và dấu vết đã bị loại bỏ.

Nếu chuỗi lưu trữ một không gian, nó sẽ bị loại khỏi danh sách cuối cùng.

Bạn cũng có thể sử dụng một vòng lặp

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
9 đơn giản để chia một từ thành một danh sách các chữ cái.

Copied!

my_str = 'hello' my_list = [] for letter in my_str: my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)

Chúng tôi đã sử dụng vòng lặp

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
9 để lặp qua chuỗi và sử dụng phương thức

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
1 để thêm mỗi chữ cái vào danh sách.

Phương thức Danh sách.Append () thêm một mục vào cuối danh sách.

Phương thức trả về

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
2 khi nó đột biến danh sách ban đầu.

Bạn cũng có thể thêm thư vào danh sách.

Copied!

my_str = 'hello' my_list = [] for letter in my_str: if letter.strip() != '': my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)

Chuỗi chỉ được thêm vào danh sách nếu nó không phải là một không gian.

Cách dễ nhất có lẽ chỉ là sử dụng

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
4, nhưng cũng có ít nhất một tùy chọn khác:

s = "Word to Split"
wordlist = list(s)               # option 1, 
wordlist = [ch for ch in s]      # option 2, list comprehension.

Cả hai nên cung cấp cho bạn những gì bạn cần:

['W','o','r','d',' ','t','o',' ','S','p','l','i','t']

Như đã nêu, lần đầu tiên có khả năng thích hợp nhất cho ví dụ của bạn nhưng có những trường hợp sử dụng có thể làm cho cái sau khá tiện dụng cho những thứ phức tạp hơn, chẳng hạn như nếu bạn muốn áp dụng một số chức năng tùy ý cho các mục, chẳng hạn như với:

[doSomethingWith(ch) for ch in s]

Đưa ra một chuỗi, hãy viết một chương trình Python để chia các ký tự của chuỗi đã cho thành một danh sách bằng Python.

Examples:    

Input : geeks
Output : ['g', 'e', 'e', 'k', 's']

Input : Word
Output : ['W', 'o', 'r', 'd']

Phương pháp 1: Chia một chuỗi thành danh sách Python bằng phương thức unpack (*)

Hành động giải nén liên quan đến việc loại bỏ mọi thứ, cụ thể là các vòng lặp như từ điển, danh sách và bộ dữ liệu.

Python3

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
4

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
5

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
6

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
7

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
8

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
9

Copied!

my_str = 'hello' my_list = [] for letter in my_str: my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
0

Output:  

['g', 'e', 'e', 'k', 's']

Phương pháp 2: Chia một chuỗi thành danh sách Python bằng cách sử dụng vòng lặp

Ở đây, chúng tôi đang chia các chữ cái bằng cách sử dụng bản địa bằng cách sử dụng vòng lặp và sau đó chúng tôi đang nối nó vào một danh sách mới.

Python3

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
4

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
5

Copied!

my_str = 'hello' my_list = [] for letter in my_str: my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
3

Copied!

my_str = 'hello' my_list = [] for letter in my_str: my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
4

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
5

Copied!

my_str = 'hello' my_list = [] for letter in my_str: my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
6

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
9

Copied!

my_str = 'hello' my_list = [] for letter in my_str: my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
8

Copied!

my_str = 'hello' my_list = [] for letter in my_str: my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
9

Copied!

my_str = 'hello' my_list = [] for letter in my_str: if letter.strip() != '': my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
0

Copied!

my_str = 'hello' my_list = [] for letter in my_str: if letter.strip() != '': my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
1

Copied!

my_str = 'hello' my_list = [] for letter in my_str: if letter.strip() != '': my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
2

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
7

Copied!

my_str = 'hello' my_list = [] for letter in my_str: if letter.strip() != '': my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
4

Output:

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
0

Phương pháp 3: Chia một chuỗi thành danh sách python bằng cách sử dụng danh sách hiểu

Cách tiếp cận này sử dụng danh sách hiểu để chuyển đổi từng ký tự thành một danh sách. Sử dụng cú pháp sau, bạn có thể chia các ký tự của chuỗi thành danh sách.

Python3

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
4

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
5

Copied!

my_str = 'hello' my_list = [] for letter in my_str: if letter.strip() != '': my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
7

Copied!

my_str = 'hello' my_list = [] for letter in my_str: my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
8

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
5
s = "Word to Split"
wordlist = list(s)               # option 1, 
wordlist = [ch for ch in s]      # option 2, list comprehension.
0

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
9
s = "Word to Split"
wordlist = list(s)               # option 1, 
wordlist = [ch for ch in s]      # option 2, list comprehension.
2

Copied!

my_str = 'hello' my_list = [] for letter in my_str: my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
9
s = "Word to Split"
wordlist = list(s)               # option 1, 
wordlist = [ch for ch in s]      # option 2, list comprehension.
4

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
7
s = "Word to Split"
wordlist = list(s)               # option 1, 
wordlist = [ch for ch in s]      # option 2, list comprehension.
6

Output:  

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
1

Phương pháp 4: Chia một chuỗi thành danh sách Python bằng Danh sách () & NBSP; Typecasting

Python cung cấp đánh máy trực tiếp các chuỗi vào danh sách bằng danh sách Python ().

Python3

s = "Word to Split"
wordlist = list(s)               # option 1, 
wordlist = [ch for ch in s]      # option 2, list comprehension.
7
s = "Word to Split"
wordlist = list(s)               # option 1, 
wordlist = [ch for ch in s]      # option 2, list comprehension.
8

Copied!

my_str = 'hello' my_list = [] for letter in my_str: if letter.strip() != '': my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
1
['W','o','r','d',' ','t','o',' ','S','p','l','i','t']
0
['W','o','r','d',' ','t','o',' ','S','p','l','i','t']
1
['W','o','r','d',' ','t','o',' ','S','p','l','i','t']
2

['W','o','r','d',' ','t','o',' ','S','p','l','i','t']
3

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
5
['W','o','r','d',' ','t','o',' ','S','p','l','i','t']
5

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
7
['W','o','r','d',' ','t','o',' ','S','p','l','i','t']
7

Output:  

['g', 'e', 'e', 'k', 's']

Phương pháp 5: Chia một chuỗi thành danh sách Python bằng cách sử dụng Extend ()

Mở rộng lặp lại qua đầu vào của nó, mở rộng danh sách và thêm từng thành viên.

Python3

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
4

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
5
[doSomethingWith(ch) for ch in s]
0

Copied!

my_str = 'hello' my_list = [] for letter in my_str: my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
4

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
5

Copied!

my_str = 'hello' my_list = [] for letter in my_str: my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
6

[doSomethingWith(ch) for ch in s]
4

Copied!

my_str = 'h e l l o' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
7

Copied!

my_str = 'hello' my_list = [] for letter in my_str: if letter.strip() != '': my_list.append(letter) # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
4

Output:  

Copied!

my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print(my_list)
3

Làm thế nào để bạn chia từ thành các chữ cái trong Python?

Sử dụng lớp Danh sách () để chia một từ thành danh sách các chữ cái, ví dụ:my_list = list (my_str).Lớp danh sách () sẽ chuyển đổi chuỗi thành một danh sách các chữ cái., e.g. my_list = list(my_str) . The list() class will convert the string into a list of letters.

Làm thế nào để bạn chia nhiều từ trong Python?

Split () Đây là phương pháp hiệu quả nhất và thường được sử dụng để phân chia nhiều ký tự cùng một lúc.Nó sử dụng regex (biểu thức thông thường) để làm điều này. This is the most efficient and commonly used method to split multiple characters at once. It makes use of regex(regular expressions) in order to do this.

Làm cách nào để tách các từ khỏi chuỗi?

Phương thức Split () chia một chuỗi thành một mảng các chuỗi con.Phương thức chia () trả về mảng mới.Phương thức chia () không thay đổi chuỗi gốc.Nếu ("") được sử dụng làm dấu phân cách, chuỗi được phân chia giữa các từ.. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

Làm thế nào để bạn tách các ký tự trong một chuỗi?

Đầu tiên, xác định một chuỗi.Tiếp theo, tạo một vòng lặp trong đó biến vòng lặp sẽ bắt đầu từ INDEX 0 và kết thúc ở độ dài của chuỗi đã cho.In ký tự có ở mọi chỉ mục để tách từng ký tự riêng lẻ.Để trực quan hóa tốt hơn, tách từng ký tự riêng lẻ theo không gian.