首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python lambda if/ and条件失败:'int‘和'function’冲突

python lambda if/ and条件失败:'int‘和'function’冲突
EN

Stack Overflow用户
提问于 2016-11-21 16:22:47
回答 2查看 183关注 0票数 1

我正在尝试使用内联的lambda函数来执行一些if/else条件,在这里我得到了"unsupported operand type(s) for +: 'int' and 'function'"错误。这里,我试图将set_duration设置为1.5,除非它的第一次也是最后一次数组迭代。你能看一下并提供任何提示吗?展望未来。

代码语言:javascript
复制
for idx, string in enumerate(lines):


    duration = 10
    clips = []

    clips.append(ImageClip(os.path.join(folder,"gradient.png"))
                 .set_duration(lambda idx: 3 if (idx == 0 and lines[len(lines) - 1]) else 1.5)
                 )

    clips.append(ImageClip(os.path.join(folder,"image-{0}.png"))
                 .resize(width=800)
                 .set_duration(lambda idx: 3 if (idx == 0 and lines[len(lines) - 1]) else 1.5)
                 .margin(right=60, opacity=0)
                 .set_pos(("right","center"))
                 )

    clips.append(ImageClip(os.path.join(folder, "big-append.png"))
                 .resize(width=900)
                 .margin(left=60,opacity=0)
                 .set_duration(lambda idx: 3 if (idx == 0 and lines[len(lines) - 1]) else 1.5)
                 .set_pos(("left", "center"))
                 )



// rest of the code...

FullTraceback

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\vhosts\phpsols\pymovie\FORMAT-4\three.py", line 90, in <module>
    .set_duration(lambda idx: 3 if (idx == 0 and lines[len(lines) - 1]) else 1.5)
  File "<decorator-gen-29>", line 2, in set_duration
  File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\decorators.py", line 29, in apply_to_mask
    newclip = f(clip, *a, **k)
  File "<decorator-gen-28>", line 2, in set_duration
  File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\decorators.py", line 41, in apply_to_audio
    newclip = f(clip, *a, **k)
  File "<decorator-gen-27>", line 2, in set_duration
  File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\decorators.py", line 89, in wrapper
    return f(*new_a, **new_kw)
  File "<decorator-gen-26>", line 2, in set_duration
  File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\decorators.py", line 14, in outplace
    f(newclip, *a, **k)
  File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\Clip.py", line 288, in set_duration
    self.end = None if (t is None) else (self.start + t)
TypeError: unsupported operand type(s) for +: 'int' and 'function'
Process terminated with an exit code of 1
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-21 16:27:20

你不需要朗姆达。直接设置时间就行了。该值已在每个循环迭代中重新计算。

代码语言:javascript
复制
             .set_duration(3.0 if idx == 0 and lines[-1] else 1.5)
票数 5
EN

Stack Overflow用户

发布于 2016-11-21 16:29:07

在这种情况下,你根本不需要一个羔羊。把"3如果(idx .)“在set_duration()调用中的表达式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40724747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档