将对象列表转换为整数列表和查找表
为了说明我的意思,下面是一个示例,
messages = [
('Ricky', 'Steve', 'SMS'),
('Steve', 'Karl', 'SMS'),
('Karl', 'Nora', 'Email')
]
我想将此列表和组的定义转换为整数列表和查找字典,以便组中的每个元素都获得唯一的 id。该 id 应该像这样映射到查找表中的元素
messages_int, lookup_table = create_lookup_list(
messages, ('person', 'person', 'medium'))
print messages_int
[ (0, 1, 0),
(1, 2, 0),
(2, 3, 1) ]
print lookup_table
{ 'person': ['Ricky', 'Steve', 'Karl', 'Nora'],
'medium': ['SMS', 'Email']
}
我想知道是否有一个优雅且Pythonic的解决方案来解决这个问题。
我也愿意接受比 create_lookup_list
等更好的术语
To illustrate what I mean by this, here is an example
messages = [
('Ricky', 'Steve', 'SMS'),
('Steve', 'Karl', 'SMS'),
('Karl', 'Nora', 'Email')
]
I want to convert this list and a definition of groups to a list of integers and a lookup dictionary so that each element in the group gets a unique id. That id should map to the element in the lookup table like this
messages_int, lookup_table = create_lookup_list(
messages, ('person', 'person', 'medium'))
print messages_int
[ (0, 1, 0),
(1, 2, 0),
(2, 3, 1) ]
print lookup_table
{ 'person': ['Ricky', 'Steve', 'Karl', 'Nora'],
'medium': ['SMS', 'Email']
}
I wonder if there is an elegant and pythonic solution to this problem.
I am also open to better terminology than create_lookup_list
etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
defaultdict
与itertools.count().next
方法结合使用是为唯一项分配标识符的好方法。以下是如何在您的案例中应用此示例:编辑:请注意
count().next
变为count().__next__
或lambda: next(count Python 3 中的 ())
。defaultdict
combined with theitertools.count().next
method is a good way to assign identifiers to unique items. Here's an example of how to apply this in your case:Edit: note that
count().next
becomescount().__next__
orlambda: next(count())
in Python 3.我的长度和复杂性大致相同:
Mine's about the same length and complexity:
在奥托的回答(或任何其他人的 string->id 字典)中,我将替换(如果您对速度着迷):
by
这更好,因为直接分配给逆数组中的每个项目比排序更快。
In Otto's answer (or anyone else's with string->id dicts), I'd replace (if obsessing over speed is your thing):
by
This is better because direct assignment to each item in the inverse array directly is faster than sorting.
这是我自己的解决方案 - 我怀疑这是最好的
Here is my own solution - I doubt it's the best
这个比较简单,也比较直接。
如果查找是正确的字典,而不是列表,则可以进一步简化。
使你的“查找表”具有以下结构
并且可以进一步降低复杂性。
您可以将查找的工作副本转换为逆副本,如下所示:
This is a bit simpler, and more direct.
If the lookups were proper dictionaries, not lists, this could be simplified further.
Make your "lookup table" have the following structure
And it can be further reduced in complexity.
You can turn this working copy of the lookups into it's inverse as follows:
这是我的解决方案,它并没有更好 - 只是不同:)
Here is my solution, it's not better - it's just different :)
这是我的,内部函数让我将索引元组编写为生成器。
Here is mine, the inner function lets me write the index-tuple as a generator.