Python:每日一题 177
本帖最后由 新手·ing 于 2018-5-26 12:49 编辑【矩阵转置】
输入一个矩阵字符串srcStr,输出这个矩阵的转置矩阵;
说明:
输入将以“用半角逗号隔开列,用半角分号隔开行”来描述矩阵
比如,输入的字符串格式定义如下:
a,b,c,d,e;f,g,h,i,j;k,l,m,n,o;p,q,r,s,t
其表示的矩阵如下:
a b c d e
f g h i j
k l m n o
p q r s t
注意,判题用例中输入字符串不保证每行列数一样,所以无法保证输入的字符串能换转成矩阵;解题注意处理与判断;
【输出】:string转置之后的矩阵字符串
若不能转置,输出“impossible”
输出的字符串格式定义如下(中间无空格):
a,f,k,p;b,g,l,q;c,h,m,r;d,i,n,s;e,j,o,t
其表示的矩阵如下:
a f k p
b g l q
c h m r
d i n s
e j o t
import numpy
def fun(s):
x = numpy.transpose( row.split(',') for row in s.split(';') )
ret = ';'.join( ','.join(x) for i in range( len(x)) )
return ret if s != ret else 'impossible' str1 = 'a,b,c,d,e;f,g,h,i,j;k,l,m,n,o;p,q,r,s,t'
list1 = []
for i in str1:
list1.append(i)
while ',' in list1:
list1.remove(',')
while ';' in list1:
list1.remove(';')
if len(list1) % 5 == 0:
str2 =str1.replace(',', '\t')
str3 =str2.replace(';', '\n')
print(str3)
else:
print('不能形成矩阵') 本帖最后由 asdfg5954457 于 2018-5-27 17:09 编辑
matrix = input('輸入矩陣')
matrixS = matrix.split(';')
matrixC = ''
totalrow = len(matrixS)
columns_in_row1 = len(matrixS.split(','))
bull = 0
for eachrow in range(totalrow):
bull += abs(len(matrixS.split(','))-columns_in_row1)
if bull == 0:
for eachcolumn in range(columns_in_row1):
for eachrow in range(totalrow):
matrixC = matrixC + matrixS.split(',') + ','
matrixC = matrixC.rstrip(',')
matrixC = matrixC + ';'
matrixC = matrixC.rstrip(';')
print(matrixC)
else:
print('impossible')
逗號跟分號的處理似乎是有點不漂亮,不過目前也沒想到其他比較合適的方法 完全没用过矩阵的函数,觉得好久没敲动脑子的代码,迟钝了,,
def f(x):
lst=x.replace(',','').split(';')
print(lst)
l=len(lst)
L=len(lst)
tlst=[]
for i in range(L):
if len(lst)!=l:
print('impossible')
return 0
for j in range(l):
for i in range(L):
print(lst+'\t',end='')
print()
srcStr='a,b,c,d,e;f,g,h,i,j;k,l,m,n,o;p,q,r,s,t'
f(srcStr)
f(srcStr[:-1]) # -*- coding:utf-8 -*-
def Rransposition_Str_Matrix(Str1):
m=Str1.count(';')+1
n=Str1.count(',')/m+1
if m != n:
print('不能形成矩阵')
else:
a=[ for j in range(n)]
list1=Str1.replace(',','').split(';')
Str2 = Str1
for i in range(m):
for j in range(n):
a=list1
for i in a:
b = ''.join(i)
print(b)
srcStr='a,b,c,d,e;f,g,h,i,j;k,l,m,n,o;p,q,r,s,t;u,v,w,x,y'
Rransposition_Str_Matrix(srcStr)
# -*- coding:utf-8 -*-
def Rransposition_Str_Matrix(Str1):
m=Str1.count(';')+1
n=Str1.count(',')/m+1
if m != n:
print('不能形成矩阵')
else:
a=[ for j in range(n)]
list1=Str1.replace(',','').split(';')
Str2 = Str1
for i in range(m):
for j in range(n):
a=list1
for i in a:
b = ''.join(i)
print(b)
srcStr='a,b,c,d,e;f,g,h,i,j;k,l,m,n,o;p,q,r,s,t;u,v,w,x,y'
Rransposition_Str_Matrix(srcStr)
# -*- coding:utf-8 -*-
def Rransposition_Str_Matrix(Str1):
m=Str1.count(';')+1
n=Str1.count(',')/m+1
if m != n:
print('不能形成矩阵')
else:
a=[ for j in range(n)]
list1=Str1.replace(',','').split(';')
Str2 = Str1
for i in range(m):
for j in range(n):
a=list1
for i in a:
b = ''.join(i)
print(b)
srcStr='a,b,c,d,e;f,g,h,i,j;k,l,m,n,o;p,q,r,s,t;u,v,w,x,y'
Rransposition_Str_Matrix(srcStr) 用zip做转置。
srcStr = 'a,b,c,d,e;f,g,h,i,j;k,l,m,n,o;p,q,r,s,t'
list1 =
if len(set()) == 1:
print(';'.join([','.join(i) for i in zip(*list1)]))
else:
print('impossible!')
def fun177(x):
x = x.split(';')
for n in x:
if len(n) != len(x):
print('impossible')
break
else:
for i in x:
i =
for j in i:
if j == i[-1]:
print(j)
else:
print(j,end=' ')
if __name__ == '__main__':
x = str(input('请输入字符串:'))
fun177(x) 看起来题目就是普通的矩阵转置啊,怎么现在没什么人答题了,
可能这几天大伙欧冠和NBA东西决复盘吧{:10_268:}
元素是字符串比较奇怪
手动做也不麻烦
def fun(string):
lst =
if any(len(i)-len(lst) for i in lst):
return "impossible"
else:
return [ for row in lst] for i in range(len(lst))]
如果字符串是为了代数运算,那sympy貌似可以派上用场
from sympy import Matrix
from sympy.abc import *
def fun2(string):
lst =
try:
return Matrix(lst).transpose()
except ValueError:
return "impossible" def generate_matrix(input_str):
elements_list = input_str.split(";")
matrix = []
for each in elements_list:
matrix.append(each.split(","))
return matrix
def matrix_transpose(matrix):
if len(set()) != 1:
return "Impossible"
else:
transpose_matrix = [[] for i in range(len(matrix))]
for each in matrix:
for i in range(len(each)):
transpose_matrix.append(each)
return transpose_matrix
def output_matrix(matrix):
if isinstance(matrix, str):
return matrix
else:
str_list = []
for each in matrix:
str_list.append(','.join(each))
return ';'.join(str_list)
if __name__ == '__main__':
s = input("Please input your matrix:")
transpose_mat = matrix_transpose(generate_matrix(s))
if transpose_mat != "Impossible":
for each in transpose_mat:
for i in each:
print(i, end=' ')
print()
else:
print(transpose_mat)
print(output_matrix(transpose_mat)) strings = 'a,b,c,d,e;f,g,h,i,j;k,l,m,n,o;p,q,r,s,t'
def matrix(string):
lst = string.split(';')
tmp = list(map(len, lst))
if sum(tmp) == tmp * len(tmp):
for i in lst:
print('')
for j in i:
if j != ',':
print('{0:^2}'.format(j), end='')
else:
print('impossible') def transpose(s):
arr=
lenth=len(arr)
if :
return 'impossible'
return ';'.join(','.join( for x in arr]) for k in range(lenth))
s='a,b,c,d,e;f,g,h,i,j;k,l,m,n,o;p,q,r,s,t'
print(transpose(s)) 本帖最后由 z1446773686 于 2018-5-31 14:31 编辑
def matr(x,y,coe_list,str1):
count = 1
max_x = max(x,y)
min_y = min(x,y)
ifmin_y != 1 and len(str1) / max_x == min_y:
for each in str1:
print(each,end = ' ')
if not count % max_x:
print('\n')
count += 1
else:
print('此字符串矩阵不成立')
def pro(coe_list,str1):
tit = 1
print('pro',coe_list)
for each in range(len(coe_list)-1):
tit *= coe_list
#print(tit,coe_list[-1])
return matr(tit,coe_list[-1],coe_list,str1)
def coe(str1):
coe_n = len(str1)
coe_list = []
coe_k = 2
while(coe_k<=(int(coe_n/2)+1) or not coe_n % coe_k ):
while(coe_n % coe_k):
coe_k += 1
coe_list.append(coe_k)
coe_n /= coe_k
print('coe',coe_list)
if len(str1) > 3:
x = 1
y = 1
if not len(coe_list)%2 and len(coe_list) > 2:
for each in range(int(len(coe_list)/2)):
x *= coe_list
y = x
coe_list = []
coe_list.append(x)
coe_list.append(y)
return pro(coe_list,str1)
else:
print('此字符串矩阵不成立')
return 0,0
def split_str1(str1):
str1 = list(str1)
index = 0
while(',' in str1):
if str1 == ',':
del str1
index -= 1
if index > len(str1):
index = 0
index += 1
return coe(str1)
while(True):
str1 = input('请输入一个字符串用逗号隔开中间不要有空格:')
split_str1(str1)
#print(len(str2))
'''
coe_list,str1 = coe(str2)
x,y = pro(coe_list,str1)
matr(x,y,str2)
'''
{:5_90:} 大佬们都这么厉害果真我写的是最长的,哎,慢慢来不着急
..
本帖最后由 z1446773686 于 2018-5-31 11:35 编辑def test177(srcstr):
aa =
if len(set(len(i) for i in aa)) != 1:
print('impossible')
else:
newlist = [ for i in range(len(aa))] for j in range(len(aa))]
print(';'.join([','.join(i) for i in newlist])) 本帖最后由 kinkon 于 2022-9-6 10:41 编辑
srcStr = 'a,b,c,d,e;f,g,h,i,j;k,l,m,n,o;p,q,r,s,t'
arr =
brr = ';'.join([','.join(a) for a in zip(*arr)])
print(brr) if len(brr) == len(srcStr) else print('impossible')
页:
[1]