【问题标题】:How do I add values to a set in a comprehension?如何将值添加到理解中的集合?
【发布时间】: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


    【解决方案1】:

    这似乎有效:

    str_list = [
        ['its', 'all', 'ball', 'bearings', 'these', 'days'], 
        ['its', 'all', 'in', 'a', 'days', 'work']
    ]
    str_set = set(word for sublist in str_list for word in sublist)
    
    str_dict = {word: set(lindex
            for lindex, sublist in enumerate(str_list) if word in sublist)
        for word in str_set}
    
    print (str_dict)
    

    【讨论】:

      【解决方案2】:
      >>> alist = [['its', 'all', 'ball', 'bearings', 'these', 'days'], 
      ... ['its', 'all', 'in', 'a', 'days', 'work']]
      >>> aset = {'its', 'all', 'ball', 'bearings', 'these', 'days', 'in', 'a', 'work'}
      
      >>> {x: {alist.index(y) for y in alist if x in y} for x in aset}
      {'a': set([1]), 'all': set([0, 1]), 'ball': set([0]), 'these': set([0]), 'bearings': set([0]), 'work': set([1]), 'days': set([0, 1]), 'in': set([1]), 'its': set([0, 1])}
      

      您也可以使用enumerate 并使用列表作为值将使结果更清晰:

      >>> {x: [i for i, y in enumerate(alist) if x in y] for x in aset}
      {'a': [1], 'all': [0, 1], 'ball': [0], 'these': [0], 'bearings': [0], 'work': [1], 'days': [0, 1], 'in': [1], 'its': [0, 1]}
      

      【讨论】:

        【解决方案3】:

        这是我的代码,与一些嵌套循环一起工作,试图让你觉得容易阅读和理解的东西!

        def accumulate(stringList,stringSet):
            outputDict = {}
            for setItem in stringSet:
                outputItem = set()
                for i,listItem in enumerate(stringList):
                    if setItem in listItem:
                        outputItem.add(i)
                outputDict[setItem] = outputItem
            return outputDict
        
        stringList = [['its', 'all', 'ball', 'bearings', 'these', 'days'], ['its', 'all', 'in', 'a', 'days', 'work']]
        stringSet = {'its', 'all', 'ball', 'bearings', 'these', 'days', 'in', 'a', 'work'}
        
        print(accumulate(stringList,stringSet))
        

        【讨论】:

          【解决方案4】:

          您可以使用嵌套循环:

          result = {}
          for w in stringSet:
              result[w] = []
              for i,l in enumerate(stringList):
                  if w in l:
                      result[w].append(i)
          

          它的作用是遍历stringSet 中的每个单词,并检查它是否在第一个列表、第二个列表等中,并相应地更新字典。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多