列表推导 += 运算符
List comprehension += operator
如何转换这个 for 循环:
smoke_ray = [18, 14]
total = 0
for i in smoke_ray:
total += i
进入列表理解?我试过了:
smoke_ray = [18, 14]
total = 0
[total += i for i in smoke_ray]
问题出在 +=
操作员身上吗?
那是我出错的地方
已更新完整代码:
days = [
{ "day_name": "wed",
"smoked_at": {
'15:30': 1,
'16:30': 1,
'16:50': 2,
'17:30': 1,
'18:30': 1,
'20:20': 1,
'21:30': 1,
'22:30': 1,
'25:00': 5
}
},
{ "day_name": "thurs",
"smoked_at": {
'08:15': 1,
'08:40': 1,
'09:20': 1,
'10:00': 1,
'11:20': 1,
'11:38': 1,
'12:10': 1,
'13:00': 1,
'14:26': 1,
'15:40': 1,
'17:08': 1,
'18:10': 1,
'19:30': 1,
'20:20': 1,
'22:00': 1,
'23:00': 1,
'25:00': 2
}
}
]
smoke_ray = []
for i in days:
print(i["day_name"])
smokes = i["smoked_at"].values()
smokes_day = sum(smokes)
print(smokes_day)
smoke_ray.append(i)
total = 0
for i in smoke_ray:
total += i
print(total)
当试图将最后一个 for 循环转换为列表理解时(你是在告诉我这不是 shorthand 编写循环的方式吗?我听说它更快)
我收到这个错误:
File "compiler.py", line 47
[total += i for i in smoke_ray]
^
SyntaxError: invalid syntax
当尝试使用 sum 时,它就是行不通:
sum(smoke_ray)
wed
14
thurs
18
Traceback (most recent call last):
File "compiler.py", line 47, in
sum(smoke_ray)
TypeError: unsupported operand type(s) for +: 'int' and 'dict'
您甚至不需要列表理解。只需使用 sum
:
total = sum(smoke_ray)
如果您想明智地查看总和值列表,则可以使用
import itertools
smoke_ray = [18, 14]
print(list(itertools.accumulate(smoke_ray)))
这将按元素显示序列的总和
输出
[18, 32]
列表推导具体是通过两个动作将输入列表转换为输出列表的操作:
- 每个元素的变换(也称为“映射”)
- 过滤 基于一些过滤条件
就是这样。
您想要的操作根本不同:您想要使用给定操作累加 列表中的元素。此操作也称为“减少”,可通过 functools.reduce
.
在 Python 库中使用
所以你可以这样写
import functools
import operator
functools.reduce(operator.add, smoke_ray)
... 但这是一个如此常见的操作,因此有一个 shorthand :如前所述,您也可以只使用 sum(smoke_ray)
.
你可以通过多种方式做到这一点,因为你在这里要求 list comprehension
(虽然列表理解在这里是一个糟糕的选择,电池选项是 sum(your_list)
)试试这个:
sum([i for i in [18, 14]])
24
查看您现在为上下文添加的代码,我认为您遇到的问题实际上不是对列表求和,而是构建列表本身。当我 运行 你的原始代码时,使用 for 循环,对我来说也失败了。
我认为问题出在 smoke_ray.append(i)
行:在这里,您将字典的整个元素(例如:{ "day_name": "...", "smoked_at": { ... } }
)附加到 smoke_ray
列表。然后,对该列表中的值求和是没有意义的,因为它们是字典。如果您想将每个 smokes_day
添加到列表中,然后对它们求和,您可以在该循环中执行 smoke_ray.append(smoke_day)
。然后,您应该可以像其他答案中提到的那样使用 sum
来对列表求和。
编辑:这并不是说不能对代码做更多的改进,顺便说一下,一个简单的更改可以保留原始结构,就是将 for 循环更改为这样的东西:
total = 0
for i in days:
print(i['day_name'])
smokes = i['smoked_at'].values()
smokes_day = sum(smokes)
print(smokes_day)
total += smokes_day
print(total)
这样您就可以在一个循环中对值求和,而无需构建另一个 list/use 列表理解。
如何转换这个 for 循环:
smoke_ray = [18, 14]
total = 0
for i in smoke_ray:
total += i
进入列表理解?我试过了:
smoke_ray = [18, 14]
total = 0
[total += i for i in smoke_ray]
问题出在 +=
操作员身上吗?
那是我出错的地方
已更新完整代码:
days = [
{ "day_name": "wed",
"smoked_at": {
'15:30': 1,
'16:30': 1,
'16:50': 2,
'17:30': 1,
'18:30': 1,
'20:20': 1,
'21:30': 1,
'22:30': 1,
'25:00': 5
}
},
{ "day_name": "thurs",
"smoked_at": {
'08:15': 1,
'08:40': 1,
'09:20': 1,
'10:00': 1,
'11:20': 1,
'11:38': 1,
'12:10': 1,
'13:00': 1,
'14:26': 1,
'15:40': 1,
'17:08': 1,
'18:10': 1,
'19:30': 1,
'20:20': 1,
'22:00': 1,
'23:00': 1,
'25:00': 2
}
}
]
smoke_ray = []
for i in days:
print(i["day_name"])
smokes = i["smoked_at"].values()
smokes_day = sum(smokes)
print(smokes_day)
smoke_ray.append(i)
total = 0
for i in smoke_ray:
total += i
print(total)
当试图将最后一个 for 循环转换为列表理解时(你是在告诉我这不是 shorthand 编写循环的方式吗?我听说它更快)
我收到这个错误:
File "compiler.py", line 47
[total += i for i in smoke_ray]
^
SyntaxError: invalid syntax
当尝试使用 sum 时,它就是行不通:
sum(smoke_ray)
wed
14
thurs
18
Traceback (most recent call last):
File "compiler.py", line 47, in
sum(smoke_ray)
TypeError: unsupported operand type(s) for +: 'int' and 'dict'
您甚至不需要列表理解。只需使用 sum
:
total = sum(smoke_ray)
如果您想明智地查看总和值列表,则可以使用
import itertools
smoke_ray = [18, 14]
print(list(itertools.accumulate(smoke_ray)))
这将按元素显示序列的总和
输出
[18, 32]
列表推导具体是通过两个动作将输入列表转换为输出列表的操作:
- 每个元素的变换(也称为“映射”)
- 过滤 基于一些过滤条件
就是这样。
您想要的操作根本不同:您想要使用给定操作累加 列表中的元素。此操作也称为“减少”,可通过 functools.reduce
.
所以你可以这样写
import functools
import operator
functools.reduce(operator.add, smoke_ray)
... 但这是一个如此常见的操作,因此有一个 shorthand :如前所述,您也可以只使用 sum(smoke_ray)
.
你可以通过多种方式做到这一点,因为你在这里要求 list comprehension
(虽然列表理解在这里是一个糟糕的选择,电池选项是 sum(your_list)
)试试这个:
sum([i for i in [18, 14]])
24
查看您现在为上下文添加的代码,我认为您遇到的问题实际上不是对列表求和,而是构建列表本身。当我 运行 你的原始代码时,使用 for 循环,对我来说也失败了。
我认为问题出在 smoke_ray.append(i)
行:在这里,您将字典的整个元素(例如:{ "day_name": "...", "smoked_at": { ... } }
)附加到 smoke_ray
列表。然后,对该列表中的值求和是没有意义的,因为它们是字典。如果您想将每个 smokes_day
添加到列表中,然后对它们求和,您可以在该循环中执行 smoke_ray.append(smoke_day)
。然后,您应该可以像其他答案中提到的那样使用 sum
来对列表求和。
编辑:这并不是说不能对代码做更多的改进,顺便说一下,一个简单的更改可以保留原始结构,就是将 for 循环更改为这样的东西:
total = 0
for i in days:
print(i['day_name'])
smokes = i['smoked_at'].values()
smokes_day = sum(smokes)
print(smokes_day)
total += smokes_day
print(total)
这样您就可以在一个循环中对值求和,而无需构建另一个 list/use 列表理解。