【问题标题】:How to count overlapping values from a nested list or tuple to print out =如何计算嵌套列表或元组中的重叠值以打印出 =
【发布时间】:2020-04-01 14:17:27
【问题描述】:
    alterneuTabelle = [list(value for value in file if value) for file in neueTabelleResult]

我从 SQL 获取数据并交给 alterneutabelle,因为我想删除 neueTabellerResult 中的空字符串。

alterneuTabelle 现在看起来像:

  alterneutabelle[["Area1","Sport", "Math", "Politics" ]["Area2","Sport","Fun","Party"]["Area3","Fun", "Sport", "Math","Politics"]]

注意:我在 SQL 数据库中有大约 40 个区域,将显示那些与我的输入有重叠值的区域。 还是可以的。

现在我想计算一个区域的选定输入命中的百分比并将其返回(可能通过将其附加到末尾)

预期:我的输入是:派对和运动。

输出:

[“Area1”、“运动”、“数学”、“政治”, 33% ]

["Area2","Sport","Fun","Party",67%]

[“Area3”、“乐趣”、“运动”、“数学”、“政治”、25%]

我想稍后与 % 合作,就去哪里提出建议。

我现在的问题是我不知道该怎么做。我尝试使用 if 子句然后对其进行计数,但没有成功。

   for i in alterneuTabelle:
        for x in i:
            if (x==pick1 or x==pick2 or x==pick3 or x==pick4 or x==pick5):
                b= b+1
                a.append(b)
                b=0
            elif (x==pick1 or x==pick2 or x==pick3 or x==pick4 and x!=pick5):
                b= b+1
                a.append(b)
                b=0
            elif (x==pick1 or x==pick2 or x==pick3 and x!=pick4 and x!=pick5):
                b= b+1
                a.append(b)
                b=0
            elif (x==pick1 or x==pick2 and x!=pick3 and x!=pick4 and x!=pick5):
                b= b+1
                a.append(b)
                b=0
            elif (x==pick1 and x!=pick2 and x!=pick3 and x!=pick4 and x!=pick5):
                b= b+1
                a.append(b)
                b=0

我是否应该使用来自 neueTabelleResult 的元组,并在删除空字符串之前添加百分比?

任何帮助表示赞赏:)

【问题讨论】:

    标签: python python-3.x list nested-lists


    【解决方案1】:
    queries = ["Party", "Sport"]
    docs = [["Area1","Sport", "Math", "Politics" ]["Area2","Sport","Fun","Party"]["Area3","Fun", "Sport", "Math","Politics"]]
    
    counts = [0] * len(docs)
    for i, doc in enumerate(docs):
        for q in queries:
            counts[i] += doc.count(q)
    
    docs_with_p = [l + [c / (len(l) - 1)] for l, c in zip(docs, count)]
    

    将创建一个新列表docs_with_p,并将百分比(根据子列表的len - 1 从百分比中删除“区域”)附加到每个子列表(文档)的末尾。但是,您可能应该只制作一本字典。

    docs_to_p = {l: c / (len(l) - 1) for l, c in zip(docs, count)}
    

    因为它可能更容易用于此后发生的任何事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-03
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 2013-09-11
      • 1970-01-01
      • 2018-03-13
      相关资源
      最近更新 更多