如何使用 Python 获取列表列表中最大列表的索引?

发布于 2024-09-07 11:22:38 字数 947 浏览 17 评论 0原文

我将 Cinema4D 中的动画关键帧(使用很棒的 py4D)存储到列表列表中:

props = [lx,ly,lz,sx,sy,sz,rx,ry,rz]

我打印出了任意动画中每个属性/轨道的关键帧,它们的长度不同:

track Position . X has 24 keys
track Position . Y has 24 keys
track Position . Z has 24 keys
track Scale . X has 1 keys
track Scale . Y has 1 keys
track Scale . Z has 1 keys
track Rotation . H has 23 keys
track Rotation . P has 24 keys
track Rotation . B has 24 keys

现在,如果我想在搅拌机我需要做类似的事情:

  1. 转到当前帧
  2. 设置该关键帧的属性(可以是位置、旋转、缩放)并插入关键帧

到目前为止我的计划是:

  1. 从 0 循环到关键帧的最大数量对于所有属性
  2. 循环遍历每个属性
  3. 检查它是否为当前键存储了值,如果是,则转到 Blender 中的帧并存储值/插入关键帧

这是执行此操作的最佳方法吗?

这是问题的背景。

首先我需要找到道具存储的最大列表。我是 python 新手,想知道是否有一个神奇的函数可以为你做到这一点。与 max() 类似,但用于列表长度。

目前我正在考虑这样编码:

# after props are set
lens = []
for p in props: lens.append(len(p))
maxLen = max(lens)

实现这一目标的最佳方法是什么?

谢谢

I am storing animation key frames from Cinema4D(using the awesome py4D) into a lists of lists:

props = [lx,ly,lz,sx,sy,sz,rx,ry,rz]

I printed out the keyframes for each property/track in an arbitrary animation and they are of different lengths:

track Position . X has 24 keys
track Position . Y has 24 keys
track Position . Z has 24 keys
track Scale . X has 1 keys
track Scale . Y has 1 keys
track Scale . Z has 1 keys
track Rotation . H has 23 keys
track Rotation . P has 24 keys
track Rotation . B has 24 keys

Now if I want to use those keys in Blender I need to do something like:

  1. go to the current frame
  2. set the properties for that key frame( can be location,rotation,scale) and insert a keyframe

So far my plan is to:

  1. Loop from 0 to the maximum number of key frames for all the properties
  2. Loop through each property
  3. Check if it has a value stored for the current key, if so, go to the frame in Blender and store the values/insert keyframe

Is this the best way to do this ?

This is the context for the question.

First I need to find the largest list that props stores. I'm new to python and was wondering if there was a magic function that does that for you. Similar to max(), but for list lengths.

At the moment I'm thinking of coding it like this:

# after props are set
lens = []
for p in props: lens.append(len(p))
maxLen = max(lens)

What would be the best way to get that ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苄①跕圉湢 2024-09-14 11:22:38
max(enumerate(props), key = lambda tup: len(tup[1]))

这将为您提供一个包含 props 中最长列表的 (index, list) 的元组。

max(enumerate(props), key = lambda tup: len(tup[1]))

This gives you a tuple containing (index, list) of the longest list in props.

岁月染过的梦 2024-09-14 11:22:38

您可以使用生成器表达式:

maxLen = max(len(p) for p in props)

You can use a generator expression:

maxLen = max(len(p) for p in props)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文