Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Traverse all paths of a two-dimensional matrix

Shulou Source: shulou.com Published: 2022-06-03 07:39:09 09月14日 Update

#! / usr/bin/env python

#-*-coding: utf-8-*-

Def printList (datalist = []):

For i in range (len (datalist)):

Print datalist [i]

Def mergeList (list1, list2 = []):

If len (list1) = = 0:

Return list2

If len (list2) = = 0:

Return list1

List3 = []

For list1_i in range (len (list1)):

For list2_i in range (len (list2)):

List3_item = str (List1 [List1 _ I]) +'-'+ str (List2 [List2 _ I])

List3.append (list3_item)

Return list3

Def row2columnList (datalist = []):

List_cr = [[]]

For col in range (len (datalist [0])):

List_tmp = []

For row in range (len (datalist)):

List_tmp.append (datalist [row] [col])

List_cr.append (list_tmp)

Return list_cr

Def traversalPath (datalist = []):

List_tmp = []

For row in range (len (datalist)):

List_tmp = mergeList (list_tmp, datalist [row])

Return list_tmp

If _ _ name__=='__main__':

M = [['a _ 1,'b _ 1,'c _ 1,'d _ 1,'e _ 1], ['a _ 2,'b _ 2, c _ 2, d _ 2, e _ 2], [a _ 3, b _ 3, c _ 3, d _ 3, e _ 3]]

M_tmp = row2columnList (M)

Tp_list = traversalPath (M_tmp)

PrintList (tp_list)

Print len (tp_list)

Tags: Utf-8 Matrix path 2D Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi Shulou Information MariaDB MySQL