计算嵌套列表中的最大差异

2024-10-02 18:25:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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)]]

有人有主意吗?谢谢你的帮助。你知道吗


Tags: 列表数字差异文本文档感兴趣主意sentiment极性
2条回答

您可以使用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

给出了一个示例类,该类描述了如何访问案例中所需的元素:

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

相关问题 更多 >