當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Integer Matrix轉String Matrix用法及代碼示例

給定一個具有整數值的矩陣,將每個元素轉換為字符串。

Input : test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6]]
Output : [['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6']] 
Explanation : All elements of Matrix converted to Strings. 
Input : test_list = [[4, 5, 7], [10, 8, 3]] 
Output : [['4', '5', '7'], ['10', '8', '3']] 
Explanation : All elements of Matrix converted to Strings.

方法#1:使用str() + 列表理解

上述方法的組合可以用來解決這個問題。在此,我們使用 str() 執行轉換,並使用列表理解來迭代所有元素。

Python3


# Python3 code to demonstrate working of 
# Convert Integer Matrix to String Matrix
# Using str() + list comprehension
# initializing list
test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
# printing original list
print("The original list : " + str(test_list))
# using str() to convert each element to string 
res = [[str(ele) for ele in sub] for sub in test_list]
# printing result 
print("The data type converted Matrix : " + str(res))
輸出
The original list : [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
The data type converted Matrix : [['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6'], ['9', '3', '6']]

方法#2:使用str() + map()

上述函數的組合也可以用來解決這個問題。在此,我們使用map()將字符串轉換擴展到所有行元素。

Python3


# Python3 code to demonstrate working of 
# Convert Integer Matrix to String Matrix
# Using str() + map()
# initializing list
test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
# printing original list
print("The original list : " + str(test_list))
# using map() to extend all elements as string  
res = [list(map(str, sub)) for sub in test_list]
# printing result 
print("The data type converted Matrix : " + str(res))
輸出
The original list : [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
The data type converted Matrix : [['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6'], ['9', '3', '6']]

所有方法的時間和空間複雜度都是相同的:

時間複雜度:O(n2)
輔助空間:在)

方法#3:使用 numpy.char.mod() 函數。

算法:

  1. 使用 numpy.array() 函數將輸入列表轉換為 numpy 數組。
  2. 使用 numpy.char.mod() 函數將數組的元素轉換為字符串。 %s 格式說明符用於指示字符串轉換。
  3. 使用 tolist() 方法將生成的 numpy 數組轉換回列表。

Python3


import numpy as np
# initializing list
test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
# printing original list
print("The original list : " + str(test_list))
matrix = np.array(test_list)
res = np.char.mod('%s', matrix).tolist()
# printing result 
print("The data type converted Matrix : " + str(res))

輸出

The original list : [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
The data type converted Matrix : [[‘4’, ‘5’, ‘7’], [’10’, ‘8’, ‘3’], [’19’, ‘4’, ‘6’], [‘9’, ‘3’, ‘6’]]

時間複雜度:

該算法的時間複雜度為 O(m * n),其中 m 是輸入矩陣的行數,n 是輸入矩陣的列數。這是因為我們對矩陣的每個元素進行一次迭代,將其轉換為字符串。

空間複雜度:

該算法的空間複雜度也是 O(m * n),因為我們正在創建一個與輸入矩陣大小相同的新 numpy 數組,然後將其轉換為列表。但是,numpy 數組的內存使用通常比 Python 列表更高效,因此此方法可能比其他使用 Python 列表的方法更節省內存。

迭代字符串轉換

我們可以迭代矩陣的每個元素,並使用 str() 函數將其轉換為字符串。我們將轉換後的元素存儲在與原始矩陣大小相同的新矩陣中。

腳步:

  1. 初始化一個與輸入矩陣大小相同的空矩陣。
  2. 使用嵌套循環迭代輸入矩陣中的每個元素。
  3. 使用str() function將每個元素轉換為字符串並將其存儲在新矩陣中的相應位置。
  4. 返回新的矩陣。

Python3


def convert_matrix_to_string(test_list):
    rows = len(test_list)
    cols = len(test_list[0])
    string_matrix = [[''] * cols for _ in range(rows)]
    for i in range(rows):
        for j in range(cols):
            string_matrix[i][j] = str(test_list[i][j])
    return string_matrix
test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6]]
string_matrix = convert_matrix_to_string(test_list)
print(string_matrix)
輸出
[['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6']]

這種方法的時間複雜度為 O(m*n),其中 m 和 n 是輸入矩陣的維度。
這種方法使用的輔助空間也是O(m*n)。

方法5:使用嵌套循環。

分步方法:

  • 初始化一個空列表來存儲轉換後的字符串矩陣。
  • 使用嵌套循環迭代整數矩陣的元素。
  • 使用 str() 函數將每個元素轉換為字符串並將其附加到臨時列表中。
  • 轉換一行中的所有元素後,將臨時列表追加到結果列表中。
  • 對整數矩陣中的所有行重複上述步驟。
  • 打印原始列表和轉換後的字符串矩陣。

Python3


# initializing list
test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
# printing original list
print("The original list : " + str(test_list))
# converting integer matrix to string matrix
str_matrix = []
for row in test_list:
    str_row = []
    for element in row:
        str_row.append(str(element))
    str_matrix.append(str_row)
# printing result
print("The data type converted Matrix : " + str(str_matrix))
輸出
The original list : [[4, 5, 7], [10, 8, 3], [19, 4, 6], [9, 3, 6]]
The data type converted Matrix : [['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6'], ['9', '3', '6']]

時間複雜度:O(mn),其中 m 是矩陣的行數,n 是矩陣的列數。
輔助空間:O(mn),用於存儲轉換後的字符串矩陣。



相關用法


注:本文由純淨天空篩選整理自manjeet_04大神的英文原創作品 Python – Convert Integer Matrix to String Matrix。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。