Hướng dẫn find minimum index in array python - tìm chỉ số tối thiểu trong mảng python
Tôi cần tìm chỉ số của nhiều hơn một giá trị tối thiểu xảy ra trong một mảng. Tôi khá được biết đến với Show
Điều này cho tôi 0, thay vào đó tôi mong đợi, 0,5,7. Cảm ơn!
Đã hỏi ngày 23 tháng 10 năm 2013 lúc 16:09Oct 23, 2013 at 16:09
2 Cái này cần phải dùng mẹo:
Argmin không trả lại một danh sách như bạn mong đợi trong trường hợp này. Đã trả lời ngày 23 tháng 10 năm 2013 lúc 16:21Oct 23, 2013 at 16:21
Tom Swiftytom SwiftyTom Swifty 2.7942 Huy hiệu vàng16 Huy hiệu bạc25 Huy hiệu Đồng2 gold badges16 silver badges25 bronze badges 2 Có lẽ
Nó sẽ cho [0,5,7]. Đã trả lời ngày 23 tháng 10 năm 2013 lúc 16:20Oct 23, 2013 at 16:20
Tonjotonjotonjo 1.3461 Huy hiệu vàng14 Huy hiệu bạc27 Huy hiệu đồng1 gold badge14 silver badges27 bronze badges 2 Tôi nghĩ rằng đây sẽ là cách dễ nhất, mặc dù nó không sử dụng bất kỳ chức năng Numpy lạ mắt nào
Đã trả lời ngày 23 tháng 10 năm 2013 lúc 16:26Oct 23, 2013 at 16:26
jrk0414jrk0414jrk0414 1341 Huy hiệu vàng1 Huy hiệu bạc11 Huy hiệu đồng1 gold badge1 silver badge11 bronze badges Đưa ra một danh sách N số nguyên, hãy tìm vị trí phần tử tối đa và tối thiểu trong danh sách Python. & NBSP; Example: Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 3 Phương pháp 1: Sử dụng phương pháp bản địaCách tiếp cận ngây thơ sẽ là đi qua trong danh sách và theo dõi mức tối thiểu và tối đa cùng với các chỉ số của họ. Chúng ta phải thực hiện so sánh N cho tối thiểu và đồng thời so sánh N cho tối đa. Dưới đây là việc thực hiện phương pháp ngây thơ. & NBSP;naive approach will be to traverse in the list and keep track of the minimum and maximum along with their indices. We have to do N comparisons for minimum and at the same time N comparisons for maximum. Below is the implementation of the naive approach. Python3
0 1 2 3 4 3 6 3 8 3__ 2 0 4 5 6 5 1 9 0 1 2 3 4 3 6 7 8 9 Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 30 Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 31 Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 32 0 Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 34 8 9 Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 37 Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 31 Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 39____10 Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 34 Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 102 3Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 104 Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 105 Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 106 Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 107 Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 102 3The maximum is at position 6 The minimum is at position 30 Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 105 Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 106 The maximum is at position 6 The minimum is at position 33 Output: Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 10 Phương pháp 2: Sử dụng hàm inbuildChức năng sẵn có của Python cho phép chúng tôi tìm thấy nó trong một dòng, chúng tôi có thể tìm thấy mức tối thiểu trong danh sách bằng hàm min () và sau đó sử dụng hàm index () để tìm ra chỉ mục của phần tử tối thiểu đó. Tương tự, chúng ta có thể thực hiện tương tự để tìm ra phần tử tối đa bằng cách sử dụng hàm tối đa () và sau đó tìm ra chỉ số của phần tử tối đa bằng cách sử dụng hàm sẵn có chỉ số () trong Python. & NBSP; allows us to find it in one line, we can find the minimum in the list using the min() function and then use the index() function to find out the index of that minimum element. Similarly we can do the same for finding out the maximum element using the max() function and then find out the index of the maximum element using the index() inbuilt function in python. Lưu ý: Index () Trả về chỉ số của lần xuất hiện đầu tiên trong trường hợp có nhiều lần xuất hiện của một phần tử. Vì vậy, nếu tối đa (hoặc tối thiểu) xảy ra nhiều lần, lần xuất hiện đầu tiên được trả lại. Dưới đây là việc thực hiện phương pháp trên: & nbsp;index() returns the index of the first occurrence in case there are multiple occurrences of an element. So if maximum (or minimum) occurs more than once, the first occurrence is returned. Below is the implementation of the above approach: Python3The maximum is at position 6 The minimum is at position 34 The maximum is at position 6 The minimum is at position 35 8The maximum is at position 6 The minimum is at position 37 0 The maximum is at position 6 The minimum is at position 39 Min: 8 Max: 50 Min: 8 Max: 51 8Min: 8 Max: 53 0 The maximum is at position 6 The minimum is at position 39 Min: 8 Max: 56 Min: 8 Max: 51 8Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 102 np.argmin 0np.argmin 1np.argmin 2 4 8Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 102 np.argmin 6np.argmin 7np.argmin 2 4
0 1gfg_list 3 3gfg_list 5____13 4 3gfg_list 3 3gfg_list 5__13____ 05 6Min: 8 Max: 51 Output: The maximum is at position 6 The minimum is at position 3 Phương pháp 3: Sử dụng gấu trúcTrong phương thức này, chúng tôi sẽ sử dụng idxmin () và idxmax () để in chỉ mục tối đa và chỉ mục tối thiểu bằng mô -đun Pandas. Python3 08 09Is 23 24 3 26 3 21 3__ 23 35 3 19 3 39 3 41 3 43 3 13 1Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 102 3 49 50Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 102 3 53 54Output: Min: 8 Max: 5 |