0

I have 2 lists that I want to convert them into a dict with key and values. I managed to do so but there are too many steps so I would like to know if there's a simpler way of achieving this. Basically I would like to create the dict directly in the loop without having the extra steps bellow. I just started working with python and I don't quite understand all the datatypes that it provides. The jName form can be modified if needed.

jName=["Nose", "Neck", "RShoulder", "RElbow", "RWrist", "LShoulder", "LElbow", "LWrist", "RHip",
        "RKnee","RAnkle","LHip", "LKnee", "LAnkle", "REye", "LEye", "REar", "LEar"]
def get_joints(subset, candidate):
    joints_per_skeleton = [[] for i in range(len(subset))]
    # for each detected skeleton
    for n in range(len(subset)):
        # for each joint
        for i in range(18):
            cidx = subset[n][i]
            if cidx != -1:
                y = candidate[cidx.astype(int), 0]
                x = candidate[cidx.astype(int), 1]
                joints_per_skeleton[n].append((y, x))
            else:
                joints_per_skeleton[n].append(None)
    return joints_per_skeleton

joints = get_joints(subset,candidate)
print joints

Here is the output of the joints list of list

[[None, (48.0, 52.0), (72.0, 50.0), None, None, (24.0, 55.0), (5.0, 105.0), None, (63.0, 159.0), (57.0, 221.0), (55.0, 281.0), (28.0, 154.0), (23.0, 219.0), (23.0, 285.0), None, (25.0, 17.0), (55.0, 18.0), (30.0, 21.0)]]

Here I defined a function to create the dictionary from the 2 lists

def create_dict(keys, values):
    return dict(zip(keys, values))

my_dict = create_dict(jointsName, joints[0])

Here is the result:

{'LAnkle': (23.0, 285.0),
 'LEar': (30.0, 21.0),
 'LElbow': (5.0, 105.0),
 'LEye': (25.0, 17.0),
 'LHip': (28.0, 154.0),
 'LKnee': (23.0, 219.0),
 'LShoulder': (24.0, 55.0),
 'LWrist': None,
 'Neck': (48.0, 52.0),
 'Nose': None,
 'RAnkle': (55.0, 281.0),
 'REar': (55.0, 18.0),
 'RElbow': None,
 'REye': None,
 'RHip': (63.0, 159.0),
 'RKnee': (57.0, 221.0),
 'RShoulder': (72.0, 50.0),
 'RWrist': None}
2
  • I'm not very clear on your question (what are subset and candidate?) but you might want to look at dictionary comprehensions. You could also make an empty dict, d = {} and then fill it as you loop Commented Jun 13, 2018 at 0:34
  • I just want to create the dictionary, as shown in the last part of the code in the for loop to ditch the extra processing step. Commented Jun 13, 2018 at 5:58

1 Answer 1

1

I think defaultdict could help you. I made my own example to show that you could predefine the keys and then go through a double for loop and have the values of the dict be lists of potentially different sizes. Please let me know if this answers your question:

from collections import defaultdict
import random
joint_names = ['hip','knee','wrist']
num_skeletons = 10

d = defaultdict(list)

for skeleton in range(num_skeletons):
    for joint_name in joint_names:
        r1 = random.randint(0,10)
        r2 = random.randint(0,10)
        if r1 > 4:
            d[joint_name].append(r1*r2)

print d

Output:

defaultdict(<type 'list'>, {'hip': [0, 5, 30, 36, 56], 'knee': [35, 50, 10], 'wrist': [27, 5, 15, 64, 30]})

As a note I found it very difficult to read through your code since there were some variables that were defined before the snippet you posted.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.