如何将非连续数字添加到范围? [英] How can I add non-sequential numbers to a range?
本文介绍了如何将非连续数字添加到范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试遍历 range(750, 765) 并添加非连续数字 769、770、774.如果我尝试在 range 函数之后添加数字,它会返回范围列表,然后是单个数字:
<预><代码>>>> for x in range(750, 765), 769, 770, 774: 打印 x...[750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764]769770774如何获取单个列表中的所有数字?
解决方案
使用内置 + 运算符将非连续数字附加到范围.
for x in range(750, 765) + [769, 770, 774]: 打印 x
I am trying to iterate through the range(750, 765) and add the non-sequential numbers 769, 770, 774. If I try adding the numbers after the range function, it returns the range list, then the individual numbers:
>>> for x in range(750, 765), 769, 770, 774: print x
...
[750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764]
769
770
774
How can I get all the numbers in a single list?
解决方案
Use the built-in + operator to append your non-sequential numbers to the range.
for x in range(750, 765) + [769, 770, 774]: print x
这篇关于如何将非连续数字添加到范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文