结合范围和数字列表的python [英] python combining a range and a list of numbers
问题描述
range(5, 15) [1, 1, 5, 6, 10, 10, 10, 11, 17, 28]
range(6, 24) [4, 10, 10, 10, 15, 16, 18, 20, 24, 30]
range(7, 41) [9, 18, 19, 23, 23, 26, 28, 40, 42, 44]
range(11, 49) [9, 23, 24, 27, 29, 31, 43, 44, 45, 45]
range(38, 50) [1, 40, 41, 42, 44, 48, 49, 49, 49, 50]
我从功能的打印命令中获得上述支出.我真正想要的是范围的组合列表,例如,在顶行5,6,7 ... 15,1、1,5,6等. 输出范围来自
I get the above outpout from a print command from a function. What I really want is a combined list of the range, for example in the top line 5,6,7...15,1,1,5,6 etc. The output range comes from
range_draws=range(int(lower),int(upper))
我天真地认为会给出一个范围.其他数字来自切片列表.
which I naively thought would give a range. The other numbers come from a sliced list.
有人可以帮助我获得预期的结果吗?
Could someone help me to get the desired result.
推荐答案
range()
函数返回特殊的范围对象保存在内存中(只有开始,结束和步长大小时,无需将所有数字保留在内存中).将其投射到列表以扩展"它:
The range()
function returns a special range object to save on memory (no need to keep all the numbers in memory when only the start, end and step size will do). Cast it to a list to 'expand' it:
list(yourrange) + otherlist
引用文档:
range
类型相对于常规list
或range
对象将始终占用相同(少量)的内存,无论它表示的范围大小如何(因为它仅存储start
,stop
和step
值,并根据需要计算各个项目和子范围.
The advantage of the
range
type over a regularlist
ortuple
is that arange
object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores thestart
,stop
andstep
values, calculating individual items and subranges as needed).
这篇关于结合范围和数字列表的python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!