简体   繁体   English

如何将范围作为列表元素传递给python中的列表?

[英]How to pass a range as a list element to a list in python?

I want to pass a range of numbers to a list. 我想将一系列数字传递给列表。 Here I set the variable rowsList which contain a list of index numbers. 在这里,我设置了变量rowsList,其中包含索引号列表。

rowsList = [range(13),17,23]
for index in rowsList:
    ws.insert_rows(index,1)

Obviously it raises: TypeError: 'range' object cannot be interpreted as an integer. 显然会引发:TypeError:“范围”对象不能解释为整数。 What little change could I do to make this work ? 我可以做些什么改变才能使它正常工作? Thanks for your help. 谢谢你的帮助。

* (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)

output : 输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 17, 23]

Assuming you want [17, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 假设您想要[17, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

rowsList = [17, 23]
rowsList.extend(range(13))

Assuming you want [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 17, 23] 假设您想要[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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM