Python编程的相关问题
发布网友
发布时间:2022-04-23 01:30
我来回答
共2个回答
热心网友
时间:2023-06-28 08:02
我的理解可能是要做列表排序
# coding: utf-8
l = [1,2,3,4,
1,2,3,4,
1,2,3,4,
1,2,3,4,
]
# 每一行组成一个数组
def rows(width, data):
l = []
l1 = []
for i in range(len(data)):
l1.append(data[i])
if not (i + 1) % width:
l.append(l1)
l1 = []
return l
# 每一列组成一个数组
def coulmns(width, data):
l = []
d = {}
for i in range(len(data)):
remainder = (i - width) % width
if d.get(remainder):
d[remainder].append(data[i])
else:
d[remainder] = [data[i]]
for l1 in d.values():
l.append(l1)
return l
print "rows:", rows(4, l)
print "coulmns", coulmns(4,l)
rows: [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
coulmns [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]
热心网友
时间:2023-06-28 08:02
希望满足你的要求。
sentinel = 'end' # 要结束输入,最后一行输入 end
matrix = []
for line in iter(input, sentinel):
matrix.append(line.split(' '))
print(matrix)
for index_x, x in enumerate(matrix):
# 动态生成变量,Python 中所有的变量名称和值都存储在 locals 字典中
locals()['Lx{index}'.format(index=index_x + 1)] = x
print('Lx{index} = {value}'.format(index=index_x + 1, value=x))
for index_y, y in enumerate(map(list, zip(*matrix))):
# 动态生成变量
locals()['Ly{index}'.format(index=index_y + 1)] = y
print('Ly{index} = {value}'.format(index=index_y + 1, value=y))
print(locals())
参考资料:
1. 多行输入
How do I read multiple lines of raw input in Python? - Stack Overflow
python3如何进行多行输入? - 知乎
2. 动态生成变量名
Python 动态生成变量名 - CSDN博客