我有一份清单,清单如下:
MyList = [[[[11, 12], [13, 14]], [[12, 13], [22, 23]], [[24, 34], [53, 54]], [[43, 44], [54, 55]]],
[[[12, 13], [22, 23]], [[11, 12], [13, 14]], [[15, 25], [44, 54]], [[24, 34], [53, 54]]],
[[[13, 14], [21, 31]], [[15, 25], [44, 54]], [[52, 53], [54, 55]]],
[[[15, 25], [44, 54]], [[12, 13], [22, 23]], [[13, 14], [21, 31]]],
[[[24, 34], [53, 54]], [[11, 12], [13, 14]], [[12, 13], [22, 23]]],
[[[34, 35], [45, 55]], [[52, 53], [54, 55]]],
[[[43, 44], [54, 55]], [[11, 12], [13, 14]]],
[[[52, 53], [54, 55]], [[13, 14], [21, 31]], [[34, 35], [45, 55]]]]
在这个列表中,我有8项。我想这样重命名这些项目:
^{pr2}$最后,重命名后的列表如下所示:
MyListRename = [[1, 2, 5, 7],
[2, 1, 4, 5],
[3, 4, 8],
[4, 2, 3],
[5, 1, 2],
[6, 8],
[7, 1],
[8, 3, 6]]
在python中最好的方法是什么?在
我将以编程的方式执行此操作,利用每个列表的第一个元素是可能元素的列表:
注意:列表通常不能用于字典键。在本例中,我们使用^{} 来获取字符串形式的表示,以用作键
然后我们可以通过以下方式压缩列表:
^{pr2}$并通过以下方式重建原始文件:
如果还可以使用零索引,则可以删除+/-1以稍微降低复杂性。在
您可以使用以下函数:
然后执行一个双循环
^{pr2}$for
来更改MyList
中的值:甚至可以将
map
用于列表理解(此解决方案绝对可以改进)
我正在使用一个字典,其中的键是表示要替换的列表的元组。请记住,列表是可变的,因此不是散列的。在
相关问题 更多 >
编程相关推荐