4 tích hợp của Python có ích khi bạn cần tìm các giá trị nhỏ nhất và lớn nhất trong một cuộc tranh luận thông thường hoặc trong một loạt các đối số thông thường. Mặc dù những điều này có vẻ giống như các tính toán khá cơ bản, chúng hóa ra có nhiều trường hợp sử dụng thú vị trong lập trình trong thế giới thực. Bạn sẽ thử một số trường hợp sử dụng ở đây.smallest and largest values in an iterable or in a series of regular arguments. Even though these might seem like fairly basic computations, they turn out to have many interesting use cases in real-world programing. You’ll try out some of those use cases here.
1 vòng lặp, chức năng, danh sách toàn diện và biểu thức máy phát.
Bắt đầu với các chức năng Python từ min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
3 và min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
4
Python bao gồm một số chức năng tích hợp làm cho cuộc sống của bạn dễ chịu và hiệu quả hơn vì chúng có nghĩa là bạn không cần phải phát minh lại bánh xe. Hai ví dụ về các chức năng này là
4. Chúng chủ yếu áp dụng cho các vòng lặp, nhưng bạn cũng có thể sử dụng chúng với nhiều đối số thông thường. Công việc của họ là gì? Họ quan tâm đến việc tìm kiếm các giá trị nhỏ nhất và lớn nhất trong dữ liệu đầu vào của họ.
4, bạn có thể sử dụng chức năng để đạt được hai hành vi hơi khác nhau. Hành vi tiêu chuẩn cho mỗi hành vi là trả về giá trị tối thiểu hoặc tối đa thông qua so sánh đơn giản của dữ liệu đầu vào khi nó đứng. Hành vi thay thế là sử dụng chức năng đơn lẻ để sửa đổi các tiêu chí so sánh trước khi tìm các giá trị nhỏ nhất và lớn nhất.
4, bạn có thể bắt đầu bằng cách gọi mỗi hàm với một hàm có thể lặp lại như một đối số hoặc với hai hoặc nhiều đối số thông thường. Đó là những gì bạn sẽ làm ngay lập tức.
Gọi min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
3 và min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
4 với một đối số có thể lặp lại
4 tích hợp có hai chữ ký khác nhau cho phép bạn gọi chúng bằng một điều có thể được lặp lại như đối số đầu tiên của họ hoặc với hai hoặc nhiều đối số thông thường. Chữ ký chấp nhận một đối số có thể lặp lại duy nhất trông giống như sau:
Chấp nhận chức năng đơn lẻ để tùy chỉnh các tiêu chí so sánh
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
Sau đó trong hướng dẫn này, bạn sẽ tìm hiểu thêm về các đối số
>>> min(3,5,9,1,-5)-5>>> max(3,5,9,1,-5)9
4 và
>>> min(3,5,9,1,-5)-5>>> max(3,5,9,1,-5)9
3 tùy chọn. Hiện tại, chỉ cần tập trung vào đối số
4 với hai hoặc nhiều đối số thông thường. Một lần nữa, bạn sẽ nhận được giá trị tối thiểu hoặc tối đa trong dữ liệu đầu vào, tương ứng. Điều kiện duy nhất là các đối số phải có thể so sánh.
Sử dụng min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
3 và min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
4 với chuỗi và vòng lặp của chuỗi
4 có thể xử lý các giá trị có thể so sánh. Nếu không, bạn nhận được một
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
04, như bạn đã học. Cho đến thời điểm này, bạn đã thấy các ví dụ sử dụng các giá trị số trong một đối số có thể lặp lại hoặc nhiều đối số thông thường.
4 với các giá trị số được cho là trường hợp sử dụng phổ biến và hữu ích nhất của các hàm này. Tuy nhiên, bạn cũng có thể sử dụng các chức năng với chuỗi và vòng lặp của chuỗi. Trong những trường hợp này, thứ tự bảng chữ cái của các ký tự sẽ quyết định kết quả cuối cùng.
4 để tìm các chữ cái nhỏ nhất và lớn nhất trong một số văn bản. Trong bối cảnh này, nhỏ nhất có nghĩa là gần nhất với đầu bảng chữ cái và lớn nhất có nghĩa là gần nhất với phần cuối của bảng chữ cái:
4 với hai hoặc nhiều đối số thông thường. Một lần nữa, bạn sẽ nhận được giá trị tối thiểu hoặc tối đa trong dữ liệu đầu vào, tương ứng. Điều kiện duy nhất là các đối số phải có thể so sánh.
4 có thể xử lý các giá trị có thể so sánh. Nếu không, bạn nhận được một
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
04, như bạn đã học. Cho đến thời điểm này, bạn đã thấy các ví dụ sử dụng các giá trị số trong một đối số có thể lặp lại hoặc nhiều đối số thông thường.
4 với hai hoặc nhiều đối số thông thường. Một lần nữa, bạn sẽ nhận được giá trị tối thiểu hoặc tối đa trong dữ liệu đầu vào, tương ứng. Điều kiện duy nhất là các đối số phải có thể so sánh.
4 với hai hoặc nhiều đối số thông thường. Một lần nữa, bạn sẽ nhận được giá trị tối thiểu hoặc tối đa trong dữ liệu đầu vào, tương ứng. Điều kiện duy nhất là các đối số phải có thể so sánh.Unicode code point of that character. In these examples, the code point for the uppercase
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
43 is lower than the code point for the lowercase
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
4 với hai hoặc nhiều đối số thông thường. Một lần nữa, bạn sẽ nhận được giá trị tối thiểu hoặc tối đa trong dữ liệu đầu vào, tương ứng. Điều kiện duy nhất là các đối số phải có thể so sánh.
4 với hai hoặc nhiều đối số thông thường. Một lần nữa, bạn sẽ nhận được giá trị tối thiểu hoặc tối đa trong dữ liệu đầu vào, tương ứng. Điều kiện duy nhất là các đối số phải có thể so sánh.
4 trả về cái lớn nhất. Bạn có thể nhận được kết quả tương tự bằng phương thức
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
76 trên từ điển đầu vào của mình:
Sự khác biệt duy nhất giữa ví dụ sau này và cái trước là ở đây, mã rõ ràng hơn và rõ ràng hơn về những gì bạn đang làm. Bất cứ ai đọc mã của bạn sẽ nhanh chóng nhận ra rằng bạn muốn tìm các khóa nhỏ nhất và lớn nhất trong từ điển đầu vào.
>>>
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
4 trả về cái lớn nhất. Bạn có thể nhận được kết quả tương tự bằng phương thức
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
76 trên từ điển đầu vào của mình:
Sự khác biệt duy nhất giữa ví dụ sau này và cái trước là ở đây, mã rõ ràng hơn và rõ ràng hơn về những gì bạn đang làm. Bất cứ ai đọc mã của bạn sẽ nhanh chóng nhận ra rằng bạn muốn tìm các khóa nhỏ nhất và lớn nhất trong từ điển đầu vào.
>>>
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
4 trả về cái lớn nhất. Bạn có thể nhận được kết quả tương tự bằng phương thức
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
76 trên từ điển đầu vào của mình:
Sự khác biệt duy nhất giữa ví dụ sau này và cái trước là ở đây, mã rõ ràng hơn và rõ ràng hơn về những gì bạn đang làm. Bất cứ ai đọc mã của bạn sẽ nhanh chóng nhận ra rằng bạn muốn tìm các khóa nhỏ nhất và lớn nhất trong từ điển đầu vào.
Một yêu cầu phổ biến khác sẽ là tìm các giá trị nhỏ nhất và lớn nhất trong từ điển. Để tiếp tục với ví dụ
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
74, giả sử bạn muốn biết giá nhỏ nhất và lớn nhất. Trong tình huống này, bạn có thể sử dụng phương pháp
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
78:
Trong các ví dụ này, min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
3 đi qua tất cả các giá trị trong >>> min([3, 5, 9, 1, -5])
-5
>>> min([])
Traceback (most recent call last):
...
ValueError: min() arg is an empty sequence
>>> max([3, 5, 9, 1, -5])
9
>>> max([])
Traceback (most recent call last):
...
ValueError: max() arg is an empty sequence
74 và tìm thấy giá tối thiểu. Tương tự, min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
4 lặp lại các giá trị của >>> min([3, 5, 9, 1, -5])
-5
>>> min([])
Traceback (most recent call last):
...
ValueError: min() arg is an empty sequence
>>> max([3, 5, 9, 1, -5])
9
>>> max([])
Traceback (most recent call last):
...
ValueError: max() arg is an empty sequence
74 và trả về giá tối đa.
Cuối cùng, bạn cũng có thể sử dụng phương thức
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
83 trên từ điển đầu vào để tìm các cặp giá trị khóa tối thiểu và tối đa:keyword-only arguments.
4 trả về cái lớn nhất. Bạn có thể nhận được kết quả tương tự bằng phương thức
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
76 trên từ điển đầu vào của mình:
Sự khác biệt duy nhất giữa ví dụ sau này và cái trước là ở đây, mã rõ ràng hơn và rõ ràng hơn về những gì bạn đang làm. Bất cứ ai đọc mã của bạn sẽ nhanh chóng nhận ra rằng bạn muốn tìm các khóa nhỏ nhất và lớn nhất trong từ điển đầu vào.
>>>
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
4 trả về cái lớn nhất. Bạn có thể nhận được kết quả tương tự bằng phương thức
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
76 trên từ điển đầu vào của mình:function object, or more accurately, a callable object.
Sự khác biệt duy nhất giữa ví dụ sau này và cái trước là ở đây, mã rõ ràng hơn và rõ ràng hơn về những gì bạn đang làm. Bất cứ ai đọc mã của bạn sẽ nhanh chóng nhận ra rằng bạn muốn tìm các khóa nhỏ nhất và lớn nhất trong từ điển đầu vào.
Một yêu cầu phổ biến khác sẽ là tìm các giá trị nhỏ nhất và lớn nhất trong từ điển. Để tiếp tục với ví dụ
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
74, giả sử bạn muốn biết giá nhỏ nhất và lớn nhất. Trong tình huống này, bạn có thể sử dụng phương pháp
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
78:
>>>
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
4 trả về cái lớn nhất. Bạn có thể nhận được kết quả tương tự bằng phương thức
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
76 trên từ điển đầu vào của mình:
Sử dụng min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
3 và min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
4 với các biểu thức toàn diện và trình tạo
4 với danh sách hiểu hoặc biểu thức trình tạo làm đối số. Tính năng này có ích khi bạn cần chuyển đổi dữ liệu đầu vào ngay trước khi tìm giá trị biến đổi tối thiểu hoặc tối đa.list comprehension or generator expression as an argument. This feature comes in handy when you need to transform the input
data right before finding the minimum or maximum transformed value.
32 để bằng cách nào đó sửa đổi các tiêu chí so sánh. Sự khác biệt là sự hiểu biết thực sự biến đổi dữ liệu đầu vào trước khi thực hiện tính toán, do đó giá trị kết quả đến từ dữ liệu được chuyển đổi chứ không phải từ bản gốc.
Lưu ý rằng sử dụng min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
3 hoặc min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
4 với khả năng hiểu danh sách tương tự như sử dụng đối số >>> min(3, 5, 9, 1, -5)
-5
>>> max(3, 5, 9, 1, -5)
9
3. Sự khác biệt chính là với sự hiểu biết, kết quả cuối cùng là một giá trị được chuyển đổi, trong khi với >>> min(3, 5, 9, 1, -5)
-5
>>> max(3, 5, 9, 1, -5)
9
3, kết quả đến từ dữ liệu gốc:
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
32 để bằng cách nào đó sửa đổi các tiêu chí so sánh. Sự khác biệt là sự hiểu biết thực sự biến đổi dữ liệu đầu vào trước khi thực hiện tính toán, do đó giá trị kết quả đến từ dữ liệu được chuyển đổi chứ không phải từ bản gốc.
Danh sách toàn diện tạo ra một danh sách đầy đủ trong bộ nhớ, thường là một hoạt động lãng phí. Thực tế này đặc biệt đúng nếu bạn không cần danh sách kết quả trong mã của bạn nữa, đây có thể là trường hợp với min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
3 và min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
4. Vì vậy, nó luôn luôn hiệu quả hơn để sử dụng biểu thức máy phát.
Cú pháp cho các biểu thức máy phát gần giống như đối với các toàn bộ danh sách:
Lưu ý rằng sử dụng min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
3 hoặc min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
4 với khả năng hiểu danh sách tương tự như sử dụng đối số >>> min(3, 5, 9, 1, -5)
-5
>>> max(3, 5, 9, 1, -5)
9
3. Sự khác biệt chính là với sự hiểu biết, kết quả cuối cùng là một giá trị được chuyển đổi, trong khi với >>> min(3, 5, 9, 1, -5)
-5
>>> max(3, 5, 9, 1, -5)
9
3, kết quả đến từ dữ liệu gốc:
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
32 để bằng cách nào đó sửa đổi các tiêu chí so sánh. Sự khác biệt là sự hiểu biết thực sự biến đổi dữ liệu đầu vào trước khi thực hiện tính toán, do đó giá trị kết quả đến từ dữ liệu được chuyển đổi chứ không phải từ bản gốc.
3? Bạn có thể đối phó với các giá trị lớn, có phải bạn không? Vấn đề là bạn cần so sánh từng giá trị lớn với giới hạn trên của khoảng thời gian và sau đó chọn số nhỏ hơn của cả hai. Về cơ bản, bạn sẽ đặt tất cả các giá trị lớn thành giới hạn trên được quy định:
Cuộc gọi này đến min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
4 clip các giá trị nhỏ đến giới hạn thấp hơn khoảng thời gian. Để thực hiện việc cắt này, min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
4 so sánh số hiện tại và giới hạn khoảng thời gian để tìm giá trị tối đa. Trong ví dụ, >>> min([3, 5.0, 9, 1.0, -5])
-5
>>> max([3, 5.0, 9, 1.0, -5])
9
77 là số duy nhất được cắt.
Cuối cùng, bạn có thể chạy cả hai thao tác trong một lần bằng cách kết hợp
84 từ Numpy, có một mảng và giới hạn của khoảng mục tiêu, sau đó nó cắt tất cả các giá trị ngoài khoảng vào các cạnh của khoảng thời gian.
Tìm những điểm gần nhất
Bây giờ nói rằng bạn có một danh sách các bộ dữ liệu chứa các cặp giá trị đại diện cho các điểm Cartesian. Bạn muốn xử lý tất cả các cặp điểm này và tìm ra cặp nào có khoảng cách nhỏ nhất giữa các điểm. Trong tình huống này, bạn có thể làm một cái gì đó như sau:
86. Hàm này trả về khoảng cách Euclide giữa hai điểm, p và q, mỗi điểm được đưa ra dưới dạng một chuỗi tọa độ. Hai điểm phải có cùng số lượng kích thước.
3 để tìm cặp điểm với khoảng cách tối thiểu giữa các điểm.
Trong ví dụ này, bạn cần một hàm >>> min([3, 5.0, 9, 1.0, -5])
-5
>>> max([3, 5.0, 9, 1.0, -5])
9
90 vì >>> min(3, 5, 9, 1, -5)
-5
>>> max(3, 5, 9, 1, -5)
9
3 mong đợi một chức năng đối số đơn, trong khi >>> min([3, 5.0, 9, 1.0, -5])
-5
>>> max([3, 5.0, 9, 1.0, -5])
9
94 yêu cầu hai đối số. Vì vậy, hàm >>> min([3, 5.0, 9, 1.0, -5])
-5
>>> max([3, 5.0, 9, 1.0, -5])
9
90 có một đối số duy nhất, >>> min([3, 5.0, 9, 1.0, -5])
-5
>>> max([3, 5.0, 9, 1.0, -5])
9
96 và sau đó giải nén nó thành hai đối số để đưa vào >>> min([3, 5.0, 9, 1.0, -5])
-5
>>> max([3, 5.0, 9, 1.0, -5])
9
94.
Xác định các sản phẩm giá rẻ và đắt tiền
Bây giờ nói rằng bạn có một từ điển với tên và giá của một số sản phẩm và bạn muốn xác định các sản phẩm rẻ nhất và đắt nhất. Trong tình huống này, bạn có thể sử dụng
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
4 có tiêu chí so sánh thích hợp. Do đó, bạn có được một bộ phận với các sản phẩm rẻ nhất và đắt nhất trong dữ liệu đầu vào.
Tìm số số nguyên coprime
Một ví dụ thú vị khác về việc sử dụng min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
3 để giải quyết vấn đề trong thế giới thực là khi bạn cần tìm ra nếu hai số là coprime. Nói cách khác, bạn cần biết liệu số của bạn có chỉ là người chia cấp thông thường là >>> min([3, "5.0", 9, 1.0, "-5"])
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of 'str' and 'int'
>>> max([3, "5.0", 9, 1.0, "-5"])
Traceback (most recent call last):
...
TypeError: '>' not supported between instances of 'str' and 'int'
05 hay không.
Trong tình huống đó, bạn có thể mã hóa một hàm giá trị boolean hoặc định vị như sau:
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
15 chạy một câu lệnh dựa trên chuỗi một số lần nhất định. Trong các ví dụ này, tuyên bố được lặp lại ba lần. Cuộc gọi đến
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
21 và các hàm hẹn giờ Python khác, bạn có thể có ý tưởng về thuật toán nào của bạn là hiệu quả nhất về thời gian thực hiện. Ví dụ trên cho thấy rằng các toàn bộ danh sách có thể nhanh hơn một chút so với hàm
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
22 tích hợp khi nói đến việc xây dựng các danh sách mới.
Khám phá vai trò của >>> min([3, "5.0", 9, 1.0, "-5"])
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of 'str' and 'int'
>>> max([3, "5.0", 9, 1.0, "-5"])
Traceback (most recent call last):
...
TypeError: '>' not supported between instances of 'str' and 'int'
23 và >>> min([3, "5.0", 9, 1.0, "-5"])
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of 'str' and 'int'
>>> max([3, "5.0", 9, 1.0, "-5"])
Traceback (most recent call last):
...
TypeError: '>' not supported between instances of 'str' and 'int'
24 trong min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
3 và min(arg_1, arg_2[, ..., arg_n], *[, key]) -> minimum_value
max(arg_1, arg_2[, ..., arg_n], *[, key]) -> maximum_value
4
4 ôm lấy triết lý đánh máy con vịt Python bằng cách dựa vào các phương pháp đặc biệt
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
23 và
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
24.
Những phương pháp này là một phần của những gì Python gọi là các phương pháp so sánh phong phú. Cụ thể,
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
23 và
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
24 hỗ trợ các toán tử nhỏ hơn (
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
35) và lớn hơn (
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
36), tương ứng. Ý nghĩa của sự hỗ trợ ở đây là gì? Khi Python tìm thấy một cái gì đó như
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
37 trong mã của bạn, nó sẽ làm
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
38.rich comparison methods. Specifically,
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
23 and
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
24 support the less than
(
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
35) and greater than (
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
36) operators, respectively. What’s the meaning of support here? When Python finds something like
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
37 in your code, it internally does
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
4 với các giá trị của bất kỳ loại dữ liệu nào thực hiện
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
23 và
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
24. Đó là lý do tại sao các chức năng này hoạt động với các giá trị của tất cả các loại dữ liệu tích hợp của Python,:
>>>
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
0
Các loại dữ liệu tích hợp Python, thực hiện các phương pháp đặc biệt
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
23 và
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
24. Vì vậy, bạn có thể cung cấp bất kỳ loại dữ liệu nào trong số này vào
4. Để đạt được điều này, bạn cần cung cấp các triển khai của riêng bạn
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
23 và
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
24. Xem xét lớp
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
51 sau đây là một ví dụ về khả năng tương thích này:
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
1
Lưu ý rằng việc triển khai
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
23 và
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
24 yêu cầu một đối số mà thường có tên là
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
54. Đối số này đại diện cho toán hạng thứ hai trong các hoạt động so sánh cơ bản. Ví dụ: trong một biểu thức như
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
37, bạn sẽ có
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
56 sẽ là
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
57 và
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
58 sẽ là
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
54.
Trong ví dụ này,
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
23 và
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
24 trả về kết quả của việc so sánh hai người ____ ____362. Ở đây, cách thức hoạt động của nó trong thực tế:
>>>
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
2
Mát mẻ! Bạn có thể xử lý các đối tượng
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
23 và
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
3
Bởi vì lớp
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
72 này không cung cấp các triển khai phù hợp của
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
23 và
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
Cho đến thời điểm này, bạn đã học được cách hoạt động của Python từ ____43 và ____44. Bạn đã sử dụng chúng để tìm các giá trị nhỏ nhất và lớn nhất trong số một số số, chuỗi và hơn thế nữa. Bạn biết cách gọi các chức năng này với một điều duy nhất là một đối số hoặc với một số lượng đối số thông thường không được xác định. Cuối cùng, bạn đã mã hóa một loạt các ví dụ thực tế tiếp cận các vấn đề trong thế giới thực bằng cách sử dụng
4 để tìm các giá trị nhỏ nhất và lớn nhất trong dữ liệu của bạn, việc học cách thực hiện tính toán này từ đầu là một bài tập hữu ích có thể cải thiện tư duy logic và kỹ năng lập trình của bạn.
Trong phần này, bạn sẽ học cách tìm các giá trị tối thiểu và tối đa trong dữ liệu của bạn. Bạn cũng sẽ học cách thực hiện các phiên bản
Để tìm giá trị tối thiểu trong một danh sách nhỏ các số với tư cách là con người, bạn thường kiểm tra các số và ngầm so sánh tất cả chúng trong tâm trí bạn. Vâng, bộ não của bạn thật tuyệt vời! Tuy nhiên, máy tính không thông minh. Họ cần hướng dẫn chi tiết để hoàn thành bất kỳ nhiệm vụ.
Bạn phải nói với máy tính của mình để lặp lại tất cả các giá trị trong khi so sánh chúng theo cặp. Trong quá trình, máy tính phải lưu ý đến giá trị tối thiểu hiện tại trong mỗi cặp cho đến khi danh sách các giá trị được xử lý hoàn toàn.
Giải thích này có thể khó hình dung, vì vậy, đây là một chức năng Python thực hiện công việc:
>>>
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
4
Trong đoạn mã này, bạn xác định
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
4 không phải là trống và các giá trị của nó theo thứ tự tùy ý.
Hàm coi giá trị đầu tiên là một
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
1 lặp lại phần còn lại của các phần tử trong dữ liệu đầu vào.
Tuyên bố có điều kiện so sánh
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
94 hiện tại với
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
92 dự kiến trong lần lặp đầu tiên. Nếu
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
94 hiện tại nhỏ hơn
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
92, thì các bản cập nhật có điều kiện
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
92 phù hợp.
Mỗi lần lặp mới so sánh
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
94 hiện tại với
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
92 sẽ giữ giá trị nhỏ nhất trong dữ liệu đầu vào.
Mát mẻ! Bạn đã mã hóa một hàm tìm thấy giá trị nhỏ nhất trong số lượng số lượng. Bây giờ xem lại
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
90 và nghĩ về cách bạn mã hóa một hàm để tìm giá trị lớn nhất. Vâng, đó là nó! Bạn chỉ cần thay đổi toán tử so sánh từ nhỏ hơn (
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
35) thành lớn hơn (
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
36) và có thể đổi tên hàm và một số biến cục bộ để ngăn chặn sự nhầm lẫn.
Chức năng mới của bạn có thể trông giống như thế này:
>>>
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
5
Trong đoạn mã này, bạn xác định
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
4 không phải là trống và các giá trị của nó theo thứ tự tùy ý.
Hàm coi giá trị đầu tiên là một
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
1 lặp lại phần còn lại của các phần tử trong dữ liệu đầu vào.
Tuyên bố có điều kiện so sánh
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
94 hiện tại với
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
92 dự kiến trong lần lặp đầu tiên. Nếu
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
94 hiện tại nhỏ hơn
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
92, thì các bản cập nhật có điều kiện
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
92 phù hợp.
Mỗi lần lặp mới so sánh
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
94 hiện tại với
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
92 sẽ giữ giá trị nhỏ nhất trong dữ liệu đầu vào.
Mát mẻ! Bạn đã mã hóa một hàm tìm thấy giá trị nhỏ nhất trong số lượng số lượng. Bây giờ xem lại >>> min([3, "5.0", 9, 1.0, "-5"])
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of 'str' and 'int'
>>> max([3, "5.0", 9, 1.0, "-5"])
Traceback (most recent call last):
...
TypeError: '>' not supported between instances of 'str' and 'int'
90 và nghĩ về cách bạn mã hóa một hàm để tìm giá trị lớn nhất. Vâng, đó là nó! Bạn chỉ cần thay đổi toán tử so sánh từ nhỏ hơn (>>> min([3, "5.0", 9, 1.0, "-5"])
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of 'str' and 'int'
>>> max([3, "5.0", 9, 1.0, "-5"])
Traceback (most recent call last):
...
TypeError: '>' not supported between instances of 'str' and 'int'
35) thành lớn hơn (>>> min([3, "5.0", 9, 1.0, "-5"])
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of 'str' and 'int'
>>> max([3, "5.0", 9, 1.0, "-5"])
Traceback (most recent call last):
...
TypeError: '>' not supported between instances of 'str' and 'int'
36) và có thể đổi tên hàm và một số biến cục bộ để ngăn chặn sự nhầm lẫn.
Chức năng mới của bạn có thể trông giống như thế này:
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
90. Sự khác biệt quan trọng nhất, ngoài việc đặt tên, là
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
36) thay vì ít hơn toán tử (
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
35).
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
6
Là một bài tập, bạn có thể nghĩ cách tránh mã lặp đi lặp lại trong nguyên tắc
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
4 bằng cách sử dụng các kỹ năng Python của bạn, mà bạn sẽ giải quyết chỉ trong giây lát.
Trước khi lặn vào, bạn cần phải nhận thức được các yêu cầu kiến thức. Bạn sẽ kết hợp các chủ đề như báo cáo có điều kiện, xử lý ngoại lệ, trình biết danh sách, lặp lại xác định với các vòng
Nếu bạn cảm thấy rằng bạn không biết mọi thứ về các chủ đề này, thì đừng lo lắng. Bạn sẽ học bằng cách làm. Nếu bạn bị mắc kẹt, thì bạn có thể quay lại và xem lại các tài nguyên được liên kết.
4, bạn sẽ bắt đầu bằng cách mã hóa một hàm trợ giúp mà có thể tìm thấy giá trị nhỏ nhất hoặc lớn nhất trong dữ liệu đầu vào, tùy thuộc vào các đối số bạn sử dụng trong cuộc gọi. Tất nhiên, hàm trợ giúp sẽ đặc biệt phụ thuộc vào toán tử được sử dụng để so sánh các giá trị đầu vào.
Giữ chức năng toán tử so sánh thích hợp để tính toán trong tay
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
7
>>> min(3,5,9,1,-5)-5>>> max(3,5,9,1,-5)9
3
Có chức năng đơn lẻ để sửa đổi chức năng so sánh tiêu chí và hành vi
49 sẽ giữ một danh sách các giá trị có thể trống. Ngay cả khi danh sách kết quả trống rỗng, thì giờ đây, nó đã sạch sẽ và sẵn sàng để tiếp tục quá trình tìm kiếm giá trị tối thiểu hoặc tối đa của nó.
Xử lý đối số >>> min(3, 5, 9, 1, -5)
-5
>>> max(3, 5, 9, 1, -5)
9
4
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
8
Trong đoạn mã này, bạn xác định một điều kiện để kiểm tra xem
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
9
Bạn bắt đầu đoạn mã này với một điều kiện kiểm tra xem người dùng đã cung cấp chức năng
>>> min(3,5,9,1,-5)-5>>> max(3,5,9,1,-5)9
3. Nếu họ trú ẩn, thì bạn sẽ tạo một danh sách các khóa trực tiếp từ
>>> min([3,5,9,1,-5])-5>>> min([])Traceback (most recent call last):...ValueError: min() arg is an empty sequence>>> max([3,5,9,1,-5])9>>> max([])Traceback (most recent call last):...ValueError: max() arg is an empty sequence
89 nếu đối số của nó là có thể gọi được và
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
08 khác.
Khi bạn chắc chắn rằng
>>> min(3,5,9,1,-5)-5>>> max(3,5,9,1,-5)9
3 là một đối tượng có thể gọi được, thì bạn sẽ xây dựng danh sách các khóa so sánh bằng cách áp dụng
>>> min(3,5,9,1,-5)-5>>> max(3,5,9,1,-5)9
3 cho mỗi giá trị trong dữ liệu đầu vào.
Cuối cùng, nếu
>>> min(3,5,9,1,-5)-5>>> max(3,5,9,1,-5)9
3 không phải là một đối tượng có thể gọi được, thì mệnh đề
22. Các chức năng này tương đương chức năng của các toán tử lớn hơn (
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
36) và nhỏ hơn (
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
35), tương ứng. Ví dụ: biểu thức boolean
>>> min([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '<' not supported between instances of 'str' and 'int'>>> max([3,"5.0",9,1.0,"-5"])Traceback (most recent call last):...TypeError: '>' not supported between instances of 'str' and 'int'
37 tương đương với chức năng gọi
>>> min(3,5,9,1,-5)-5>>> max(3,5,9,1,-5)9
16. Bạn sẽ sử dụng các chức năng này để cung cấp đối số
4 tích hợp của Python để tìm các giá trị nhỏ nhất và lớn nhất trong một loạt hoặc trong một loạt hai hoặc nhiều đối số thông thường. Bạn cũng đã tìm hiểu về một vài đặc điểm khác của
4 có thể làm cho chúng hữu ích trong chương trình hàng ngày của bạn.smallest and largest values in an iterable or in a series of two or more regular arguments. You also learned about a few other characteristics of
4 trong Pure Python, một bài tập học tập đẹp giúp bạn hiểu logic đằng sau các chức năng tích hợp này.real-world problems that you might run into while coding. You’ve also a written custom version of
4 in pure Python, a nice learning exercise that helped you understand the logic behind these built-in functions.
Làm thế nào để bạn tìm thấy mức tối thiểu mà không có chức năng sẵn có trong Python?
Quá trình: Ban đầu gán phần tử nằm ở 0 đến tối thiểu bằng Min = L [0].Sử dụng cho vòng lặp truy cập mỗi vị trí theo phương pháp từ 1 đến LEN (L) -1.Nếu phần tử nằm ở bất kỳ vị trí nào nhỏ hơn Min, thì hãy gán phần tử là Min bằng cách sử dụng Min = L [i] cuối cùng Min giữ giá trị tối thiểu trong danh sách.Initially assign the element located at 0 to min using min = l[0]. using for loop visit each location serially from 1 to len(l)-1. if the element located in any position is lesser than min, then assign the element as min by using min = l[i] finally min holds the minimum value in the list.
Làm thế nào để bạn tìm thấy giá trị tối đa và tối thiểu trong Python?
Sử dụng Python's Min () và Max () để tìm các giá trị nhỏ nhất và lớn nhất trong dữ liệu của bạn.Gọi Min () và Max () với một lần lặp lại hoặc với bất kỳ số lượng đối số thông thường nào.Sử dụng Min () và Max () với chuỗi và từ điển.. Call min() and max() with a single iterable or with any number of regular arguments. Use min() and max() with strings and dictionaries.
Là chức năng tối đa trong Python?
Hàm Python Max () trả về mục lớn nhất trong một đối số có thể lặp lại hoặc lớn nhất trong hai hoặc nhiều đối số.Nó có hai hình thức.. It has two forms.
Làm thế nào để bạn tìm thấy giá trị tối đa và tối thiểu?
Giá trị tối đa của biểu đồ là điểm mà tọa độ y có giá trị lớn nhất.Giá trị tối thiểu là điểm trên biểu đồ trong đó tọa độ y có giá trị nhỏ nhất.. The minimum value is the point on the graph where the y-coordinate has the smallest value.