Hướng dẫn can you cast a string to a list in python? - bạn có thể truyền một chuỗi vào danh sách trong python không?

Trong hướng dẫn ngắn này, hãy tìm cách chuyển đổi chuỗi thành liệt kê trong Python. Chúng tôi xem xét tất cả các cách bạn có thể đạt được điều này cùng với ưu và nhược điểm của họ.

Mục lục - Chuyển đổi chuỗi thành danh sách trong Python

  • Chuyển đổi chuỗi thành danh sách trong Python
  • Giải pháp 1: Sử dụng Split []
  • Giải pháp 2: Sử dụng Danh sách []
  • Hạn chế và cảnh báo

Chuyển đổi chuỗi thành danh sách trong Python:

Chuyển đổi kiểu dữ liệu hoặc đúc loại trong Python là một thực tế rất phổ biến. Tuy nhiên, việc chuyển đổi chuỗi thành liệt kê trong Python không đơn giản như chuyển đổi INT thành chuỗi hoặc ngược lại.

Chuỗi có thể được chuyển đổi thành danh sách bằng danh sách []. Chúng tôi sẽ xem xét phương pháp này dưới đây. Tuy nhiên, trong phương pháp này, Python sẽ không biết mỗi mục bắt đầu và kết thúc ở đâu, trả lại một danh sách các ký tự. Do đó, Python cung cấp một vài phương pháp thay thế có thể được sử dụng để chuyển đổi chuỗi thành danh sách.

Giải pháp 1: Sử dụng Split []

Giải pháp 2: Sử dụng Danh sách []

Syntax:

Hạn chế và cảnh báo

Parameters:

  • Chuyển đổi chuỗi thành danh sách trong Python: - Optional. Specifies the desired delimiter to use when splitting the string. If left empty, whitespaces are considered delimiters.
  • Chuyển đổi kiểu dữ liệu hoặc đúc loại trong Python là một thực tế rất phổ biến. Tuy nhiên, việc chuyển đổi chuỗi thành liệt kê trong Python không đơn giản như chuyển đổi INT thành chuỗi hoặc ngược lại. - Optional. Specifies how many splits to do.

Chuỗi có thể được chuyển đổi thành danh sách bằng danh sách []. Chúng tôi sẽ xem xét phương pháp này dưới đây. Tuy nhiên, trong phương pháp này, Python sẽ không biết mỗi mục bắt đầu và kết thúc ở đâu, trả lại một danh sách các ký tự. Do đó, Python cung cấp một vài phương pháp thay thế có thể được sử dụng để chuyển đổi chuỗi thành danh sách.

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']

Phương thức phân chia được sử dụng để phân chia một chuỗi dựa trên một dấu phân cách được chỉ định. Sau khi chia, nó trả về chuỗi phân chia trong danh sách, sử dụng phương thức này, chúng ta có thể chuyển đổi chuỗi thành danh sách trong Python. Since no argument was passed as a delimiter, the string was split on whitespace.

string.split[ delimiter, maxsplit]

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']

Giải pháp 2: Sử dụng Danh sách []

Hạn chế và cảnh báo

Chuyển đổi chuỗi thành danh sách trong Python:

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']

Chuyển đổi kiểu dữ liệu hoặc đúc loại trong Python là một thực tế rất phổ biến. Tuy nhiên, việc chuyển đổi chuỗi thành liệt kê trong Python không đơn giản như chuyển đổi INT thành chuỗi hoặc ngược lại.

Chuỗi có thể được chuyển đổi thành danh sách bằng danh sách []. Chúng tôi sẽ xem xét phương pháp này dưới đây. Tuy nhiên, trong phương pháp này, Python sẽ không biết mỗi mục bắt đầu và kết thúc ở đâu, trả lại một danh sách các ký tự. Do đó, Python cung cấp một vài phương pháp thay thế có thể được sử dụng để chuyển đổi chuỗi thành danh sách.

Trong chương trình này, chúng tôi sẽ cố gắng chuyển đổi một chuỗi nhất định thành một danh sách, trong đó không gian hoặc bất kỳ ký tự đặc biệt nào khác, theo lựa chọn của người dùng, gặp phải. Để làm điều này, chúng tôi sử dụng phương thức chia [] trong chuỗi.string.

string.split["delimiter"]

Examples:

Input : "Geeks for Geeks"
Output : ['Geeks', 'for', 'Geeks']
Input : "Geeks-for-Geeks"
Output : ['Geeks', 'for', 'Geeks']

Phương pháp số 1: Sử dụng phương thức Split []

Phương pháp phân chia được sử dụng để phân chia các chuỗi và lưu trữ chúng trong danh sách. Phương thức tích hợp trả về một danh sách các từ trong chuỗi, sử dụng Delimiter, làm chuỗi phân cách. Nếu một dấu phân cách không được chỉ định hoặc không có, một thuật toán phân tách khác được áp dụng: Các lần chạy khoảng trắng liên tiếp được coi là một dấu phân cách duy nhất và kết quả sẽ không chứa các chuỗi trống ở đầu hoặc kết thúc nếu chuỗi có khoảng trắng dẫn đầu hoặc dấu vết.

Ví dụ 1a:

Python3

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
8
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
9

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
1
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
3
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
4
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
5
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
6

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
8
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
9

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
0____22
str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
2

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
3
str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
4

Đầu ra

['Geeks', 'for', 'Geeks']

Ví dụ 1b:

Python3

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
8
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
9

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
1
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
3
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
4
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
5
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
6

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
8
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
9

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
0____22
str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
2

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
3
str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
4

Đầu ra

['Geeks', 'for', 'Geeks']

Ví dụ 1b:Using string slicing

Python3

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
8
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
9

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
1
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
3
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
4
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
5
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
6

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
8
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
9

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
0____22
str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
2

Ví dụ 1b:

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
3
str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
4

Đầu ra

['A', 'B', 'C', 'D']

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
1
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
3
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
4
string.split["delimiter"]
2
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
6

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
string.split["delimiter"]
9

Python3

Phương pháp số 2: Sử dụng cắt chuỗi

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
8
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
9

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
1
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
3
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
4
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
5
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
6

['Geeks', 'for', 'Geeks']
1
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
Input : "Geeks-for-Geeks"
Output : ['Geeks', 'for', 'Geeks']
9

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
3
['Geeks', 'for', 'Geeks']
5
['Geeks', 'for', 'Geeks']
6
['Geeks', 'for', 'Geeks']
7

Đầu ra

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
8
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
9
Using list comprehension 

Python3

['Geeks', 'for', 'Geeks']
8
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
['A', 'B', 'C', 'D']
0

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
0____22
str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
2

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
3
['A', 'B', 'C', 'D']
9

Đầu ra

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
1

Ví dụ 1b: Using enumerate function 

Python3

['Geeks', 'for', 'Geeks']
8
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
02

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
1
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
3
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
4
string.split["delimiter"]
2
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
6

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
3
['A', 'B', 'C', 'D']
9

Đầu ra

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
string.split["delimiter"]
9
Using JSON

Python3

Phương pháp số 2: Sử dụng cắt chuỗi

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
Input : "Geeks for Geeks"
Output : ['Geeks', 'for', 'Geeks']
55____22
Input : "Geeks for Geeks"
Output : ['Geeks', 'for', 'Geeks']
7

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
Input : "Geeks for Geeks"
Output : ['Geeks', 'for', 'Geeks']
9
Input : "Geeks-for-Geeks"
Output : ['Geeks', 'for', 'Geeks']
0
Input : "Geeks-for-Geeks"
Output : ['Geeks', 'for', 'Geeks']
1
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
Input : "Geeks-for-Geeks"
Output : ['Geeks', 'for', 'Geeks']
3

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
3
['Geeks', 'for', 'Geeks']
5
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
23
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
24

Đầu ra

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
3

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
8
Input : "Geeks-for-Geeks"
Output : ['Geeks', 'for', 'Geeks']
6
Using ast.literal

Python3

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
0____22
Input : "Geeks-for-Geeks"
Output : ['Geeks', 'for', 'Geeks']
9

Phương thức số 3: Sử dụng re.findall [] phương thức & nbsp;

Nhiệm vụ này có thể được thực hiện bằng cách sử dụng biểu thức thông thường. Chúng ta có thể sử dụng mẫu để khớp với tất cả bảng chữ cái và lập danh sách với tất cả các yếu tố phù hợp. & NBSP;

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
3
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
34

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
3
['Geeks', 'for', 'Geeks']
5
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
37
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
38

Đầu ra

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
4

['Geeks', 'for', 'Geeks']
2
['Geeks', 'for', 'Geeks']
3
Using lambda function

Python3

['Geeks', 'for', 'Geeks']
8
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
['A', 'B', 'C', 'D']
0

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
0
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
8
['Geeks', 'for', 'Geeks']
8
['Geeks', 'for', 'Geeks']
9
['Geeks', 'for', 'Geeks']
0

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
3
['A', 'B', 'C', 'D']
9

Đầu ra

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
1

Phương pháp số 9: Sử dụng Map [] & NBSP; Using map[] 

Python3

['Geeks', 'for', 'Geeks']
8
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
['A', 'B', 'C', 'D']
0

['A', 'B', 'C', 'D']
1
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
3
['Geeks', 'for', 'Geeks']
5
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
61
['Geeks', 'for', 'Geeks']
5
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
63
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
64

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
3
['A', 'B', 'C', 'D']
9

Đầu ra

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
1

Phương pháp số 10: Sử dụng Danh sách []Using list[]

Python3

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
67
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
['A', 'B', 'C', 'D']
0

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
70
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
2
str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split["-"]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
3
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
73

str_1 = "Hire freelance developers"


list_1 = list[str_1.strip[" "]]
print[list_1]

#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']
3
['A', 'B', 'C', 'D']
9

Output:

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split[]
print[list_1]

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
1

Làm thế nào để bạn phân tích một chuỗi vào một danh sách trong Python?

Trong bài viết này, chúng tôi sẽ xem cách chuyển đổi một chuỗi thành một danh sách ...
Với dải và tách. Trước tiên chúng tôi áp dụng phương pháp dải để loại bỏ các dấu ngoặc vuông và sau đó áp dụng chức năng phân chia. Chức năng phân chia với dấu phẩy vì tham số của nó tạo danh sách từ chuỗi. ....
Với JSON. tải. ....
Với AST. literal_eval ..

Làm cách nào để lưu trữ một chuỗi trong danh sách Python?

Để làm điều này, chúng tôi sử dụng phương thức chia [] trong chuỗi.Phương pháp phân chia được sử dụng để phân chia các chuỗi và lưu trữ chúng trong danh sách.Phương thức tích hợp trả về một danh sách các từ trong chuỗi, sử dụng Delimiter, làm chuỗi phân cách.use the split[] method in string. The split method is used to split the strings and store them in the list. The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string.

Làm cách nào để chuyển đổi một chuỗi thành danh sách các ký tự?

Approach:..
Nhận chuỗi ..
Tạo một danh sách các ký tự ..
Chuyển đổi thành chuỗi thành intStream bằng phương thức chars [] ..
Chuyển đổi IntStream thành phát trực tuyến bằng phương thức maptoobj [] ..
Thu thập các yếu tố làm danh sách các ký tự bằng cách sử dụng Sưu tầm [].
Trả lại danh sách ..

Làm cách nào để chuyển đổi một chuỗi thành một danh sách mà không cần chia trong Python?

Sử dụng phương thức str.Split [] để chia chuỗi thành một danh sách các chuỗi.Sử dụng hàm map [] để chuyển đổi từng chuỗi thành một số nguyên.Sử dụng lớp Danh sách [] để chuyển đổi đối tượng MAP thành danh sách. split[] method to split the string into a list of strings. Use the map[] function to convert each string into an integer. Use the list[] class to convert the map object to a list.

Chủ Đề