【问题标题】:Calculate the greatest difference in a nested list计算嵌套列表中的最大差异
【发布时间】:2019-08-22 07:17:33
【问题描述】:

我使用 WordBlob 创建了一个包含来自文本文档的列表的列表。现在我想在每个列表中创建一个差异最大的列表,我只对极性感兴趣。我想将最高和最低的数字附加到另一个列表中,然后将它们相互减去。但是我怎么能引用“极性”中的数字呢?这是我的嵌套列表:

[[Sentiment(polarity=0.35, subjectivity=0.65),
  Sentiment(polarity=0.0, subjectivity=0.0),
  Sentiment(polarity=0.0, subjectivity=0.0),
  Sentiment(polarity=0.6, subjectivity=0.87),
  Sentiment(polarity=0.0, subjectivity=0.0),
  Sentiment(polarity=0.0, subjectivity=0.0)],
 [Sentiment(polarity=0.0, subjectivity=0.0),
  Sentiment(polarity=0.5, subjectivity=0.8),
  Sentiment(polarity=0.0, subjectivity=0.0),
  Sentiment(polarity=-0.29, subjectivity=0.54),
  Sentiment(polarity=0.0, subjectivity=0.0),
  Sentiment(polarity=0.25, subjectivity=1.0)],
  [Sentiment(polarity=0.5, subjectivity=0.8),
  Sentiment(polarity=0.0, subjectivity=0.0)]]

有人有想法吗?感谢您的帮助。

【问题讨论】:

    标签: python nested-lists textblob


    【解决方案1】:

    您可以使用 python 内置函数 minmax 连同它们的 key 参数在给定关键标准的情况下查找列表中的最小/最大值。写成一个函数,它可能看起来像这样:

    def polarity_diffs(sentiments):
        diffs = []
        for row in sentiments:
            smallest = min(row, key=lambda s: s.polarity).polarity
            biggest = max(row, key=lambda s: s.polarity).polarity
            diffs.append(biggest - smallest)
        return diffs
    

    给定一个虚拟对象和一些测试数据 -

    class Sentiment:  # Example class
        def __init__(self, polarity, subjectivity):
            self.polarity = polarity
            self.subjectivity = subjectivity
    
    test_data = [
        # normal values
        [Sentiment(polarity=0.35, subjectivity=0.65),
         Sentiment(polarity=0.0, subjectivity=0.0),
         Sentiment(polarity=0.0, subjectivity=0.0),
         Sentiment(polarity=0.6, subjectivity=0.87),
         Sentiment(polarity=0.0, subjectivity=0.0),
         Sentiment(polarity=0.0, subjectivity=0.0)],
        # more normal values
        [Sentiment(polarity=0.0, subjectivity=0.0),
         Sentiment(polarity=0.5, subjectivity=0.8),
         Sentiment(polarity=0.0, subjectivity=0.0),
         Sentiment(polarity=-0.29, subjectivity=0.54),
         Sentiment(polarity=0.0, subjectivity=0.0),
         Sentiment(polarity=0.25, subjectivity=1.0)],
        # only a single entry
        [Sentiment(polarity=0.35, subjectivity=0.65)],
        # multiple entries, but identical
        [Sentiment(polarity=0.0, subjectivity=0.0),
         Sentiment(polarity=0.0, subjectivity=0.0)]
    ]
    

    - 这些是结果:

    for diff in polarity_diffs(x):
        print(diff)
    0.6   # normal values
    0.79  # more normal values
    0.0   # only a single entry
    0.0   # multiple entries, but identical
    

    【讨论】:

    • 这是我的问题的解决方案 - 谢谢!但如果只有一个数字,它也会附加一个数字。我想要数字之间的差异。你有什么想法吗?
    • 你的意思是要跳过只有一个元素的子列表,还是如果差是0就跳过?
    • 我想要一个列表,每个子列表都有一个条目。如果子列表中只有一个数字,则应在最终列表中添加零。如果差为零,则在最终列表中也为零。
    • 在这种情况下,如果您在主要问题中提供测试数据和预期输出会有所帮助。从文字中理解这些极端情况可能会很棘手。
    • 嗯,如果我用单项列表尝试它已经这样了。
    【解决方案2】:

    给定一个示例类,它是您在案例中访问所需元素的方式:

    class Sentiment:  # Example class
        def __init__(self, polarity, subjectivity):
            self.polarity = polarity
            self.subjectivity = subjectivity
    
    
    ar = [[Sentiment(polarity=0.35, subjectivity=0.65),
          Sentiment(polarity=0.0, subjectivity=0.0),
          Sentiment(polarity=0.0, subjectivity=0.0),
          Sentiment(polarity=0.6, subjectivity=0.87),
          Sentiment(polarity=0.0, subjectivity=0.0),
          Sentiment(polarity=0.0, subjectivity=0.0)],
         [Sentiment(polarity=0.0, subjectivity=0.0),
          Sentiment(polarity=0.5, subjectivity=0.8),
          Sentiment(polarity=0.0, subjectivity=0.0),
          Sentiment(polarity=-0.29, subjectivity=0.54),
          Sentiment(polarity=0.0, subjectivity=0.0),
          Sentiment(polarity=0.25, subjectivity=1.0)]]
    
    print(ar[0][0].polarity)  # this is the first polarity value
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多