Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the index of the largest list inside a list of lists using Python?

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

like image 210
George Profenza Avatar asked Jun 30 '10 13:06

George Profenza


1 Answers

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

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

like image 190
anton.burger Avatar answered Sep 28 '22 13:09

anton.burger