Python: 如何将剩余的列表元素添加到列表中,类似于解包?
Python: how to add remaining list elements to list, similar to unpack?
我有一个列表,它可以有不同的大小,但它总是有第一项。我写这样的理解:
return {
'index': [[i[0][7:], i[1], i[2], i[3]] for i in columns if i[0].startswith("index::")]
}
i[1], i[2], i[3]
部分可能会有所不同。它可以有 0 个或更多大小,我需要将它们指定为列表元素。
类似
return {
'index': [[i[0][7:], *i[1:]] for i in columns if i[0].startswith("index::")]
}
那就太好了。
输入:
[['index::test', '1', '2', '3']]
[['index::test', '1', '2', '3', '5']]
[['index::test']]
输出:
[['index', '1', '2', '3']]
[['index', '1', '2', '3', '5']]
[['index']]
如果我没理解错的话,你想使用 [i[0][7:]] + i[1:]
。
好的,感谢您提供 input/output 数据。
然后,正如 BrenBarn 和 Anand S Kumar 所建议的那样
return {
'index': [[i[0][7:]]+i[1:]] for i in columns if i[0].startswith("index::")]
}
我有一个列表,它可以有不同的大小,但它总是有第一项。我写这样的理解:
return {
'index': [[i[0][7:], i[1], i[2], i[3]] for i in columns if i[0].startswith("index::")]
}
i[1], i[2], i[3]
部分可能会有所不同。它可以有 0 个或更多大小,我需要将它们指定为列表元素。
类似
return {
'index': [[i[0][7:], *i[1:]] for i in columns if i[0].startswith("index::")]
}
那就太好了。
输入:
[['index::test', '1', '2', '3']]
[['index::test', '1', '2', '3', '5']]
[['index::test']]
输出:
[['index', '1', '2', '3']]
[['index', '1', '2', '3', '5']]
[['index']]
如果我没理解错的话,你想使用 [i[0][7:]] + i[1:]
。
好的,感谢您提供 input/output 数据。 然后,正如 BrenBarn 和 Anand S Kumar 所建议的那样
return {
'index': [[i[0][7:]]+i[1:]] for i in columns if i[0].startswith("index::")]
}