问题描述
我有一个列表,可以有不同的大小,但总是有第一项。 我写这样的理解:
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']]
1楼
如果我理解正确,那么您想使用[i[0][7:]] + i[1:]
。
2楼
好的,谢谢您的输入/输出数据。 然后,正如BrenBarn和Anand S Kumar所建议的那样
return {
'index': [[i[0][7:]]+i[1:]] for i in columns if i[0].startswith("index::")]
}