简体   繁体   中英

Add even numbers, subtract uneven numbers in Python

I got a task of creating a for-loop to add even numbers and subtracting uneven number from a list of numbers. Something like this: list = [(6,(+8),(-95),(+2),(+12),(+152),(+4),(+78),(-621),(-45))] I'm not really sure on where to start but I got this far:

list = [6,8,95,2,12,152,4,78,621,45]
sum = 0

for x in list:
    if list (0 % 2) == 0:
        sum = sum + list[x]
    elif list (0 % 2) != 0:
        sum = sum - list[x]
    return sum

Not sure on how to iterate through the list though...

Don't use list as a variable name, because it hides the list data type!

lst = [6, 8, 95, 2, 12, 152, 4, 78, 621, 45]

total = 0
for x in lst:
    if x % 2:    # odd
        total -= x
    else:        # even
        total += x

Edit: just for fun, you could also try

total = sum(x * (1 - (x % 2 * 2)) for x in lst)

Bonus points for figuring out how it works ;-)

You can use a conditional expression with a for loop adding even numbers and subtracting odd numbers to/from the running total/sm:

lst = [6,8,95,2,12,152,4,78,621,45]
sm = 0
for ele in lst:
    sm = sm + ele if not ele % 2 else sm - ele

if not ele % 2 will be True for even numbers as 0 is a falsey value.

You can also check the least significant bit, if ele & 1 which if True means the number is odd or if ele & 0 to find even numbers:

sm = 0
for ele in lst:
    sm = sm - ele if ele & 1 else sm + ele

print(sm)

Which can be all be put in a generator expression :

print(sum(-ele if ele & 1 else ele for ele in lst))

sum is a builtin function as is list so try to avoid using either as a variable name.

Just for interest some timings:

In [8]: timeit sum(-x if x % 2 else x for x in lst)
1000000 loops, best of 3: 1.44 µs per loop

In [9]: %%timeit                                   
sm = 0
for ele in lst:
    sm = sm + ele if not ele % 2 else sm - ele
   ...: 
1000000 loops, best of 3: 1.12 µs per loop

In [11]: timeit sum(-ele if ele & 1 else ele for ele in lst)
1000000 loops, best of 3: 1.27 µs per loop

In [13]: %%timeit                                           
sm = 0
for ele in lst:
    sm = sm + ele if not ele % 2 else sm - ele
   ....: 
1000000 loops, best of 3: 1.11 µs per loop

In [14]: %%timeit                                           
sm = 0
for ele in lst:
    sm = sm - ele if ele & 1 else sm + ele
   ....: 
1000000 loops, best of 3: 875 ns per loop

In [15]: %%timeit
   ....: total = 0
   ....: for x in lst:
   ....:     if x % 2:    # odd
   ....:         total -= x
   ....:     else:        # even
   ....:         total += x 
1000000 loops, best of 3: 1.02 µs per loop

In [16]: timeit sum(x * (1 - (x % 2 * 2)) for x in lst)
100000 loops, best of 3: 2.2 µs per loop

Use += and -= to add/subtract to the sum.

num_list = [6,8,95,2,12,152,4,78,621,45]
sum = 0

for x in num_list:
    if x % 2 == 0:
        sum += x
    else:
        sum -= x

您实际上可以在一行中完成此操作,作为列表理解:

print sum([j * [1, -1][j % 2] for j in data])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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