Chúng tôi có ba phương pháp để chuyển đổi danh sách các chuỗi thành float bằng lập trình Python. Hãy để chúng tôi tiến hành xem mã và đầu ra cho từng phương thức
Sử dụng Python cho các vòng lặp
Sử dụng vòng lặp For, chúng tôi lặp qua tất cả các giá trị bên trong danh sách chuỗi, chuyển đổi từng giá trị thành float và lưu trữ các giá trị đã chuyển đổi thành một biến mới
Mã số
num_string = ["0.1", "0.2", "0.3", "0.4", "0.5"]
num_float = []
print["Before conversion: ",num_string]
for values in num_string:
num_float.append[float[values]]
print["Converted values: ",num_float]
print["Type: ",type[num_float[1]]]
đầu ra
Before conversion: ['0.1', '0.2', '0.3', '0.4', '0.5']
Converted values: [0.1, 0.2, 0.3, 0.4, 0.5]
Type:
Sử dụng lệnh bản đồ Python
Thay vì sử dụng vòng lặp for, chúng ta có thể sử dụng kỹ thuật ánh xạ để áp dụng một hàm cho tất cả các phần tử trong một dãy
Mã số
num_string = ["0.1", "0.2", "0.3", "0.4", "0.5"]
num_float = list[map[float, num_string]]
print["Before conversion: ",num_string]
print["Converted values: ",num_float]
print["Type: ",type[num_float[3]]]
đầu ra
Before conversion: ['0.1', '0.2', '0.3', '0.4', '0.5']
Converted values: [0.1, 0.2, 0.3, 0.4, 0.5]
Type:
Sử dụng Numpy [Gói phân tích dữ liệu của bên thứ ba]
Numpy là một gói nằm trong ngăn xếp Khoa học dữ liệu/Phân tích dữ liệu Python. NumPy được sử dụng để làm việc với mảng nhiều chiều. Trong ví dụ này, chúng tôi sẽ sử dụng Numpy để chuyển đổi các giá trị chuỗi thành float
Ghi chú
Trước khi chúng tôi sử dụng mã, trước tiên chúng tôi cần cài đặt gói NumPy để nhập gói vào mã. Bạn có thể cài đặt NumPy bằng tiện ích lệnh PIP. NumPy được phân phối theo mặc định với bản phân phối Anaconda
Đôi khi, trong khi làm việc với dữ liệu Python, chúng ta có thể gặp sự cố khi cần chuyển đổi một số float thành một danh sách các chữ số. Vấn đề này là phổ biến trong lập trình ngày. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.
Phương pháp số 1. Sử dụng các phương thức dựng sẵn
Python3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]2
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]4
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]5
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]6
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]7
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]0
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]1
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]2
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]4
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]5
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]6
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]8
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]6
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]5
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]1
_______42____76____44
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]5
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]6
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]7
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]8
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]9____40
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]1
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]2
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]9
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]0
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]6
_______47____48____49
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]20
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]21
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]1
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]2
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]24
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]4
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]5
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]27
Đầu ra
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]
Phương pháp #2. Sử dụng hiểu danh sách + isdigit[].
Có thể sử dụng tổ hợp các chức năng trên để thực hiện tác vụ này. Trong phần này, trước tiên chúng tôi chuyển đổi số float thành chuỗi và sau đó lặp lại nó chuyển đổi từng chữ số thành số nguyên và xây dựng danh sách bằng cách sử dụng khả năng hiểu danh sách.
Python3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]2
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]30
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]4
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]5
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]6
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]7
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]0
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]1
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]2
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]4
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]5
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]6
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]30
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]2
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]6
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]46
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]9
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]48
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]8
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]50
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]0
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]5
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]1
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]55
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]21
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]1
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]2
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]24
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]4
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]5
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]27
Đầu ra
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]
Phương pháp #3. Sử dụng map[] + biểu thức regex + findall[].
Có thể sử dụng tổ hợp các chức năng trên để thực hiện tác vụ này. Trong phần này, chúng tôi lặp qua danh sách bằng map[] và trích xuất và chuyển đổi từng phần tử của số float bằng biểu thức regex và findall[].
Python3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]2
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]65
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]66
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]67
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]4
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]5
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]6
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]7
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]0
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]1
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]2
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]4
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]5
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]6
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]3
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]65
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]2
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]6
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]03
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]2
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]05
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]2
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]9______408
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]09
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]10
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]5
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]12