Trong bài viết này được cung cấp một ma trận, nhiệm vụ là viết chương trình Python để tính chỉ số bắt đầu của tất cả các danh sách lồng nhau
Thí dụ
Đầu vào. test_list = [[5], [9, 3, 1, 4], [3, 2], [4, 7, 8, 3, 1, 2], [3, 4, 5]]
đầu ra. [0, 1, 5, 7, 13]
Giải trình. 1 + 4 = độ dài của 2 danh sách ban đầu = 5, 3, của danh sách thứ 3 bắt đầu từ chỉ mục thứ 5 [ 0 based indexing ],
do đó 5. là phần tử thứ 3 trong danh sách kết quả
Đầu vào. test_list = [[5], [9, 3, 1, 4], [3, 2], [3, 4, 5]]
đầu ra. [0, 1, 5, 7]
Giải trình. 1 + 4 = độ dài của 2 danh sách ban đầu = 5, 3, của danh sách thứ 3 bắt đầu từ chỉ mục thứ 5 [ 0 based indexing ],
do đó 5. là phần tử thứ 3 trong danh sách kết quả
Phương pháp số 1. Sử dụng vòng lặp + len[]
Trong trường hợp này, độ dài của mỗi danh sách con được tính bằng cách sử dụng len[] và tính tổng, cộng dồn và cộng làm kết quả trung gian. Chỉ mục ban đầu là dẫn xuất của độ dài của danh sách con
Python3
# Python3 code to demonstrate working of
# Initial element index in Matrix
# Using loop
# initializing list
test_list
__=
[[
5
], [
# Python3 code to demonstrate working of
0# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
2# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
4# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
6], [
# Python3 code to demonstrate working of
2# Python3 code to demonstrate working of
1# Initial element index in Matrix
0], [
# Python3 code to demonstrate working of
6# Python3 code to demonstrate working of
1# Initial element index in Matrix
4# Python3 code to demonstrate working of
1# Initial element index in Matrix
6# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
2# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
4# Python3 code to demonstrate working of
1# Initial element index in Matrix
0], [
# Python3 code to demonstrate working of
2# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
6# Python3 code to demonstrate working of
15
# initializing list
0
# initializing list
1# initializing list
2# initializing list
3 # initializing list
4 # initializing list
5# initializing list
6
# initializing list
7=
# initializing list
9
test_list
0=
test_list
2
test_list
3 test_list
4test_list
5 test_list
6
test_list
7test_list
8
test_list
7=
0
test_list
7test_list
0# initializing list
4=
=
5=
6
=
7
# initializing list
1# initializing list
2[[
0 # initializing list
4 # initializing list
5[[
3
đầu ra
Danh sách ban đầu là. [[5], [9, 3, 1, 4], [3, 2], [4, 7, 8, 3, 1, 2], [3, 4, 5]]
Chỉ số phần tử ban đầu. [0, 1, 5, 7, 13]
Phương pháp #2. Sử dụng tích lũy[] + map[] + len[]
Trong phần này, chúng tôi thực hiện nhiệm vụ lấy tổng bằng cách sử dụng tích lũy [], map [] được sử dụng để lấy độ dài của tất cả các danh sách con được tính bằng cách sử dụng len [].
Python3
# Python3 code to demonstrate working of
# Initial element index in Matrix
[[
6
[[
7 [[
8[[
9 5
0
# initializing list
test_list
__=
[[
5
], [
# Python3 code to demonstrate working of
0# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
2# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
4# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
6], [
# Python3 code to demonstrate working of
2# Python3 code to demonstrate working of
1# Initial element index in Matrix
0], [
# Python3 code to demonstrate working of
6# Python3 code to demonstrate working of
1# Initial element index in Matrix
4# Python3 code to demonstrate working of
1# Initial element index in Matrix
6# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
2# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
4# Python3 code to demonstrate working of
1# Initial element index in Matrix
0], [
# Python3 code to demonstrate working of
2# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
6# Python3 code to demonstrate working of
15
# initializing list
0
# initializing list
1# initializing list
2# initializing list
3 # initializing list
4 # initializing list
5# initializing list
6
# Python3 code to demonstrate working of
24
# Python3 code to demonstrate working of
25
# initializing list
7__=
# Python3 code to demonstrate working of
28test_list
2# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
31# Python3 code to demonstrate working of
32# Python3 code to demonstrate working of
33# initializing list
2=
5# Python3 code to demonstrate working of
36# Python3 code to demonstrate working of
37# Python3 code to demonstrate working of
4# Python3 code to demonstrate working of
39
=
7
# initializing list
1# initializing list
2[[
0 # initializing list
4 # initializing list
5[[
3
đầu ra
Danh sách ban đầu là. [[5], [9, 3, 1, 4], [3, 2], [4, 7, 8, 3, 1, 2], [3, 4, 5]]
Chỉ số phần tử ban đầu. [0, 1, 5, 7, 13]
Phương pháp #3. Sử dụng kiểu [] và vòng lặp và câu lệnh if
Trong phần này, chúng tôi chỉ cần kiểm tra loại phần tử trong danh sách nếu đó là danh sách khác, chúng tôi in chỉ mục của nó nếu không. Phương pháp này sẽ hoạt động bất kể số lượng phần tử loại không phải danh sách trong danh sách
Python3
# Python3 code to demonstrate working of
47
# Python3 code to demonstrate working of
48
# Python3 code to demonstrate working of
49=
[[
# Python3 code to demonstrate working of
4# Python3 code to demonstrate working of
1# Initial element index in Matrix
0# Python3 code to demonstrate working of
1# Python3 code to demonstrate working of
2# Python3 code to demonstrate working of
57# Python3 code to demonstrate working of
6# Python3 code to demonstrate working of
15
# Python3 code to demonstrate working of
61# Python3 code to demonstrate working of
62# Python3 code to demonstrate working of
1# Initial element index in Matrix
4# Python3 code to demonstrate working of
1# Initial element index in Matrix
6# Python3 code to demonstrate working of
57# Python3 code to demonstrate working of
0# Python3 code to demonstrate working of
1test_list
2# Python3 code to demonstrate working of
61# Python3 code to demonstrate working of
72# Using loop
9