我需要获取一个整数列表并将其元素相乘,以便获得最大数量。这个号码,我需要输出一个字符串。 例如:
(int list) givenList = [2, 0, 2, 2, 0]
Output: 8
//because (2)*(2)*(2)=8
(int list) givenList= [-2, -3, 4, -5]
Output:60
//because (-3)*(-5)*(4)=60
这是我的代码:
import operator
xs = [0,0]
if xs.count(0) == len(xs) :
print ("0")
elif xs.count(0) == len(xs)-1:
print ("0")
else:
negatives =[]
legits = []
listLength=len(xs)
counter = 0
while counter < listLength:
if xs[counter] <0:
negatives.append(xs[counter])
if xs[counter] > 0:
legits.append(xs[counter])
counter = counter +1
if len(negatives)%2 !=0:
negatives[negatives.index(max([n for n in negatives if n<0]))] = 1
newList = negatives + legits
print str((reduce(operator.mul, newList)))