sorted[range[len[a]], key=lambda i: a[i]][-2:]
hoặc
sorted[range[len[a]], key=lambda i: a[i], reverse=True][:2]
hoặc
import operator
zip[*sorted[enumerate[a], key=operator.itemgetter[1]]][0][-2:]
hoặc [đối với danh sách dài], hãy xem xét sử dụng heapq.nlargest
zip[*heapq.nlargest[2, enumerate[a], key=operator.itemgetter[1]]][0]
Cải thiện bài viết
Lưu bài viếtargsort[]
function.
Đọc
Bàn luậnnumpy.argsort[]
function performs an indirect sort along the given axis.
Đô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 muốn tìm n yếu tố lớn nhất. Nhiệm vụ này có thể xảy ra trong nhiều lĩnh vực như phát triển web và trong khi làm việc với cơ sở dữ liệu. Đôi khi chúng ta có thể yêu cầu chỉ tìm các chỉ số của chúng. Hãy để thảo luận về cách nhất định để tìm các chỉ số của các yếu tố lớn nhất trong danh sách.
Phương pháp: Sử dụng
import operator
zip[*sorted[enumerate[a], key=operator.itemgetter[1]]][0][-2:]
8 + Lambda + Danh sách SLIGIN Tác vụ Có thể được thực hiện bằng cách sử dụng kết hợp các chức năng trên. Trong đó, import operator
zip[*sorted[enumerate[a], key=operator.itemgetter[1]]][0][-2:]
8, có thể được sử dụng để lấy container theo cách yêu cầu để có n phần tử lớn nhất ở phía sau và sau đó các chỉ số có thể được tính toán bằng cách sử dụng cắt danh sách.numpy.argsort[Numpy_array, axis=None, kind=None, order=None]
Các
numpy.argsort[Numpy_array, axis=None, kind=None, order=None]
7numpy.argsort[Numpy_array, axis=None, kind=None, order=None]
8numpy.argsort[Numpy_array, axis=None, kind=None, order=None]
9 0# Importing the Numpy Package
import numpy as np
# Numpy Array
scores = np.array[[100,67,92,87,66,89,76,22]]
# Getting indices of N = 3 maximum values
x = np.argsort[scores][::-1][:3]
print["Indices:",x]
# Getting N maximum values
print["Values:",scores[x]]
1# Importing the Numpy Package
import numpy as np
# Numpy Array
scores = np.array[[100,67,92,87,66,89,76,22]]
# Getting indices of N = 3 maximum values
x = np.argsort[scores][::-1][:3]
print["Indices:",x]
# Getting N maximum values
print["Values:",scores[x]]
2# Importing the Numpy Package
import numpy as np
# Numpy Array
scores = np.array[[100,67,92,87,66,89,76,22]]
# Getting indices of N = 3 maximum values
x = np.argsort[scores][::-1][:3]
print["Indices:",x]
# Getting N maximum values
print["Values:",scores[x]]
3# Importing the Numpy Package
import numpy as np
# Numpy Array
scores = np.array[[100,67,92,87,66,89,76,22]]
# Getting indices of N = 3 maximum values
x = np.argsort[scores][::-1][:3]
print["Indices:",x]
# Getting N maximum values
print["Values:",scores[x]]
1zip[*heapq.nlargest[2, enumerate[a], key=operator.itemgetter[1]]][0]
9zip[*heapq.nlargest[2, enumerate[a], key=operator.itemgetter[1]]][0]
6# Importing the Numpy Package
import numpy as np
# Numpy Array
scores = np.array[[100,67,92,87,66,89,76,22]]
# Getting indices of N = 3 maximum values
x = np.argsort[scores][::-1][:3]
print["Indices:",x]
# Getting N maximum values
print["Values:",scores[x]]
1zip[*heapq.nlargest[2, enumerate[a], key=operator.itemgetter[1]]][0]
8# Importing the Numpy Package
import numpy as np
# Numpy Array
scores = np.array[[100,67,92,87,66,89,76,22]]
# Getting indices of N = 3 maximum values
x = np.argsort[scores][::-1][:3]
print["Indices:",x]
# Getting N maximum values
print["Values:",scores[x]]
8numpy.argsort[Numpy_array, axis=None, kind=None, order=None]
The original list is : [5, 6, 10, 4, 7, 1, 19] Indices list of max N elements is : [1, 4, 2, 6]
0
8__numpy.argsort[Numpy_array, axis=None, kind=None, order=None]
7numpy.argsort[Numpy_array, axis=None, kind=None, order=None]
8numpy.argsort[Numpy_array, axis=None, kind=None, order=None]
heapq.nlargest
1
0# Importing the Numpy Package
import numpy as np
# Numpy Array
scores = np.array[[100,67,92,87,66,89,76,22]]
# Getting indices of N = 3 maximum values
x = np.argsort[scores][::-1][:3]
print["Indices:",x]
# Getting N maximum values
print["Values:",scores[x]]
1# Importing the Numpy Package
import numpy as np
# Numpy Array
scores = np.array[[100,67,92,87,66,89,76,22]]
# Getting indices of N = 3 maximum values
x = np.argsort[scores][::-1][:3]
print["Indices:",x]
# Getting N maximum values
print["Values:",scores[x]]
heapq.nlargest
4
6: Chỉ định các trường nào để so sánh đầu tiên.sorted[range[len[a]], key=lambda i: a[i], reverse=True][:2]
Giá trị trả về
Hàm numpy.argsort[]
trả về một mảng các chỉ số theo trục được chỉ định.
Mã số
Hãy để một cái nhìn vào mã bây giờ.
# Importing the Numpy Package
import numpy as np
# Numpy Array
scores = np.array[[100,67,92,87,66,89,76,22]]
# Getting indices of N = 3 maximum values
x = np.argsort[scores][::-1][:3]
print["Indices:",x]
# Getting N maximum values
print["Values:",scores[x]]
Giải trình
Trong dòng 2, chúng tôi nhập thư viện
8 với bí danhsorted[range[len[a]], key=lambda i: a[i], reverse=True][:2]
9.sorted[range[len[a]], key=lambda i: a[i], reverse=True][:2]
Trong dòng 5, chúng tôi tạo một mảng
8 cần được sắp xếp.sorted[range[len[a]], key=lambda i: a[i], reverse=True][:2]
Trong dòng 8, chúng tôi sắp xếp mảng
8 với hàmsorted[range[len[a]], key=lambda i: a[i], reverse=True][:2]
2. Hàmimport operator zip[*sorted[enumerate[a], key=operator.itemgetter[1]]][0][-2:]
2 sắp xếp mảngimport operator zip[*sorted[enumerate[a], key=operator.itemgetter[1]]][0][-2:]
8 theo thứ tự tăng dần và trả về các chỉ số của chúng.sorted[range[len[a]], key=lambda i: a[i], reverse=True][:2]
Vì tuyên bố vấn đề muốn chúng tôi tìm các chỉ số của N số tối đa, chúng tôi sử dụng
5 để đảo ngược mảng. Cuối cùng, chúng tôi sử dụngimport operator zip[*sorted[enumerate[a], key=operator.itemgetter[1]]][0][-2:]
6 để có được các giá trị N đầu tiên. Ở đây, n = 3 đã được thực hiện.import operator zip[*sorted[enumerate[a], key=operator.itemgetter[1]]][0][-2:]
Trong dòng 9, chúng tôi in các chỉ số cần thiết.
Trong dòng 12, chúng tôi in các giá trị tối đa N để kiểm tra độ trễ của chương trình.
Theo cách này, chúng ta có thể nhận được các chỉ số của n giá trị tối đa từ một mảng numpy. Chúng ta cũng có thể nhận được các chỉ số của N giá trị tối thiểu bằng cách không đảo ngược mảng. Trong trường hợp đó, chúng tôi sẽ sử dụng
import operator
zip[*sorted[enumerate[a], key=operator.itemgetter[1]]][0][-2:]
5 trong dòng 8.THẺ LIÊN QUAN
Numpy
Python
cộng đồng
Người đóng góp
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
This task can be performed using the combination of above functions. In this the
import operator
zip[*sorted[enumerate[a], key=operator.itemgetter[1]]][0][-2:]
8, can be used to get the container in way which requires to get N largest elements at rear end and then the indices can be computed using list slicing.Đô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 muốn tìm n yếu tố lớn nhất. Nhiệm vụ này có thể xảy ra trong nhiều lĩnh vực như phát triển web và trong khi làm việc với cơ sở dữ liệu. Đôi khi chúng ta có thể yêu cầu chỉ tìm các chỉ số của chúng. Hãy để thảo luận về cách nhất định để tìm các chỉ số của các yếu tố lớn nhất trong danh sách.
Phương pháp: Sử dụng
import operator
zip[*sorted[enumerate[a], key=operator.itemgetter[1]]][0][-2:]
8 + Lambda + Danh sách SLIGIN Tác vụ Có thể được thực hiện bằng cách sử dụng kết hợp các chức năng trên. Trong đó, import operator
zip[*sorted[enumerate[a], key=operator.itemgetter[1]]][0][-2:]
8, có thể được sử dụng để lấy container theo cách yêu cầu để có n phần tử lớn nhất ở phía sau và sau đó các chỉ số có thể được tính toán bằng cách sử dụng cắt danh sách.Các
numpy.argsort[Numpy_array, axis=None, kind=None, order=None]
7numpy.argsort[Numpy_array, axis=None, kind=None, order=None]
8numpy.argsort[Numpy_array, axis=None, kind=None, order=None]
9 0# Importing the Numpy Package
import numpy as np
# Numpy Array
scores = np.array[[100,67,92,87,66,89,76,22]]
# Getting indices of N = 3 maximum values
x = np.argsort[scores][::-1][:3]
print["Indices:",x]
# Getting N maximum values
print["Values:",scores[x]]
1# Importing the Numpy Package
import numpy as np
# Numpy Array
scores = np.array[[100,67,92,87,66,89,76,22]]
# Getting indices of N = 3 maximum values
x = np.argsort[scores][::-1][:3]
print["Indices:",x]
# Getting N maximum values
print["Values:",scores[x]]
2# Importing the Numpy Package
import numpy as np
# Numpy Array
scores = np.array[[100,67,92,87,66,89,76,22]]
# Getting indices of N = 3 maximum values
x = np.argsort[scores][::-1][:3]
print["Indices:",x]
# Getting N maximum values
print["Values:",scores[x]]
3# Importing the Numpy Package
import numpy as np
# Numpy Array
scores = np.array[[100,67,92,87,66,89,76,22]]
# Getting indices of N = 3 maximum values
x = np.argsort[scores][::-1][:3]
print["Indices:",x]
# Getting N maximum values
print["Values:",scores[x]]
zip[*heapq.nlargest[2, enumerate[a], key=operator.itemgetter[1]]][0]
1 zip[*heapq.nlargest[2, enumerate[a], key=operator.itemgetter[1]]][0]
9# Importing the Numpy Package import numpy as np # Numpy Array scores = np.array[[100,67,92,87,66,89,76,22]] # Getting indices of N = 3 maximum values x = np.argsort[scores][::-1][:3] print["Indices:",x] # Getting N maximum values print["Values:",scores[x]] # Importing the Numpy Package import numpy as np # Numpy Array scores = np.array[[100,67,92,87,66,89,76,22]] # Getting indices of N = 3 maximum values x = np.argsort[scores][::-1][:3] print["Indices:",x] # Getting N maximum values print["Values:",scores[x]]
6
1 zip[*heapq.nlargest[2, enumerate[a], key=operator.itemgetter[1]]][0]
8
8numpy.argsort[Numpy_array, axis=None, kind=None, order=None]
The original list is : [5, 6, 10, 4, 7, 1, 19]
Indices list of max N elements is : [1, 4, 2, 6]
0
8__numpy.argsort[Numpy_array, axis=None, kind=None, order=None]
The original list is : [5, 6, 10, 4, 7, 1, 19] Indices list of max N elements is : [1, 4, 2, 6]