Danh sách lồng nhau Lập chỉ mục Python |

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 of0# Python3 code to demonstrate working of1# Python3 code to demonstrate working of2# Python3 code to demonstrate working of1# Python3 code to demonstrate working of4# Python3 code to demonstrate working of1# Python3 code to demonstrate working of6], [# Python3 code to demonstrate working of2# Python3 code to demonstrate working of1# Initial element index in Matrix0], [# Python3 code to demonstrate working of6# Python3 code to demonstrate working of1# Initial element index in Matrix4# Python3 code to demonstrate working of1# Initial element index in Matrix6# Python3 code to demonstrate working of1# Python3 code to demonstrate working of2# Python3 code to demonstrate working of1# Python3 code to demonstrate working of4# Python3 code to demonstrate working of1# Initial element index in Matrix0], [# Python3 code to demonstrate working of2# Python3 code to demonstrate working of1# Python3 code to demonstrate working of6# Python3 code to demonstrate working of15

 

# initializing list0

# initializing list1# initializing list2# initializing list3 # initializing list4 # initializing list5# initializing list6

 

# initializing list7= # initializing list9

test_list0= test_list2

test_list3 test_list4test_list5 test_list6

 

test_list7test_list8

test_list7=0

test_list7test_list0# initializing list4= =5=6

 

=7

# initializing list1# initializing list2[[0 # initializing list4 # initializing list5[[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 50

 

# initializing list

test_list__= [[5], [# Python3 code to demonstrate working of0# Python3 code to demonstrate working of1# Python3 code to demonstrate working of2# Python3 code to demonstrate working of1# Python3 code to demonstrate working of4# Python3 code to demonstrate working of1# Python3 code to demonstrate working of6], [# Python3 code to demonstrate working of2# Python3 code to demonstrate working of1# Initial element index in Matrix0], [# Python3 code to demonstrate working of6# Python3 code to demonstrate working of1# Initial element index in Matrix4# Python3 code to demonstrate working of1# Initial element index in Matrix6# Python3 code to demonstrate working of1# Python3 code to demonstrate working of2# Python3 code to demonstrate working of1# Python3 code to demonstrate working of4# Python3 code to demonstrate working of1# Initial element index in Matrix0], [# Python3 code to demonstrate working of2# Python3 code to demonstrate working of1# Python3 code to demonstrate working of6# Python3 code to demonstrate working of15

 

# initializing list0

# initializing list1# initializing list2# initializing list3 # initializing list4 # initializing list5# initializing list6

 

# Python3 code to demonstrate working of24

# Python3 code to demonstrate working of25

# initializing list7__= # Python3 code to demonstrate working of28test_list2# Python3 code to demonstrate working of1# Python3 code to demonstrate working of31# Python3 code to demonstrate working of32# Python3 code to demonstrate working of33# initializing list2=5# Python3 code to demonstrate working of36# Python3 code to demonstrate working of37# Python3 code to demonstrate working of4# Python3 code to demonstrate working of39

 

=7

# initializing list1# initializing list2[[0 # initializing list4 # initializing list5[[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 of47

# Python3 code to demonstrate working of48

# Python3 code to demonstrate working of49= [[# Python3 code to demonstrate working of4# Python3 code to demonstrate working of1# Initial element index in Matrix0# Python3 code to demonstrate working of1# Python3 code to demonstrate working of2# Python3 code to demonstrate working of57# Python3 code to demonstrate working of6# Python3 code to demonstrate working of15# Python3 code to demonstrate working of61# Python3 code to demonstrate working of62# Python3 code to demonstrate working of1# Initial element index in Matrix4# Python3 code to demonstrate working of1# Initial element index in Matrix6# Python3 code to demonstrate working of57# Python3 code to demonstrate working of0# Python3 code to demonstrate working of1test_list2# Python3 code to demonstrate working of61# Python3 code to demonstrate working of72# Using loop9

Chủ Đề