【问题标题】:Maya Python: How to convert a list to an IntegerMaya Python:如何将列表转换为整数
【发布时间】:2023-03-10 14:39:01
【问题描述】:

我还在学习python,所以请多多包涵。 我得到了关键帧 1000 到 2000 之间动画的最后一个关键帧。

shotLength = cmds.keyframe(time=(1000,2000) ,query=True)
del shotLength[:-1]
print shotLength 

结果:

[1090.0]

此时只有所需的关键帧作为值保留在列表中。 我将此值转换为整数,如下所示:

shotLengthInt = list(map(int, shotLength))
print shotLengthInt

结果:

[1090]

现在我想给这个值加上 +1,所以它看起来像这样:

[1091]

我就是想不通。

【问题讨论】:

  • shotLengthInt[0] += 1
  • 您确定要将您的int 列入列表吗?如果没有,你可以简单地做,在开头:lastFrame = int(shotLength[-1]) + 1;否则你可以使用shotLengthInt[0] += 1(如@AK47 建议的那样),只是感觉过于复杂......
  • @mapofemergence 谢谢!这实际上是完美的。

标签: python python-2.7 math python-2.x maya


【解决方案1】:

您可以编辑以下内容:

shotLengthInt = list(map(int, shotLength))
print shotLengthInt

我们可以传递一个lambda函数给map,来实现它:

shotLengthInt = map(lambda x: int(x) + 1, shotLength)
print shotLengthInt

【讨论】:

  • 你可以使用shotLengthInt = map(lambda x: int(x) + 1, shotLength)
  • 是的,您的权利,不必再次转换为列表,谢谢,我会编辑答案。
【解决方案2】:

您的值包含在一个列表中(注意方括号),因此要将该值更新 1,您需要引用列表的第一个索引并将其增加 1

>>> shotLengthInt = [1090]
>>> shotLengthInt
> [1090]
>>> shotLengthInt[0] += 1
>>> shotLengthInt
> [1091]

您还可以在将值分配给shotLengthInt 时删除list()

>>> shotLength = [1090.0]
>>> shotLength
> [1090.0]
>>> shotLengthInt = map(int, shotLength)
>>> shotLengthInt
> [1090]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 2021-02-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多