多对一映射(创建等价类)
我有一个将一个数据库转换为另一个数据库的项目。原始数据库列之一定义了行的类别。该列应映射到新数据库中的新类别。
例如,假设原始类别是:parrot、spam、cheese_shop、Cleese、Gilliam、Palin
现在这对我来说有点冗长,我希望将这些行分类为 sketch, actor
- 也就是说,将所有草图和所有演员定义为两个等价类。
>>> monty={'parrot':'sketch', 'spam':'sketch', 'cheese_shop':'sketch',
'Cleese':'actor', 'Gilliam':'actor', 'Palin':'actor'}
>>> monty
{'Gilliam': 'actor', 'Cleese': 'actor', 'parrot': 'sketch', 'spam': 'sketch',
'Palin': 'actor', 'cheese_shop': 'sketch'}
这很尴尬——我更喜欢这样的东西:
monty={ ('parrot','spam','cheese_shop'): 'sketch',
('Cleese', 'Gilliam', 'Palin') : 'actors'}
但是,当然,这将整个元组设置为键:
>>> monty['parrot']
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
monty['parrot']
KeyError: 'parrot'
有什么想法如何在Python中创建优雅的多对一字典吗?
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 the new database.
For example, let's assume the original categories are:parrot, spam, cheese_shop, Cleese, Gilliam, Palin
Now that's a little verbose for me, And I want to have these rows categorized as sketch, actor
- That is, define all the sketches and all the actors as two equivalence classes.
>>> monty={'parrot':'sketch', 'spam':'sketch', 'cheese_shop':'sketch',
'Cleese':'actor', 'Gilliam':'actor', 'Palin':'actor'}
>>> monty
{'Gilliam': 'actor', 'Cleese': 'actor', 'parrot': 'sketch', 'spam': 'sketch',
'Palin': 'actor', 'cheese_shop': 'sketch'}
That's quite awkward- I would prefer having something like:
monty={ ('parrot','spam','cheese_shop'): 'sketch',
('Cleese', 'Gilliam', 'Palin') : 'actors'}
But this, of course, sets the entire tuple as a key:
>>> monty['parrot']
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
monty['parrot']
KeyError: 'parrot'
Any ideas how to create an elegant many-to-one dictionary in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我看来,你有两个担忧。首先,你最初如何表达你的映射,即如何将映射键入到你的 new_mapping.py 文件中。其次,重映射过程中映射是如何工作的。这两种表示没有理由相同。
从您喜欢的映射开始:
然后将其转换为您需要的映射:
生成:
然后使用
working_monty
完成工作。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:
then convert it into the mapping you need:
producing:
then use
working_monty
to do the work.您可以覆盖 dict 的索引器,但也许以下更简单的解决方案会更好:(
也许嵌套的 for 循环可以压缩为令人印象深刻的一行,但这有效并且可读。)
You could override dict's indexer, but perhaps the following simpler solution would be better:
(Perhaps the nested for loop can be compressed an impressive one-liner, but this works and is readable.)
但我告诉你,它会比普通的一对一字典慢。
But let me tell you, It will be slow than normal one to one dictionary.
如果你想让多个键指向同一个值,即
m_dictionary{('k1', 'k2', 'k3', 'k4'):1, ('k5', 'k6'):2 } 并访问它们,
检查这个多字典 python 模块
multi_key_dict
。安装并导入它。https://pypi.python.org/pypi/multi_key_dict
If you want to have multiple keys pointing to the same value, i.e.
m_dictionary{('k1', 'k2', 'k3', 'k4'):1, ('k5', 'k6'):2}
and access them as,Check this multi dictionary python module
multi_key_dict
. Install and Import it.https://pypi.python.org/pypi/multi_key_dict