在 Python 2.7 中,如果我有一个像这样的函数:
def holes_between(intervals):
# Compute the holes between the intervals, for example:
# given the intervals: ([ 8, 9] [14, 18] [19, 20] [23, 32] [34, 49])
# compute the holes: ([10, 13] [21, 22] [33, 33])
prec = intervals[0][1] + 1 # Bootstrap the iteration
for low, high in intervals[1:]:
if prec <= low - 1:
yield (prec, low - 1)
prec = high + 1
holes = list(holes_between(intervals))
由于函数的 yield
被收集到一个 list
中,所以在 holes_between
函数内部构建该列表是否更有效率?如果是,最有效率的方法是什么?