列表功能 - 确定正/负值以及它们是否大于/小于

时间:2014-12-10 06:26:08

标签: python list sum

好的,我给出了以下问题:给定一个整数列表L,写代码以确定列表中正整数的总和是 大于列表中负数之和(的绝对值)并打印一个合适的 信息。

这就是我提出的代码,但它不起作用它只是返回括号内的输入数字ex input = -1,-2,4,5 output =( - 1,-2 ,4,5)

def question(L):
   L = input()
   sumPositive = () #List where I will send the positive integers from "L" list
   sumNegative = () #List where I will send the negative integers from "L" list

   if x in L >= 0:
      append.x(sumPositive) #checks if the number is equal to or greater than 0, if so add it to          "sumPositive" list
  elif:
      append.x(sumNegative) #if not add it to "sumNegative" list

if sum(sumPositive) > abs(sum(sumNegative)):
   print "The sum of positive numbers is greater than the absolute value of negative numbers."
   elif: 
      sum(Positive) < abs(sum(sumNegative)):
         print "The sum of absolute value of negative numbers is greater than the sum of positive         numbers."
   else:
   print "They are equal."

任何人都可以告诉我哪里出错或者我是否出错了。谢谢!

4 个答案:

答案 0 :(得分:1)

更多的pythonic解决方案:

def question(lst):
    sum_pos = sum(x for x in lst if x >0)
    sum_neg = sum(abs(x) for x in lst if x<0)

    if sum_pos > sum_neg:
        print "The sum of positive numbers is greater than the absolute value of negative numbers."
    elif sum_pos < sum_neg:
        print "The sum of absolute value of negative numbers is greater than the sum of positive numbers."
    else:
        print "They are equal."

question([1,-1,-2,-2])

答案 1 :(得分:0)

()创建一个空元组,而不是列表。 append()是一种列表方法,例如foo.append(bar)

答案 2 :(得分:0)

def question(L):

   sumPositive = [] #List where I will send the positive integers from "L" list
   sumNegative = [] #List where I will send the negative integers from "L" list

   for x in L:
       if x>0:
           sumPositive.append(x)
       else :
           sumNegative.append(x) #if not add it to "sumNegative" list

   if sum(sumPositive) > abs(sum(sumNegative)):
       print "The sum of positive numbers is greater than the absolute value of negative numbers."
   elif sum(sumPositive) < abs(sum(sumNegative)):
         print "The sum of absolute value of negative numbers is greater than the sum of positive         numbers."
   else:
        print "They are equal."

question([1,-1,-2,-2])

您可以在列表中进行应用。你创建了一个元组。并且您必须使用for循环来遍历输入列表。我不明白你为什么使用if条件。

在python tuples are immutable中。你无法改变它们。你可以连接元组。但我相信这里不需要。列表就足够了。

答案 3 :(得分:0)

>>> def question(x):
...    s1=sum([i for i in x if i>0])
...    s2=sum([abs(i) for i in x if i<0])
...    if s1>s2:
...       print "The sum of positive numbers is greater than the absolute value of negative numbers."
...    elif s1<s2:
...       print "The sum of absolute value of negative numbers is greater than the sum of positive numbers."
...    else:
...       print "They are equal."
... 
>>> lst = [1,2,3,-1,-5,-7,3,-4,5,-6]
>>> question(lst)
The sum of absolute value of negative numbers is greater than the sum of positive numbers.
>>> lst=[-1,-2,3,2,3]
>>> question(lst)
The sum of positive numbers is greater than the absolute value of negative numbers.
>>> lst=[-2,-3,-3,3,2,3]
>>> question(lst)
They are equal.