Xin chào,
Làm cách nào để sắp xếp một mảng nhiều chiều theo thứ tự ngược lại?
Ví dụ,
$songs = array[
'3' => array['artist'=>'The Smashing Pumpkins', 'songname'=>'Soma'],
'4' => array['artist'=>'The Decemberists', 'songname'=>'The Island'],
'1' => array['artist'=>'Fleetwood Mac', 'songname' =>'Second-hand News'],
'2' => array['artist'=>'Jack Johnson', 'songname' =>'Only the Ocean']
];
print_r[$songs];
sẽ dẫn đến điều này,
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
nhưng tôi muốn có nó theo thứ tự này thay vào đó,
Array
[
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
]
Tôi muốn giữ các giá trị chính là [2],[1],[4],[3] [thứ tự ban đầu là [3],[4],[1],[2]]
Tôi có cần phải viết một chức năng để làm điều này?
Cảm ơn nhiều,
Lau
Trong bài viết này, chúng ta sẽ xem cách lấy chỉ mục của mảng đa chiều NumPy theo thứ tự ngược lại
Cách tiếp cận
- Đầu tiên, chúng tôi nhập thư viện NumPy và khởi tạo các tham số cần thiết bao gồm ma trận mà chúng tôi cần làm việc và các tham số cần thiết khác
- Bây giờ chúng ta sẽ lặp qua từng hàng, lật nó và tìm chỉ mục của phần tử mong muốn trong mỗi hàng theo thứ tự ngược lại và lưu trữ nó trong danh sách [kết quả] mà chúng ta đã tạo
- Bây giờ chúng tôi đã có tất cả các chỉ mục của mình từ mặt sau của các hàng trong mảng. Bây giờ chúng ta cần chuyển đổi chỉ mục phù hợp để đọc từ trái sang phải, vì vậy, chúng ta trừ độ dài của hàng với mỗi chỉ mục và tiếp tục giảm nó đi một để mảng của chúng ta phù hợp với việc lập chỉ mục bằng 0
Chỉ mục cuối cùng = Tổng số phần tử trong hàng - Chỉ mục hiện tại-1
Thí dụ
Đầu vào
[[1,2,3,4,2],
[2,3,4,1,5],
[2,2,4,3,2],
[1,3,4,2,4]]
đầu ra
[4 0 4 3]
Giải trình. Trong ví dụ trên, chúng tôi đang cố gắng tìm cách lập chỉ mục xuất hiện đầu tiên của '2' theo thứ tự ngược lại và chúng tôi có 5 phần tử trong mỗi hàng và sau khi lập chỉ mục ngược, chúng tôi nhận được một mảng như [0, 4, 0, 1] hiện đang sử dụng
final_list[0] => Tổng số phần tử trong hàng – Chỉ mục hiện tại- 1 => 5-0-1 =>4
final_list[1] => Tổng số phần tử trong hàng – Chỉ mục hiện tại- 1 => 5-4-1 =>0
final_list[2] => Tổng số phần tử trong hàng – Chỉ mục hiện tại- 1 => 5-0-1 =>4
final_list[3] => Tổng số phần tử trong hàng – Chỉ mục hiện tại- 1 => 5-1-1 =>3
ví dụ 1
Python3
#import Modules
import
numpy as np
# initialize parameters
x
__=
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
0Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
1Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
0Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
2Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
4Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
0import
7import
8import
9import
0
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
2Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
4Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
1Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2numpy as np
8import
7import
8import
9import
0
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
0Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
4Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
2Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
0import
7import
8import
9
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
1Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
2Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
4Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
0Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
4x
3x
4=
x
6x
7x
8x
9
=
0=
=
2
=
4
=
5 =
6=
7 =
8
=
9=
6=
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
02________ 99
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
04=
________ 106=
=
________ 40Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
10=
9
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
12____88Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
14x
8x
9
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
18Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
19Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
20=
x
4Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
23Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
24Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
23Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
1
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
28Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
29import
00đầu ra
[4 0 4 3]
ví dụ 2
Phương pháp trên cũng có thể hoạt động đối với chuỗi. Trong ví dụ bên dưới, chúng tôi đang cố gắng tìm chỉ mục xuất hiện đầu tiên của 'Sam' theo thứ tự ngược lại
Python3
#import Modules
import
numpy as np
# initialize parameters
x
__=
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
0____409Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2____411Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
13import
7import
8import
9____409
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2____409Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
21import
7import
8import
9____425
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2____411Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
09import
7import
8import
9____409
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2____425Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
37import
38x
4=
x
6x
7x
8import
44
=
0=
import
47
=
4
=
5 =
6=
7 import
53
=
9=
6=
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
02________ 99
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
04=
________ 106=
=
________ 409Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
10=
9
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
12____88Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
14x
8import
44
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
18Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
19Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
20=
x
4Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
23Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
24Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
23Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
1
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
28Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
29import
00đầu ra
[ 0 1 2 ]
ví dụ 3
Đối với dữ liệu Boolean, chúng tôi có cách tiếp cận tương tự, nhưng vì nó chỉ có 0 hoặc 1 làm giá trị nên chúng tôi có thể sử dụng argmax[] để tìm chỉ mục của giá trị cao nhất [đối với mỗi hàng có trục=1]. Vì True tương đương với 1 và False tương đương với 0 nên nó sẽ ghi chỉ số của giá trị True đầu tiên
Python3
import
86
import
numpy as np
# initialize parameters
import
91=
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
0import
94Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
96Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
94Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
94import
7import
8import
9import
94
Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
96Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
96Array
[
[3] => Array
[
[artist] => The Smashing Pumpkins
[songname] => Soma
]
[4] => Array
[
[artist] => The Decemberists
[songname] => The Island
]
[1] => Array
[
[artist] => Fleetwood Mac
[songname] => Second-hand News
]
[2] => Array
[
[artist] => Jack Johnson
[songname] => Only the Ocean
]
]
2import
94numpy as np
31