Phân tách python và chuyển thành int

Trong hướng dẫn ngắn này, hãy tìm cách chuyển đổi chuỗi thành danh sách 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 chúng

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à Hãy cẩn thận

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

Chuyển đổi kiểu dữ liệu hoặc truyền kiểu trong Python là một cách rất phổ biến. Tuy nhiên, chuyển string thành list trong Python không đơn giản như chuyển int thành string hoặc ngược lại

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

Giải pháp 1. Sử dụng Split()

Phương thức split được sử dụng để tách một chuỗi dựa trên dấu phân cách đã chỉ định. Sau khi tách, nó trả về chuỗi đã tách trong một 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

cú pháp

string.split( delimiter, maxsplit)

Thông số

  • Dấu phân cách - Tùy chọn. Chỉ định dấu phân cách mong muốn để sử dụng khi tách chuỗi. Nếu để trống, khoảng trắng được coi là dấu phân cách
  • Maxsplit - Tùy chọn. Chỉ định có bao nhiêu phần chia để làm

Mã để chuyển đổi chuỗi thành danh sách trong Python

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

Ghi chú. Vì không có đối số nào được chuyển dưới dạng dấu phân cách nên chuỗi được chia thành khoảng trắng

Bây giờ, hãy xem một ví dụ trong đó dấu phân cách được chỉ định

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

Như đã nói ở trên, phương thức này chuyển đổi một chuỗi thành một danh sách các ký tự. Do đó phương pháp này không được sử dụng khá thường xuyên. Tôi khuyên bạn chỉ nên sử dụng phương pháp này nếu bạn chắc chắn rằng danh sách phải chứa mỗi ký tự dưới dạng một mục và nếu chuỗi chứa một tập hợp các ký tự hoặc số không được chia bởi khoảng trắng. Nếu không, khoảng trắng cũng sẽ được coi là một ký tự và được lưu trữ trong danh sách

Mã để 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 chuỗi thành danh sách trong Python - Suy nghĩ kết thúc

Phương thức split() là phương thức được đề xuất và phổ biến nhất được sử dụng để chuyển đổi chuỗi thành danh sách trong Python. Phương pháp này không có bất kỳ nhược điểm đáng kể. Mặt khác, phương pháp typecast không được khuyến nghị rộng rãi và chỉ được sử dụng khi đáp ứng các yêu cầu. Tuy nhiên, vui lòng thực hành cả hai phương pháp để tạo điều kiện hiểu rõ hơn về khái niệm này

Để chuyển đổi một chuỗi được phân tách bằng dấu cách thành một danh sách trong Python, hãy gọi phương thức

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

sentence = "This is a test"

words_list = sentence.split()

print(words_list)

đầu ra

['This', 'is', 'a', 'test']

Điều này hoạt động vì theo mặc định,

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
71 chia chuỗi theo khoảng trống

Sau đó, chúng ta hãy xem cách tách một chuỗi số nguyên thành một danh sách các số nguyên

Cách chuyển đổi chuỗi số nguyên được phân tách bằng dấu cách thành danh sách trong Python

Để chuyển đổi một chuỗi số nguyên được phân tách bằng dấu cách thành một danh sách trong Python

  1. Tách chuỗi trên các khoảng trống
  2. Chuyển đổi từng phần tử thành một số nguyên
  3. Thêm từng số nguyên vào danh sách

Bạn có thể làm điều này với một vòng lặp for

numbers_str = "1 2 3 4 5"

numbers_list = []

for num_str in numbers_str.split():
    num_int = int(num_str)
    numbers_list.append(num_int)

print(numbers_list)

đầu ra

[1, 2, 3, 4, 5]

Để làm cho biểu thức ngắn hơn, bạn có thể sử dụng cách hiểu danh sách

________số 8

đầu ra

[1, 2, 3, 4, 5]

Sự kết luận

Cảm ơn vì đã đọc. Tôi hy vọng bạn tìm thấy câu trả lời mà bạn đang tìm kiếm

Mã hóa vui vẻ

Đọc thêm

Thủ thuật Python

Danh sách hiểu Python

Trong chương trình này, chúng tôi sẽ cố gắng chuyển đổi một chuỗi đã cho thành một danh sách, trong đó bắt gặp khoảng trắng hoặc bất kỳ ký tự đặc biệt nào khác, tùy theo lựa chọn của người dùng. Để làm điều này chúng ta sử dụng phương thức split() trong 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

ví dụ

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

Phương pháp số 1. Sử dụng phương thức split()

Phương thức split được sử dụng để tách 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 “dấu phân cách” 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 là Không, một thuật toán phân tách khác sẽ được áp dụng. các 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 chuỗi trống ở đầu hoặc cuối nếu chuỗi có khoảng trắng ở đầu hoặc cuối

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

 

 

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

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

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

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

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
22
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']
17
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

 

 

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

list_1 = str_1.split("-")
print(list_1)

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

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

list_1 = str_1.split("-")
print(list_1)

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

Đầ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']
9

 

Phân tách python và chuyển thành int

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

 

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

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

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

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

list_1 = str_1.split("-")
print(list_1)

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
91
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']
17
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

 

 

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

list_1 = str_1.split("-")
print(list_1)

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

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

list_1 = str_1.split("-")
print(list_1)

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

Đầ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']
9

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

Python3




['This', 'is', 'a', 'test']
32

 

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
17
['This', 'is', 'a', 'test']
36____119
['This', 'is', 'a', 'test']
38

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
17
numbers_str = "1 2 3 4 5"

numbers_list = []

for num_str in numbers_str.split():
    num_int = int(num_str)
    numbers_list.append(num_int)

print(numbers_list)
10____611
numbers_str = "1 2 3 4 5"

numbers_list = []

for num_str in numbers_str.split():
    num_int = int(num_str)
    numbers_list.append(num_int)

print(numbers_list)
12
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
19
numbers_str = "1 2 3 4 5"

numbers_list = []

for num_str in numbers_str.split():
    num_int = int(num_str)
    numbers_list.append(num_int)

print(numbers_list)
14

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
25
['This', 'is', 'a', 'test']
36

 

 

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
19
[1, 2, 3, 4, 5]
21

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

list_1 = str_1.split("-")
print(list_1)

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

Đầu ra

['This', 'is', 'a', 'test']
3

Phương pháp số 3. Sử dụng lại. phương thức findall()

Nhiệm vụ này có thể được thực hiện bằng biểu thức chính quy. Chúng ta có thể sử dụng mẫu để khớp tất cả các bảng chữ cái và tạo danh sách với tất cả các phần tử được khớp.  

Python3




['This', 'is', 'a', 'test']
32

[1, 2, 3, 4, 5]
25

[1, 2, 3, 4, 5]
26
[1, 2, 3, 4, 5]
27

 

[1, 2, 3, 4, 5]
28

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
25
numbers_str = "1 2 3 4 5"

numbers_list = [int(num) for num in numbers_str.split()]

print(numbers_list)
53______854
numbers_str = "1 2 3 4 5"

numbers_list = [int(num) for num in numbers_str.split()]

print(numbers_list)
55

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

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

list_1 = str_1.split("-")
print(list_1)

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
91____942____943
[1, 2, 3, 4, 5]
44

Đầu ra

numbers_str = "1 2 3 4 5"

numbers_list = []

for num_str in numbers_str.split():
    num_int = int(num_str)
    numbers_list.append(num_int)

print(numbers_list)
1

Phương pháp #4. Sử dụng hiểu danh sách

Python3




[1, 2, 3, 4, 5]
45
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

[1, 2, 3, 4, 5]
48
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

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

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

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

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

list_1 = str_1.split("-")
print(list_1)

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

Đầu ra

[1, 2, 3, 4, 5]
2

Phương pháp #5. Sử dụng hàm liệt kê

Python3




[1, 2, 3, 4, 5]
45
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

[1, 2, 3, 4, 5]
48
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

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

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

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

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

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

list_1 = str_1.split("-")
print(list_1)

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

Đầu ra

numbers_str = "1 2 3 4 5"

numbers_list = [int(num) for num in numbers_str.split()]

print(numbers_list)
5

Phương pháp #6. Sử dụng JSON

Python3




[1, 2, 3, 4, 5]
26
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

 

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

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

 

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

 

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

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

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

list_1 = str_1.split("-")
print(list_1)

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

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

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

Đầu ra

[1, 2, 3, 4, 5]
4

Phương pháp số 7. sử dụng ast. nghĩa đen

Python3




[1, 2, 3, 4, 5]
26
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

 

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

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

 

 

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

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

 

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

list_1 = str_1.split("-")
print(list_1)

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

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

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

list_1 = str_1.split("-")
print(list_1)

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
91
[1, 2, 3, 4, 5]
42
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

Đầ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']
10

Phương pháp số 8. Sử dụng hàm lambda

Python3




[1, 2, 3, 4, 5]
45
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

[1, 2, 3, 4, 5]
48
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
19_______120
[1, 2, 3, 4, 5]
42
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']
158
[1, 2, 3, 4, 5]
42
str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)

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

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

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

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

Bạn có thể truyền một chuỗi thành một int trong Python không?

Để chuyển đổi hoặc ép kiểu chuỗi thành số nguyên trong Python, bạn sử dụng hàm tích hợp int() . Hàm nhận tham số là chuỗi ban đầu bạn muốn chuyển đổi và trả về số nguyên tương đương với giá trị bạn đã truyền.

Bạn có thể chia số nguyên trong Python không?

Chia một số nguyên thành các chữ số. Sử dụng lớp str() để chuyển đổi số nguyên thành chuỗi. Sử dụng khả năng hiểu danh sách để lặp qua chuỗi. Trên mỗi lần lặp lại, hãy sử dụng lớp int() để chuyển đổi mỗi chuỗi con thành một số nguyên .