【问题标题】:How to filter elements from a list based in another list and get percentages如何从基于另一个列表的列表中过滤元素并获取百分比
【发布时间】:2021-06-22 19:46:15
【问题描述】:

我想从列表中获取元素,例如

appeared_elements = ['blue-sky','road','white-horse','green-field','tree','dusthaze-sky','brown-horse','yellow-field','black-bison','green-field','dusthaze-sky','dusthaze-sky','blue-sky','snowy-field'] 

并返回这些元素在另一个列表中出现的次数

objects = ['black-bison', 'elephant', 'white-horse', 'brown-horse', 'scarlet-ibis', 'black-ibis', 'white-ibis', 'blue-sky', 'overcast-sky', 'cloudy-sky', 'dusthaze-sky', 'rocky-mountain', 'snowy-mountain', 'birdseye-building', 'perspective-building', 'front-building', 'red-flower', 'purple-flower', 'pink-flower', 'sand', 'tree', 'green-field', 'snowy-field', 'yellow-field', 'road', 'tower', 'blue-ocean', 'green-cliff', 'black-cliff', 'waterfall']

我想出了一个解决方案...

def printPercentages(appeared_elements, objects):
  for n in objects:
    soma = len([m for m if n in appeared_elements])/len(appeared_elements)
    print(soma)

但我在某个地方失败了,我还不知道在哪里

【问题讨论】:

  • 在您的列表理解中看起来像是一个错字——应该是[m for m in objects if m in appeared_elements],或者类似的东西?
  • 你能举一个你期望输出的例子吗?我不确定“这些元素在另一个列表中出现多少次的频率”是什么意思——是 appeared_elements 中出现在 objects 中的元素的百分比(唯一与否?),还是相反,或者每个元素在objects 中重复的频率,或者...?
  • @Samwise... 是的,这就是出现在对象中的元素(是否唯一?)的百分比
  • 你没有回答“独特与否?”部分...
  • 不是唯一的,@Samwise

标签: python arrays list for-loop filter


【解决方案1】:

我想你可能会发现 collections.Counter 在这里是一个有用的模块。它生成一个字典,其中数组中的每个唯一项作为键,该项在数组中出现的次数作为值。例如,如果你有数组['a', 'b', 'c', 'b'],那么Counter(['a', 'b', 'c', 'b']) => {'a': 1, 'b': 2, 'c': 1}

因此,出于您的目的,您应该为您的 objects 数组创建一个计数器,然后使用它来确定您的 appeared_elements 之一出现在 objects 数组中的频率。

from collections import Counter

def printPercentages(objects, appeared_elements):
    counter = Counter(objects)
    soma = {}
    for e in appeared_elements:
        soma[e] = (counter.get(e) or 0)/len(objects) * 100 # this will give you the freq of each element saved to a dictionary so you can see them all.
    print(soma)

对于您提供的示例数据,您的输出将是:

{
    "black-bison": 7.142857142857142,
    "elephant": 0.0,
    "white-horse": 7.142857142857142,
    "brown-horse": 7.142857142857142,
    "scarlet-ibis": 0.0,
    "black-ibis": 0.0,
    "white-ibis": 0.0,
    "blue-sky": 14.285714285714285,
    "overcast-sky": 0.0,
    "cloudy-sky": 0.0,
    "dusthaze-sky": 21.428571428571427,
    "rocky-mountain": 0.0,
    "snowy-mountain": 0.0,
    "birdseye-building": 0.0,
    "perspective-building": 0.0,
    "front-building": 0.0,
    "red-flower": 0.0,
    "purple-flower": 0.0,
    "pink-flower": 0.0,
    "sand": 0.0,
    "tree": 7.142857142857142,
    "green-field": 14.285714285714285,
    "snowy-field": 7.142857142857142,
    "yellow-field": 7.142857142857142,
    "road": 7.142857142857142,
    "tower": 0.0,
    "blue-ocean": 0.0,
    "green-cliff": 0.0,
    "black-cliff": 0.0,
    "waterfall": 0.0
}

【讨论】:

  • 啊,objectsappeared_elements 从我的理解中倒退了 - 我将编辑我的答案。另外,我添加了一个* 100 来给你百分比
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2018-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-10
相关资源
最近更新 更多