1、使用列表生成器nums = [y for x in range(6,10) for y in (x,-x)]
print(nums)
[6, -6, 7, -7, 8, -8, 9, -9]
2、使用函数方法生成def range_with_negatives(start, end):
for x in range(start, end):
yield x
yield -x
用法:list(range_with_negatives(6, 10))
3、使用*实现>>> [*range(6, 10), *range(-9, -5)]
[6, 7, 8, 9, -9, -8, -7, -6]
4、使用itertools.chain()实现import itertools
list(itertools.chain(range(6, 10), range(-9, -5)))
或者>>> list(itertools.chain(*((x, -x) for x in range(6, 10))))
[6, -6, 7, -7, 8, -8, 9, -9]
5、使用product和starmapfrom itertools import product, starmap
from operator import mul
list(starmap(mul, product((-1, 1), range(6, 10))))