Hướng dẫn how to remove negative sign in python - làm thế nào để loại bỏ dấu hiệu phủ định trong python

8

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi muốn biết liệu có thể xóa dấu hiệu âm khỏi '{:,.2f}'.format(number) chỉ bằng định dạng.

Để có thể

'{:,.2f}'.format(10) ## 10

'{:,.2f}'.format(-10) ## 10

Cảm ơn trước

Hướng dẫn how to remove negative sign in python - làm thế nào để loại bỏ dấu hiệu phủ định trong python

Hỏi ngày 28 tháng 8 năm 2014 lúc 22:41Aug 28, 2014 at 22:41

Bạn không thể với

 '{:,.2f}'.format(abs(-10))
0 hoặc
 '{:,.2f}'.format(abs(-10))
1 một mình. Thay vào đó, sử dụng
 '{:,.2f}'.format(abs(-10))
2 trên số:

'{:,.2f}'.format(abs(value))

Đã trả lời ngày 28 tháng 8 năm 2014 lúc 22:41Aug 28, 2014 at 22:41

Martijn Pieters ♦ Martijn PietersMartijn Pieters

992K276 Huy hiệu vàng3906 Huy hiệu bạc3254 Huy hiệu đồng276 gold badges3906 silver badges3254 bronze badges

0

Sử dụng

 '{:,.2f}'.format(abs(-10))
3

 '{:,.2f}'.format(abs(-10))

Hoặc

 '{:,.2f}'.format(abs(-10))
4:

num = -10
print '{:,.2f}'.format(num).lstrip("-")
10.00

Or:

num = -10
print 'Your number is: {:,.2f}'.format(num).replace("-","")

Đã trả lời ngày 28 tháng 8 năm 2014 lúc 22:42Aug 28, 2014 at 22:42

Hướng dẫn how to remove negative sign in python - làm thế nào để loại bỏ dấu hiệu phủ định trong python

2

Đôi khi, trong khi làm việc với danh sách Python, chúng ta có thể gặp vấn đề trong đó chúng ta cần xóa tất cả các yếu tố tiêu cực khỏi danh sách. Loại vấn đề này có thể có ứng dụng trong nhiều lĩnh vực như lập trình trường học và phát triển web. 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.

Đầu vào: test_list = [6, 4, 3] Đầu ra: [6, 4, 3] Đầu vào: test_list = [-6, -4] Đầu ra: [] : test_list = [6, 4, 3] Output : [6, 4, 3] Input : test_list = [-6, -4] Output : []

Phương pháp số 1: Sử dụng danh sách Hiểu biết kết hợp các hàm trên có thể được sử dụng để giải quyết vấn đề này. Trong đó, chúng tôi thực hiện nhiệm vụ loại bỏ các phần tử tiêu cực bằng cách lặp trong một lớp lót bằng cách sử dụng danh sách hiểu & nbsp; The combination of above functions can be used to solve this problem. In this, we perform the task of removing negative elements by iteration in one liner using list comprehension 

Python3

Các

num = -10
print 'Your number is: {:,.2f}'.format(num).replace("-","")
7
num = -10
print 'Your number is: {:,.2f}'.format(num).replace("-","")
8
num = -10
print 'Your number is: {:,.2f}'.format(num).replace("-","")
9
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
0
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
3
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
4

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
5
 '{:,.2f}'.format(abs(-10))
6
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
7
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
8
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
9
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
0
 '{:,.2f}'.format(abs(-10))
5
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2

num = -10
print 'Your number is: {:,.2f}'.format(num).replace("-","")
7
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
7
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
8
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
9
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
3
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2

Đầu ra: & nbsp;

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]

& nbsp; Phương pháp số 2: Sử dụng Filter () + lambda Sự kết hợp của các hàm trên cũng có thể cung cấp một giải pháp thay thế cho vấn đề này. Trong đó, chúng tôi mở rộng logic của việc giữ lại tích cực được hình thành bằng hàm lambda và mở rộng bằng Filter (). & NBSP;Method #2 : Using filter() + lambda The combination of above functions can also offer an alternative to this problem. In this, we extend logic of retaining positive formed using lambda function and extended using filter(). 

Python3

Các

num = -10
print 'Your number is: {:,.2f}'.format(num).replace("-","")
7
num = -10
print 'Your number is: {:,.2f}'.format(num).replace("-","")
8
num = -10
print 'Your number is: {:,.2f}'.format(num).replace("-","")
9
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
0
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
3
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
4

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
5
 '{:,.2f}'.format(abs(-10))
6
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
7
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
8
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
9
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
0
 '{:,.2f}'.format(abs(-10))
5
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2

num = -10
print 'Your number is: {:,.2f}'.format(num).replace("-","")
7
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
7
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
8
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
9
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
3
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2

Đầu ra: & nbsp;

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]

Phương thức số 3: Sử dụng danh sách (), map (), startswith () Phương thức

Python3

 '{:,.2f}'.format(abs(-10))
5
 '{:,.2f}'.format(abs(-10))
6
 '{:,.2f}'.format(abs(-10))
7
 '{:,.2f}'.format(abs(-10))
8
 '{:,.2f}'.format(abs(-10))
9
num = -10
print '{:,.2f}'.format(num).lstrip("-")
10.00
0
 '{:,.2f}'.format(abs(-10))
9
num = -10
print '{:,.2f}'.format(num).lstrip("-")
10.00
223
 '{:,.2f}'.format(abs(-10))
9
num = -10
print '{:,.2f}'.format(num).lstrip("-")
10.00
2226__19__19

num = -10
print 'Your number is: {:,.2f}'.format(num).replace("-","")
7'{:,.2f}'.format(-10) ## 106
 '{:,.2f}'.format(abs(-10))
36
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
3
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
4

Các

 '{:,.2f}'.format(abs(-10))
48
 '{:,.2f}'.format(abs(-10))
6
 '{:,.2f}'.format(abs(-10))
50

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
8
 '{:,.2f}'.format(abs(-10))
52
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
0
 '{:,.2f}'.format(abs(-10))
54'{:,.2f}'.format(-10) ## 106
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
4
 '{:,.2f}'.format(abs(-10))
31__

Các

 '{:,.2f}'.format(abs(-10))
72
 '{:,.2f}'.format(abs(-10))
73

num = -10
print 'Your number is: {:,.2f}'.format(num).replace("-","")
7'{:,.2f}'.format(-10) ## 106
 '{:,.2f}'.format(abs(-10))
76
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
3
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
2

Đầu ra

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]


Làm thế nào để bạn thoát khỏi một tiêu cực trong Python?

Trong Python, các số dương có thể được thay đổi thành số âm với sự trợ giúp của phương thức được xây dựng được cung cấp trong thư viện Python có tên ABS ().Khi ABS () được sử dụng, nó chuyển đổi số âm thành dương.abs (). When abs () is used, it converts negative numbers to positive.

Làm thế nào để bạn thay đổi một số âm thành dương trong Python?

Để chuyển đổi số âm đó thành số dương, chúng ta có thể sử dụng abs ().Mã của chúng tôi trả về: 11.Nếu chúng tôi chưa chỉ định ABS (), chương trình của chúng tôi đã trả về -11 vì sự khác biệt giữa 20 và 31 là -11.Tuy nhiên, vì chúng tôi đã sử dụng abs (), chương trình của chúng tôi đã trả về một giá trị tuyệt đối.

Làm thế nào để bạn chỉ nhận được các giá trị tích cực trong Python?

Hàm abs () được sử dụng để có được giá trị tuyệt đối (dương) của một số nhất định.abs() function is used to get the absolute (positive) value of a given number.