Python - 将字符串转换为每行 K 个字符的矩阵
给定一个字符串,将其转换为矩阵,每行有 K 个字符。
Input : test_str = ‘GeeksforGeeks is best’, K = 7
Output : [[‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’], [‘r’, ‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘ ‘], [‘i’, ‘s’, ‘ ‘, ‘b’, ‘e’, ‘s’, ‘t’]]
Explanation : Each character is assigned to 7 element row in matrix.
Input : test_str = ‘GeeksforGeeks ‘, K = 7
Output : [[‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’], [‘r’, ‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘ ‘]]
Explanation : Each character is assigned to 7 element row in matrix.
方法#1:使用列表理解+切片
以上功能的组合可以用来解决这个问题。在这种情况下,我们首先使用切片和列表理解为每一行提取单独的字符串。然后使用list()将每个字符串转换为字符列表。
Python3
# Python3 code to demonstrate working of
# Convert String to K characters row Matrix
# Using list comprehension + slicing
# Function to Convert String
# to K characters row Matrix
def convertToMatrix(test_str, K):
# slicing strings
temp = [test_str[idx: idx + K] for idx in range(0, len(test_str), K)]
# conversion to list of characters
res = [list(ele) for ele in temp]
# printing result
print("The converted Matrix : " + str(res))
# Driver Code
# initializing string
input_str = 'GeeksforGeeks is best'
# printing original string
print("The original string is : " + str(input_str))
# initializing K
K = 7
# calling the function
convertToMatrix(input_str, K)
Python3
# Python3 code to demonstrate working of
# Convert String to K characters row Matrix
# Using list comprehension + map() + slicing
# Function to Convert String
# to K characters row Matrix
def convertToMatrix(test_str, K):
# slicing strings
temp = [test_str[idx: idx + K] for idx in range(0, len(test_str), K)]
# conversion using map
res = list(map(lambda ele: list(ele), temp))
# printing result
print("The converted Matrix : " + str(res))
# Driver Code
# initializing string
input_str = 'GeeksforGeeks is best'
# printing original string
print("The original string is : " + str(input_str))
# initializing K
K = 7
# calling the function
convertToMatrix(input_str, K)
输出:
The original string is : GeeksforGeeks is best
The converted Matrix : [[‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’], [‘r’, ‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘ ‘], [‘i’, ‘s’, ‘ ‘, ‘b’, ‘e’, ‘s’, ‘t’]]
方法 #2:使用列表理解 + map() + 切片
这是可以执行此任务的另一种方式。在这里,我们以与上述函数类似的方式执行任务,不同之处在于使用map()而不是列表理解来完成转换为列表。
蟒蛇3
# Python3 code to demonstrate working of
# Convert String to K characters row Matrix
# Using list comprehension + map() + slicing
# Function to Convert String
# to K characters row Matrix
def convertToMatrix(test_str, K):
# slicing strings
temp = [test_str[idx: idx + K] for idx in range(0, len(test_str), K)]
# conversion using map
res = list(map(lambda ele: list(ele), temp))
# printing result
print("The converted Matrix : " + str(res))
# Driver Code
# initializing string
input_str = 'GeeksforGeeks is best'
# printing original string
print("The original string is : " + str(input_str))
# initializing K
K = 7
# calling the function
convertToMatrix(input_str, K)
输出:
The original string is : GeeksforGeeks is best
The converted Matrix : [[‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’], [‘r’, ‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘ ‘], [‘i’, ‘s’, ‘ ‘, ‘b’, ‘e’, ‘s’, ‘t’]]