Đây là một giải pháp khác cho vấn đề của bạn:
def to_bool[s]:
return 1 - sum[map[ord, s]] % 2
# return 1 - sum[s.encode['ascii']] % 2 # Alternative for Python 3
Nó hoạt động vì tổng của các mã ASCII của 'true'
là 448
, thậm chí, trong khi tổng của các mã ASCII của
def to_bool[s]:
return 1 if s == 'true' else 0
0 là def to_bool[s]:
return 1 if s == 'true' else 0
1 là kỳ lạ.Điều thú vị về giải pháp này là kết quả của nó là khá ngẫu nhiên nếu đầu vào không phải là một trong số 'true'
hoặc
def to_bool[s]:
return 1 if s == 'true' else 0
0. Một nửa thời gian nó sẽ trở lại def to_bool[s]:
return 1 if s == 'true' else 0
4 và nửa còn lại def to_bool[s]:
return 1 if s == 'true' else 0
5. Biến thể sử dụng def to_bool[s]:
return 1 if s == 'true' else 0
6 sẽ gây ra lỗi mã hóa nếu đầu vào không phải là ASCII [do đó làm tăng tính không xác định của hành vi].Nghiêm túc mà nói, tôi tin rằng giải pháp dễ đọc nhất và nhanh hơn là sử dụng
def to_bool[s]:
return 1 if s == 'true' else 0
7:def to_bool[s]:
return 1 if s == 'true' else 0
Xem một số microbenchmark:
In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
Lưu ý cách giải pháp
def to_bool[s]:
return 1 if s == 'true' else 0
7 nhanh hơn ít nhất 2,5 lần so với tất cả các giải pháp khác. Không có ý nghĩa gì khi đặt như một yêu cầu để tránh sử dụng def to_bool[s]:
return 1 if s == 'true' else 0
7s trừ khi đây là một loại bài tập về nhà [trong trường hợp đó bạn không nên hỏi điều này ngay từ đầu].2.5x times faster than all the other solutions. It does not make sense to put as a requirement to avoid using def to_bool[s]:
return 1 if s == 'true' else 0
7s except if this is some kind of homework
[in which case you shouldn't have asked this in the first place]. Chuyển đổi đúng và sai thành 1 và 0 trong Python #
Sử dụng lớp
0 để chuyển đổi In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
1 và In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
2 thành 1 và 0, ví dụ: In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
3. Lớp In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
0 sẽ trả về 1 cho các giá trị boolean In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
1 và 0 cho các giá trị In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
2.In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
Copied!
# ✅ convert True and False to 1 and 0 bool_t = True int_1 = int[bool_t] print[int_1] # 👉️ 1 bool_f = False int_0 = int[bool_f] print[int_0] # 👉️ 0 # ----------------------------------------------- # ✅ convert 'true' and 'false' to 1 and 0 str_t = 'true' int_1 = int[str_t.lower[] == 'true'] print[int_1] # 👉️ 1 str_f = 'false' int_0 = int[str_f.lower[] == 'true'] print[int_0] # 👉️ 0 # ----------------------------------------------- # ✅ convert list of 'true' and 'false' to 1 and 0 my_list = ['true', 'true', 'false'] result = [int[item.lower[] == 'true'] for item in my_list] print[result] # 👉️ [1, 1, 0]
Chúng tôi đã sử dụng lớp
In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
0 để chuyển đổi In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
1 thành 1 và In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
2 thành 0.Copied!
result_1 = int[True] print[result_1] # 👉️ 1 result_2 = int[False] print[result_2] # 👉️ 0
Lớp Int trả về một đối tượng số nguyên được xây dựng từ đối số được cung cấp.
Trình xây dựng trả về
def to_bool[s]:
return 1 if s == 'true' else 0
4 nếu không có đối số nào được đưa ra.
1 Giá trị Boolean Trả về In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
5 Sau khi chuyển đổi sang số nguyên và giá trị def to_bool[s]:
return 1 if s == 'true' else 0
2 Trả về In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
4.def to_bool[s]:
return 1 if s == 'true' else 0
Nếu bạn cần chuyển đổi danh sách các giá trị Boolean thành danh sách
def to_bool[s]:
return 1 if s == 'true' else 0
5 và def to_bool[s]:
return 1 if s == 'true' else 0
4, hãy sử dụng danh sách hiểu.Copied!
my_list = [True, True, False] result = [int[item] for item in my_list] print[result] # 👉️ [1, 1, 0]
Chúng tôi đã sử dụng một danh sách hiểu để lặp lại trong danh sách.
Danh sách các hệ thống được sử dụng để thực hiện một số hoạt động cho mọi yếu tố hoặc chọn một tập hợp con của các phần tử đáp ứng một điều kiện.
Trên mỗi lần lặp, chúng tôi chuyển đổi giá trị boolean hiện tại thành số nguyên và trả về kết quả.
Chuyển đổi 'true' và 'false' thành 1 và 0 trong python #
Để chuyển đổi các giá trị 'true' thành 1 và 'false' thành 0:
- Sử dụng toán tử bình đẳng để kiểm tra xem giá trị có bằng với chuỗi 'true' không.
- Chuyển đổi kết quả so sánh thành một số nguyên.
- Chuyển đổi sẽ trả về
5 cho các giá trị 'true' vàdef to_bool[s]: return 1 if s == 'true' else 0
4 cho các giá trị 'sai'.def to_bool[s]: return 1 if s == 'true' else 0
Copied!
str_t = 'true' int_1 = int[str_t.lower[] == 'true'] print[int_1] # 👉️ 1 str_f = 'false' int_0 = int[str_f.lower[] == 'true'] print[int_0] # 👉️ 0
Chúng tôi đã sử dụng phương thức
9 để chuyển đổi chuỗi thành chữ thường trước khi so sánh nó với chuỗiCopied!
# ✅ convert True and False to 1 and 0 bool_t = True int_1 = int[bool_t] print[int_1] # 👉️ 1 bool_f = False int_0 = int[bool_f] print[int_0] # 👉️ 0 # ----------------------------------------------- # ✅ convert 'true' and 'false' to 1 and 0 str_t = 'true' int_1 = int[str_t.lower[] == 'true'] print[int_1] # 👉️ 1 str_f = 'false' int_0 = int[str_f.lower[] == 'true'] print[int_0] # 👉️ 0 # ----------------------------------------------- # ✅ convert list of 'true' and 'false' to 1 and 0 my_list = ['true', 'true', 'false'] result = [int[item.lower[] == 'true'] for item in my_list] print[result] # 👉️ [1, 1, 0]
0.Copied!
result_1 = int[True] print[result_1] # 👉️ 1 result_2 = int[False] print[result_2] # 👉️ 0
Phương thức Str.Lower trả về một bản sao của chuỗi với tất cả các ký tự được chuyển đổi thành chữ thường.
Phương thức không thay đổi chuỗi gốc, nó trả về một chuỗi mới. Chuỗi là bất biến trong Python.
So sánh bình đẳng sẽ trả về
1 nếu chuỗi lưu trữ giá trị 'true' và In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
2 khác.In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
Chuyển đổi
In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
1 thành một số nguyên trả về def to_bool[s]:
return 1 if s == 'true' else 0
5 và chuyển đổi In [14]: def most_readable[s]:
...: return 1 if s == 'true' else 0
In [15]: def int_cast[s]:
...: return int[s == 'true']
In [16]: def str2bool[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [17]: def str2bool2[s]:
...: try:
...: return ['false', 'true'].index[s]
...: except [ValueError, AttributeError]:
...: raise ValueError[]
In [18]: def to_bool[s]:
...: return 1 - sum[s.encode['ascii']] % 2
In [19]: %timeit most_readable['true']
10000000 loops, best of 3: 112 ns per loop
In [20]: %timeit most_readable['false']
10000000 loops, best of 3: 109 ns per loop
In [21]: %timeit int_cast['true']
1000000 loops, best of 3: 259 ns per loop
In [22]: %timeit int_cast['false']
1000000 loops, best of 3: 262 ns per loop
In [23]: %timeit str2bool['true']
1000000 loops, best of 3: 343 ns per loop
In [24]: %timeit str2bool['false']
1000000 loops, best of 3: 325 ns per loop
In [25]: %timeit str2bool2['true']
1000000 loops, best of 3: 295 ns per loop
In [26]: %timeit str2bool2['false']
1000000 loops, best of 3: 277 ns per loop
In [27]: %timeit to_bool['true']
1000000 loops, best of 3: 607 ns per loop
In [28]: %timeit to_bool['false']
1000000 loops, best of 3: 612 ns per loop
2 thành một số nguyên trả về def to_bool[s]:
return 1 if s == 'true' else 0
4.Nếu bạn cần chuyển đổi danh sách các chuỗi 'true' và 'false' thành danh sách
def to_bool[s]:
return 1 if s == 'true' else 0
5 và def to_bool[s]:
return 1 if s == 'true' else 0
4, hãy sử dụng khả năng hiểu danh sách.Copied!
my_list = ['true', 'true', 'false'] result = [int[item.lower[] == 'true'] for item in my_list] print[result] # 👉️ [1, 1, 0]
Trên mỗi lần lặp, chúng tôi so sánh chuỗi hiện tại với chuỗi 'true' và chuyển đổi kết quả so sánh với một số nguyên.
Các chuỗi lưu trữ giá trị 'true' được chuyển đổi thành
def to_bool[s]:
return 1 if s == 'true' else 0
5 và tất cả các chuỗi khác được chuyển đổi thành def to_bool[s]:
return 1 if s == 'true' else 0
4.