Many-to-one mapping (creating equivalence classes)

后端 未结 4 786
花落未央
花落未央 2020-12-31 07:08

I have a project of converting one database to another. One of the original database columns defines the row\'s category. This column should be mapped to a new category in t

4条回答
  •  梦谈多话
    2020-12-31 08:11

    It seems to me that you have two concerns. First, how do you express your mapping originally, that is, how do you type the mapping into your new_mapping.py file. Second, how does the mapping work during the re-mapping process. There's no reason for these two representations to be the same.

    Start with the mapping you like:

    monty = { 
        ('parrot','spam','cheese_shop'): 'sketch', 
        ('Cleese', 'Gilliam', 'Palin') : 'actors',
    }
    

    then convert it into the mapping you need:

    working_monty = {}
    for k, v in monty.items():
        for key in k:
            working_monty[key] = v
    

    producing:

    {'Gilliam': 'actors', 'Cleese': 'actors', 'parrot': 'sketch', 'spam': 'sketch', 'Palin': 'actors', 'cheese_shop': 'sketch'}
    

    then use working_monty to do the work.

提交回复
热议问题