简体   繁体   English

如何从基于另一个列表的列表中过滤元素并获取百分比

[英]How to filter elements from a list based in another list and get percentages

I want to get elements from a list such as我想从列表中获取元素,例如

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'] 

and return the frequency of how many times those elements appear in another list并返回这些元素出现在另一个列表中的频率

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']

I came up with a solution...我想出了一个解决方案...

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

But I am failing somewhere I don't know where yet但我在某个地方失败了,我还不知道在哪里

I think you might find collections.Counter to be a useful module here.我想你可能会发现collections.Counter是一个有用的模块。 It yields a dictionary with every unique item in an array as a key and the number of times that item occurs in the array as the value.它生成一个字典,其中数组中的每个唯一项作为键,该项在数组中出现的次数作为值。 For example, if you had the array ['a', 'b', 'c', 'b'] , then Counter(['a', 'b', 'c', 'b']) => {'a': 1, 'b': 2, 'c': 1}例如,如果你有数组['a', 'b', 'c', 'b'] ,那么Counter(['a', 'b', 'c', 'b']) => {'a': 1, 'b': 2, 'c': 1}

So for your purposes, you'd create a counter for your objects array and then use that to determine how often one of your appeared_elements shows up in the objects array.因此,出于您的目的,您将为您的objects数组创建一个计数器,然后使用它来确定您的出现元素之一appeared_elementsobjects数组中的频率。

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)

For the sample data you gave, your output would be:对于您提供的示例数据,您的 output 将是:

{
    "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
}

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

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