【发布时间】:2018-08-29 16:11:55
【问题描述】:
我继承了一段代码,我需要在原始位置以外的其他地方运行该代码,并进行了一些细微的更改。我正在尝试 map 一个字符串列表,该列表使用 python 3.6(我不熟悉的一种语言)将函数应用于该列表的每个元素。
我想使用map 而不是列表理解,但现在我怀疑这是可能的。
在以下示例中,我尝试了for 循环、yield(或不)和next(...) 的组合,但我无法使代码按预期工作。
我想看打印:
AAA! xxx
Found: foo
Found: bar
每次计数器 xxx 模 360 为 0(零)。
我知道map 函数不执行代码,所以我需要做一些事情来将该函数“应用”到输入列表的每个元素。
但是我无法使这件事发挥作用。该文档https://docs.python.org/3.6/library/functions.html#map 和https://docs.python.org/3.6/howto/functional.html#iterators 并没有太大帮助,我通过了它,我认为至少下面的注释位之一(# <python code>)应该有效。我不是一个经验丰富的 python 开发人员,我认为我错过了一些关于 python 3.6 的迭代器/生成器的语法/约定的问题。
issue_counter = 0
def foo_func(serious_stuff):
# this is actually a call to a module to send an email with the "serious_stuff"
print("Found: {}".format(serious_stuff))
def report_issue():
global issue_counter
# this actually executes once per minute (removed the logic to run this fast)
while True:
issue_counter += 1
# every 6 hours (i.e. 360 minutes) I would like to send emails
if issue_counter % 360 == 0:
print("AAA! {}".format(issue_counter))
# for stuff in map(foo_func, ["foo", "bar"]):
# yield stuff
# stuff()
# print(stuff)
iterable_stuff = map(foo_func, ["foo", "bar"])
for stuff in next(iterable_stuff):
# yield stuff
print(stuff)
report_issue()
在运行脚本时,for 循环出现许多不同的错误/意外行为:
- 当我打电话给
print(...)时不打印任何东西 TypeError: 'NoneType' object is not callableAttributeError: 'map' object has no attribute 'next'TypeError: 'NoneType' object is not iterable- 打印我期望的由
None交错的内容,例如:
AAA! 3047040 Found: foo None Found: bar None
【问题讨论】:
标签: functional-programming generator python-3.6 iterable