繁体   English   中英

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

[英]Calculate the greatest difference in a nested list

我创建了一个列表,其中包含来自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内置函数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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM