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ỏ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 Pieters♦Martijn 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
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["-",""]
7num = -10
print 'Your number is: {:,.2f}'.format[num].replace["-",""]
8num = -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]]
5The 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["-",""]
7The 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["-",""]
7num = -10
print 'Your number is: {:,.2f}'.format[num].replace["-",""]
8num = -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]]
5The 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["-",""]
7The 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]]
9num = -10
print '{:,.2f}'.format[num].lstrip["-"]
10.00
0 '{:,.2f}'.format[abs[-10]]
9num = -10
print '{:,.2f}'.format[num].lstrip["-"]
10.00
223 '{:,.2f}'.format[abs[-10]]
9num = -10
print '{:,.2f}'.format[num].lstrip["-"]
10.00
2226__19__19num = -10
print 'Your number is: {:,.2f}'.format[num].replace["-",""]
7'{:,.2f}'.format[-10] ## 10
6 '{:,.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]]
50The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]8
'{:,.2f}'.format[abs[-10]]
52The 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] ## 10
6The 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]]
73num = -10
print 'Your number is: {:,.2f}'.format[num].replace["-",""]
7'{:,.2f}'.format[-10] ## 10
6 '{:,.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]