【发布时间】:2013-07-26 02:53:32
【问题描述】:
假设我有一个字符串列表(stringList):
[['its', 'all', 'ball', 'bearings', 'these', 'days'],
['its', 'all', 'in', 'a', 'days', 'work']]
我还有一组字符串 (stringSet),它们是 stringList 中的唯一词:
{'its', 'all', 'ball', 'bearings', 'these', 'days', 'in', 'a', 'work'}
如果可能的话,使用理解,我怎样才能得到一个字典,将 stringSet 中的每个单词映射到包含该单词的 stringList 索引的字典?在上面的例子中,返回值是:
{'its': {0,1}, 'all':{0,1}, 'ball':{0}, 'bearings':{0}, 'these':{0}, 'days':{0,1}, 'in':{1}, 'a':{1}, 'work':{1}}
我的问题是如何将索引累积到字典中。我敢肯定,对于那些比我更远的人来说,它相对简单。提前谢谢...
【问题讨论】:
标签: python list-comprehension dictionary-comprehension set-comprehension