Concatenating two range function results

前端 未结 8 1298
臣服心动
臣服心动 2020-11-27 17:04

Does range function allows concatenation ? Like i want to make a range(30) & concatenate it with range(2000, 5002). So my concatenated range w

8条回答
  •  独厮守ぢ
    2020-11-27 17:29

    I came to this question because I was trying to concatenate an unknown number of ranges, that might overlap, and didn't want repeated values in the final iterator. My solution was to use set and the union operator like so:

    range1 = range(1,4)
    range2 = range(2,6)
    concatenated = set.union(set(range1), set(range2)
    for i in concatenated:
        print(i)
    

提交回复
热议问题