python - 如何将树形元组转换为矩阵形元组?

标签 python python-3.x data-structures tuples

如果我的问题显得太天真或太基本,请提前原谅。我还在学习使用 SE。

是否有一种快速方法将树形元组转换为矩阵形式元组(三角形形式)?

就是这样:

t=((1,),(2,3,),(4,5,6,),(7,8,9,10,))

对此:

t=((1,0,0,0,),(2,3,0,0,),(4,5,6,0,),(7,8,9,10,))

只要有一个想法就足够了。我可以尝试自己实现。

最佳答案

如果长度小于4可以添加0

tuple(a+(0,)*(4-len(a)) for a in t)
((1, 0, 0, 0), (2, 3, 0, 0), (4, 5, 6, 0), (7, 8, 9, 10))

关于python - 如何将树形元组转换为矩阵形元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33698810/

相关文章:

python - 异步 : Is it possible to release control of the event loop without using yield from?

c - 在链接列表的开头插入字符串时出错

c - 出现在 C 数组末尾的随机字符

python - 使用gunicorn时没有名为 'flask_cors'的模块

python - 正则表达式:如何使用不包括数字的空格/连字符捕获单词?

python - 分解 python 列表,即从 python 列表创建变量

python - 梵文的音节化

data-structures - 在 Redis 中将集合存储为散列值

python - 迭代时无法修改 Pandas DataFrame

python - 使用 create_engine 从 SQLAlchemy 到 MSSQL