A quick study Python-based interview questions

This is a group of friends in the group made a while ago of a face questions using Python dictionary features, you can skillfully use streamline the code to achieve the perfect solution.

topic

Converting the data into a form that new_data, write the conversion process.

data = {
    'a_b_h':1,
    'a_b_i':2,
    'a_c_j':3,
    'a_d':4,
    'a_c_k':5,
    'a_e':6
}

new_data = {
    'a':{
        'b':{
            'h':1,
            'i':2
        },
        'c':{
            'j':3,
            'k':5
        },
        'd':4,
        'e':6
    }
}

As can be seen, the conversion is to process key split underline, and the underline character nested behind the character in the preceding value.

Open interest on the IDE, first try to explain them himself.

Problem-solving ideas

You should soon think the main idea is to underscore splitafter, followed by the inner layer is generated using the character dictionary, when it reaches the last character of the numbers as values.

So the key point is how to continue to get the inner dictionary to modify it? Actually this question is to examine whether or not you understand Python dictionary is passed by reference this feature.

What is passed by reference? We know that in Python dictionary and a list of objects are variable objects after they pass a variable to another variable, change the object element would make two variables will also change, for example:

new_data = {}
tmp = {}
new_data['a'] = tmp
print(new_data)  # {'a': {}}
tmp['b'] = 1
print(new_data)  # {'a': {'b': 1}}

As above, use of this feature, the inner layer dictionary assigned to an intermediate variable, and then change the intermediate variables, to sync the final modified new_data variable.

According to this idea, the initial code is as follows:

data = {
    'a_b_h':1,
    'a_b_i':2,
    'a_c_j':3,
    'a_d':4,
    'a_c_k':5,
    'a_e':6
}

new_data = {}

for key, value in data.items():
    keys = key.split('_')
    tmp = new_data
    last = len(keys) - 1  # 最后一个 key 的索引值 
    for i, k in enumerate(keys):
        if i == last:
            tmp[k] = value
            continue
        if k not in tmp:
            sub_tmp = {}
            tmp[k] = sub_tmp
            tmp = sub_tmp
        else:
            tmp = tmp[k]

This is the first edition of the answers given group of friends, write and there is not much problem, but the code more complicated, there is certainly room for optimization.

We can only use an intermediate variable can be further optimized:

for field, value in data.items(): 
    keys = field.split('_') 
    tmp = new_data 
    last = len(keys) - 1
    for i, k in enumerate(keys): 
        if k not in tmp:
            tmp[k] = {} if i < last else value
        tmp = tmp[k]  # 将内层 dict 传给 tmp

This code above appears to be very simple, but there are still two if judgment, if not using ternary expression, it will be more lines.

It can be further optimized:

for field, value in data.items(): 
    keys = field.split('_') 
    tmp = new_data 
    for k in keys[:-1]: 
        tmp = tmp.setdefault(k, {})
    tmp[keys[-1]] = value

We omit the last index of the last character is determined directly by keys[:-1]avoiding the last character, and then generates a digital key at the end of the individual.

There is also a built-in method of using a dictionary - setdefault.

dict.setdefault(key, default=None)Method and getthe like, but if the key does not exist in the dictionary, will not only return to the default parameter values, also generated automatically by a value of the key pair.

if k not in tmp:
    tmp[k] = {}
v = tmp[k]

# 等价于

v = tmp.setdefault(k, {})

Eventually we use six lines of code to solve this problem, which is close to the most simple code.

If the characteristics of a dictionary reference is qualified, then when you use a setdefaultpost this method, the interviewer has given you a good fight, so be sure to be familiar with all of these built-in data objects .

Published 24 original articles · won praise 88 · views 30000 +

Guess you like

Origin blog.csdn.net/z_kqiang/article/details/104221396