[英]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. 我有一个创建for循环的任务,以添加偶数并从数字列表中减去不均匀数字。 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))]
我不确定从哪里开始,但我已经走了这么远:
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! 不要将
list
用作变量名,因为它会隐藏list
数据类型!
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: 您可以将条件表达式与for循环一起使用,以在运行的合计/ 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. if not ele % 2
对于偶数将为True,因为0
是一个假值。
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: 您还可以检查最低有效位(
if ele & 1
(如果为True表示数字是奇数),或者if ele & 0
以查找偶数:
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. sum
是一个内置函数,与list
因此请尽量避免将其中任一个用作变量名。
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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.