gpt4 book ai didi

python - py-me 抛出 Unpermitted modules 导入 itertools 异常

转载 作者:行者123 更新时间:2023-12-04 07:43:04 27 4
gpt4 key购买 nike

我正在尝试使用 py-me 压缩列表,但我得到了 未经许可的模块 异常 .
这是我的代码:

def compress(li):
from itertools import zip_longest
return([i for i,j in zip_longest(li,li[1:]) if i!=j])

x = [1,2,3,4,5,6,6,6,6,7,7,7,8,9]

print(compress(x))
Py-me 给出以下回应。

OUTPUT : Code was not executed due to Unpermitted Modules ExceptionEXCEPTION : Unpermitted Modules Exception : fromitertoolszip_longest <-^--- modules are not allowed tobe used in py-me. Check documentation for permitted modules !


提前致谢。

最佳答案

根据py-me documentation , 好像不能用 from 关键字或 . (dot operator) 在导入部分。
而不是 from itertools import zip_longest , 尝试使用 import itertools 在调用函数时,使用 itertools.zip_longest .
修改后的代码如下所示:

def compress(li):
import itertools
return([i for i,j in itertools.zip_longest(li,li[1:]) if i!=j])

x = [1,2,3,4,5,6,6,6,6,7,7,7,8,9]

print(compress(x))
如果您想检查 python 片段是否在 py-me API 中成功运行,请尝试执行 py-me web editor 中的片段首先并相应地修改代码。

关于python - py-me 抛出 Unpermitted modules 导入 itertools 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67354761/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com