Chia một từ thành danh sách các chữ cái trong Python #
Sử dụng lớp Copied! Copied! Copied!
4 để chia một từ thành danh sách các chữ cái, ví dụ: my_str = 'hello'
my_list = [letter for letter in my_str]
# 👇️ ['h', 'e', 'l', 'l', 'o']
print[my_list]
5. Lớp 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.my_str = 'hello'
my_list = [letter for letter in my_str]
# 👇️ ['h', 'e', 'l', 'l', 'o']
print[my_list]
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!
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.my_str = 'hello'
my_list = [letter for letter in my_str]
# 👇️ ['h', 'e', 'l', 'l', 'o']
print[my_list]
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!
8 trên mỗi chữ cái và xem kết quả có phải là sự thật không.my_str = 'hello'
my_list = [letter for letter in my_str]
# 👇️ ['h', 'e', 'l', 'l', 'o']
print[my_list]
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
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 = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
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
9 để lặp qua chuỗi và sử dụng phương thứcCopied!
my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
1 để thêm mỗi chữ cái vào danh sách.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]
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ề
2 khi nó đột biến danh sách ban đầu.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]
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
4, nhưng cũng có ít nhất một tùy chọn khác:Copied!
my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
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
4Copied!
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]
5Copied!
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]
6Copied!
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]
7Copied!
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]
8Copied!
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]
9Copied!
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]
0Copied!
my_str = 'hello' my_list = [] for letter in my_str: my_list.append[letter] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
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
4Copied!
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]
5Copied!
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]
3Copied!
my_str = 'hello' my_list = [] for letter in my_str: my_list.append[letter] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
4Copied!
my_str = 'hello' my_list = [] for letter in my_str: my_list.append[letter] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
5Copied!
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]
6Copied!
my_str = 'hello' my_list = [] for letter in my_str: my_list.append[letter] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
9Copied!
my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
8Copied!
my_str = 'hello' my_list = [] for letter in my_str: my_list.append[letter] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
9Copied!
my_str = 'hello' my_list = [] for letter in my_str: my_list.append[letter] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
0Copied!
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]
1Copied!
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]
2Copied!
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]
7Copied!
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]
4Copied!
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]
Output:
0Copied!
my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
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
4Copied!
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]
5Copied!
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]
7Copied!
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]
8Copied!
my_str = 'hello' my_list = [] for letter in my_str: my_list.append[letter] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
5Copied!
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]
s = "Word to Split"
wordlist = list[s] # option 1,
wordlist = [ch for ch in s] # option 2, list comprehension.
09Copied!
my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
s = "Word to Split"
wordlist = list[s] # option 1,
wordlist = [ch for ch in s] # option 2, list comprehension.
29Copied!
my_str = 'hello' my_list = [] for letter in my_str: my_list.append[letter] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
s = "Word to Split"
wordlist = list[s] # option 1,
wordlist = [ch for ch in s] # option 2, list comprehension.
47Copied!
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]
s = "Word to Split"
wordlist = list[s] # option 1,
wordlist = [ch for ch in s] # option 2, list comprehension.
6Output:
1Copied!
my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
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.
81Copied!
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]
['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']
35Copied!
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]
['W','o','r','d',' ','t','o',' ','S','p','l','i','t']
57Copied!
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]
['W','o','r','d',' ','t','o',' ','S','p','l','i','t']
7Output:
['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
4Copied!
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]
5Copied!
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]
[doSomethingWith[ch] for ch in s]
04Copied!
my_str = 'hello' my_list = [] for letter in my_str: my_list.append[letter] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
5Copied!
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]
6Copied!
my_str = 'hello' my_list = [] for letter in my_str: my_list.append[letter] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]
[doSomethingWith[ch] for ch in s]
47Copied!
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]
4Copied!
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]
Output:
3Copied!
my_str = 'hello' my_list = [letter for letter in my_str] # 👇️ ['h', 'e', 'l', 'l', 'o'] print[my_list]