如何将范围作为列表元素传递给 python 中的列表?
How to pass a range as a list element to a list in python?
我想将一系列数字传递给列表。
在这里,我设置了包含索引号列表的变量 rowsList。
rowsList = [range(13),17,23]
for index in rowsList:
ws.insert_rows(index,1)
显然它会引发:TypeError: 'range' object cannot be interpreted as an integer。我可以做些什么小改动来完成这项工作?
感谢您的帮助。
假设你想要[17, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
rowsList = [17, 23]
rowsList.extend(range(13))
假设你想要[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 17, 23]
rowsList = [17, 23]
tmpList = []
tmpList.extend(range(13))
rowsList[:0] = tmpList
* (Extended Iterable Unpacking) operator
Proposes a change to iterable unpacking syntax, allowing to specify a "catch-all" name which will be assigned a list of all items not
assigned to a "regular" name.
rowsList = [*range(13),17,23]
print (rowsList)
输出:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 17, 23]
我想将一系列数字传递给列表。 在这里,我设置了包含索引号列表的变量 rowsList。
rowsList = [range(13),17,23]
for index in rowsList:
ws.insert_rows(index,1)
显然它会引发:TypeError: 'range' object cannot be interpreted as an integer。我可以做些什么小改动来完成这项工作? 感谢您的帮助。
假设你想要[17, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
rowsList = [17, 23]
rowsList.extend(range(13))
假设你想要[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 17, 23]
rowsList = [17, 23]
tmpList = []
tmpList.extend(range(13))
rowsList[:0] = tmpList
* (Extended Iterable Unpacking) operator
Proposes a change to iterable unpacking syntax, allowing to specify a "catch-all" name which will be assigned a list of all items not assigned to a "regular" name.
rowsList = [*range(13),17,23]
print (rowsList)
输出:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 17, 23]