简体   繁体   English

如何将清单加在一起

[英]How to add list together

I'm trying to figure out a question and I'm getting confused. 我试图找出一个问题,并且感到困惑。 Basically I have a list and it's supposed to represent a bank statement. 基本上我有一个清单,它应该代表银行对帐单。 I'm trying to add the list together so the negative numbers, which is supposed to represent withdrawl, is added together and the positive numbers are added together to represent the deposits. 我正在尝试将列表加在一起,以便将应该表示提现的负数加在一起,并将正数加在一起以表示存款。 So far I got 到目前为止,我知道了

def statement(l):
    deposit = 0
    withdrawl = 0
    for a in l:
        a = sum(l)
    for b in l:
        if b == -b:
            b = sum(b)        
    return [a,-b]

but when I do statement([30.95,-15.67,45.56,-55.00,43.78]) it returns [49.620000000000005, -43.78] when it is supposed to return [120.29,-70.67] can someone help? 但是当我执行statement([30.95,-15.67,45.56,-55.00,43.78])它应返回[49.620000000000005, -43.78] ,应该返回[120.29,-70.67] ,有人可以帮忙吗?

Thanks! 谢谢!

The following seems to do it: 以下似乎可以做到这一点:

In [1]: def statement(l):
   ...:     pos, neg = 0, 0
   ...:     for a in l:
   ...:         if a > 0: pos += a
   ...:         else: neg += a
   ...:     return pos, neg
   ...: 

In [2]: statement([30.95,-15.67,45.56,-55.00,43.78])
Out[2]: (120.29, -70.67)

It returns a tuple rather than a list , which seems more logical as the length is fixed. 它返回一个tuple而不是一个list ,因为长度是固定的,这似乎更合乎逻辑。


Here's a couple of comments on your attempt: 以下是有关您的尝试的一些评论:

for a in l:
    a = sum(l)

This will lead to the sum of all elements of l being calculated len(l) times, which doesn't make much sense. 这将导致将l的所有元素的总和计算len(l)次,这没有多大意义。 To get a sum, just do a = sum(l) once. 要获得总和,只需执行a = sum(l)

if b == -b: - you probably expect this to check if a number is negative, but in fact it checks if b equals zero, because zero is the only x so that x == -x . if b == -b: -您可能希望它检查一个数字是否为负,但实际上它会检查b是否等于零,因为零是唯一的x所以x == -x You want if b < 0: . if b < 0: :, if b < 0:需要。


I checked which answer is faster on CPython 3.3, and unsurprisingly this one is about 2 times faster on the example given: 2.3 us per loop vs 5.98 us per loop . 我检查了在CPython 3.3上哪个答案更快,并且毫不奇怪,在给定的示例中,这个答案快了大约2倍: 2.3 us per loop 5.98 us per loop

This should do what you want: 这应该做您想要的:

def statement(l):
    pos = sum(i for i in l if i > 0)
    neg = sum(i for i in l if i < 0)
    return pos, neg

Your mistake is that you try to assign to iteration variables. 您的错误是您尝试分配给迭代变量。 If you want to accumulate a value, define it as 0 first outside of the for loop then add on to that with each step. 如果要累加一个值,请在for循环外首先将其定义为0 for然后在每个步骤中将其添加到该值。

At the end of the loop, a contains the sum of all the elements in l , and b contains the last element in l , because the final check always fails and b is never overwritten (which explains the result you got). 在循环结束时, a包含l中所有元素的总和, b包含l最后一个元素,因为最终检查总是失败,并且b不会被覆盖(这说明了得到的结果)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM