Hướng dẫn can we convert string to list in python? - chúng ta có thể chuyển đổi chuỗi thành 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.

Đưa ra một bộ, viết một chương trình Python để chuyển đổi tập hợp đã cho thành danh sách. Cách tiếp cận số 1: Sử dụng danh sách [set_name]. Typecasting vào danh sách có thể được thực hiện bằng cách sử dụng danh sách [set_name]. Sử dụng hàm Sắp xếp [] sẽ chuyển đổi tập hợp thành danh sách theo thứ tự được xác định.string.

string.split["delimiter"]

Examples:

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

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.

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.

Python3

Ví dụ 1a:

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

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

Python3

Ví dụ 1a:

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

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
Using string slicing

Python3

Ví dụ 1a:

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

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

Ví dụ 1b:

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

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

Ví dụ 1a:

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

['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']
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
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-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']
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

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

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
['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-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
Using JSON

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

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 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
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
Using ast.literal

Python3

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

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;

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

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; 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

Các

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 cách nào để chuyển đổi một chuỗi thành một danh sách trong 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 chuỗi?

Bạn có thể kết hợp một danh sách các chuỗi vào một chuỗi với phương thức chuỗi, hãy nối [].Gọi phương thức tham gia [] từ 'chuỗi vào chèn' và truyền [danh sách các chuỗi].Nếu bạn sử dụng một chuỗi trống '', [Danh sách các chuỗi] chỉ đơn giản là được nối và nếu bạn sử dụng dấu phẩy, thì nó sẽ tạo ra một chuỗi được phân phối bằng dấu phẩy.with the string method, join[] . Call the join[] method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.

Bạn có thể chuyển đổi danh sách thành một chuỗi không?

Để chuyển đổi danh sách thành một chuỗi, hãy sử dụng khả năng hiểu danh sách Python và hàm tham gia [].Sự hiểu biết danh sách sẽ đi qua từng phần tử một và phương thức tham gia [] sẽ kết hợp các phần tử của danh sách thành một chuỗi mới và trả về nó làm đầu ra.use Python List Comprehension and the join[] function. The list comprehension will traverse the elements one by one, and the join[] method will concatenate the list's elements into a new string and return it as output.

Làm thế nào để bạn chuyển đổi dữ liệu thành một danh sách trong Python?

Đưa ra một bộ, viết một chương trình Python để chuyển đổi tập hợp đã cho thành danh sách.Cách tiếp cận số 1: Sử dụng danh sách [set_name].Typecasting vào danh sách có thể được thực hiện bằng cách sử dụng danh sách [set_name].Sử dụng hàm Sắp xếp [] sẽ chuyển đổi tập hợp thành danh sách theo thứ tự được xác định.Using list[set_name] . Typecasting to list can be done by simply using list[set_name] . Using sorted[] function will convert the set into list in a defined order.

Bài Viết Liên Quan

Chủ Đề