python: merging dictionaries by identical value of key

This question already has an answer here:

  • How to merge two dictionaries in a single expression? 48 answers

  • What you asked for is simply enough done:

    import copy
    if 'career_business' in add_sal and 'career_business' in add_perc and 
          add_sal['career_business'] == add_perc['career_business']:
       add_all = copy.deepcopy( add_sal )
       add_all['percent'] = add_perc['percent']
    

    However, your data structure seems rather odd for the kind of data you seem to have. You don't say what problem you're trying to solve with it, but your choice of a dictionary with random looking things in it seems likely to be at the root of your problems. Maybe you wanted something more like a dictionary keyed by the career name, like:

    career_sal[ 'operations / logistics' ] = 75000.0

    and corresponding stuff for the other pieces.

    链接地址: http://www.djcxy.com/p/17566.html

    上一篇: Python CodeLab词典

    下一篇: python:通过相同的键值合并字典