我如何根据一定的限制(解释如下)生成一个列表,它从无穷远处取数,0总是在中间?你知道吗
例如:
NumberOfItem = 5 #this will decide there will be only 5 item in the list
dfNum= 1 #different between number
this list should look:
[-2,-1,0,1,2]
另一个例子是在偶数的情况下,负数将大于正数+1项
NumberOfItem = 4
dfNum= 0.5
this list should look:
[-1.0,-0.5,0,0.5,]
最好找到一个不使用循环的解决方案(例如。。(或同时)
您可以使用以下功能:
编辑:
按如下方式使用:
my_list = make_list(5,1)
或my_list = make_list(4,0.5)
或print make_list(3,1)
括号中的第一个值是NumberOfItem,第二个值是dfNum。你知道吗
这是一种使用列表理解的方法:
一般来说:
你所说的“差异”也可以看作是“比例因子”。你知道吗
相关问题 更多 >
编程相关推荐