繁体   English   中英

在列表列表中查找最大值

[英]Find max occurrences in list of lists

我试图在列表列表中找出最常出现值的方法。 我尝试使用Counter,这给了我每个不同事件的计数。 我想要一个不使用Counter的解决方案,因为我不熟悉它,但如果有人可以帮助我,我不反对。

def get_uncommon_colors(self):
    uncommon_colors_list=[]
    colors=['black','red','white','blue']
    for newCard in self.cardlist:
        newCard.rarity.split()
        if newCard.rarity=="Mythic Rare":
            if newCard.get_colors!="None":
                uncommon_colors_list.append(newCard.get_colors())
        else:
            continue
        #test=(Counter(x for sublist in uncommon_colors_list for x in sublist))
    return(uncommon_)

颜色列表清单:

[['White'],
 ['Blue'],
 ['Blue'],
 ['Black'],
 ['Red'],
 ['Red'],
 ['Green'],
 ['Green'],
 ['Red', 'Green'],
 ['White', 'Green'],
 ['Black', 'Red'],
 ['White', 'Blue'],
 ['Blue', 'Black'],
 ['White', 'Blue'],
 ['Blue', 'Red', 'Green']]

使用计数器

Counter({'Black': 3, 'Blue': 6, 'Green': 5, 'Red': 5, 'White': 4})

要获得最常用的颜色,请使用Countermost_common()方法。 第一项是最常见的:

from collections import Counter

list_of_lists = [['White'], ['Blue'], ['Blue'], ['Black'], ['Red'], ['Red'], ['Green'], ['Green'], ['Red', 'Green'], ['White', 'Green'], ['Black', 'Red'], ['White', 'Blue'], ['Blue', 'Black'], ['White', 'Blue'], ['Blue', 'Red', 'Green']]

>>> Counter(colour for sublist in list_of_lists for colour in sublist).most_common(1)
[('Blue', 6)]

如果你想自己做,你可以使用字典:

d = {}

for sublist in list_of_lists:
    for colour in sublist:
        d[colour] = d.get(colour, 0) + 1

>>> max(d.items(), key=lambda t: t[1])
('Blue', 6)

你可以使用词典:

l = [['White'],
 ['Blue'],
 ['Blue'],
 ['Black'],
 ['Red'],
 ['Red'],
 ['Green'],
 ['Green'],
 ['Red', 'Green'],
 ['White', 'Green'],
 ['Black', 'Red'],
 ['White', 'Blue'],
 ['Blue', 'Black'],
 ['White', 'Blue'],
 ['Blue', 'Red', 'Green']]

d = {}
for i in l:
    for j in i:
        if d.get(j):
            d[j] += 1
        else:
            d[j] = 1           

print(d)
{'Black': 3, 'Green': 5, 'Red': 5, 'Blue': 6, 'White': 4}

获得最大颜色和数量:

print(max(d, key=d.get),d[max(d, key=d.get)])
Blue 6

您也可以使用defaultdict来收集计数:

from collections import defaultdict
from operator import itemgetter

def count_occurences(lst):

    # flatten the list, many ways to do this
    all_colors = [color for sublist in lst for color in sublist]

    freq = defaultdict(int)

    for color in all_colors:
        freq[color] += 1

    return freq

产量:

>>> occurences = count_occurences(nested_colours)
>>> print(occurences)
defaultdict(<class 'int'>, {'Black': 3, 'Blue': 6, 'White': 4, 'Red': 5, 'Green': 5})

然后使用简单的itemgetter获取最大值:

>>> print(max(occurences.items(), key = itemgetter(1)))
('Blue', 6)

我会首先压扁列表列表,如下所示:

flattened_color_list = [item for sub_list in color_list for item in sub_list]

然后使用字典理解遍历列表以创建频率字典,如下所示:

frequency = {}
{item: 1 if item not in frequency and not frequency.update({item: 1}) else frequency[item] + 1 if not frequency.update({item: frequency[item] + 1}) else 1 for item in flattened_color_list}

然后从字典中获取最大值,如下所示:

max(frequency.iterkeys(), key=(lambda key: frequency[key]))

此外,嵌套的if语句可能不需要存在于您的代码中。 在第一个if语句中,你确保newCard.rarity等于“Mythic Rare”,所以第二个if语句总是返回true,因为newCard.rarity在那时总是“不等于”“None” 。 你可以摆脱第二个if语句,你的代码也会一样。

def get_uncommon_colors(self):
    uncommon_colors_list=[]
    colors=['black','red','white','blue']
    for newCard in self.cardlist:
        newCard.rarity.split()
        if newCard.rarity=="Mythic Rare":
            if newCard.rarity!="None":
                uncommon_colors_list.append(newCard.get_colors())
        else:
            continue
        #test=(Counter(x for sublist in uncommon_colors_list for x in sublist))
    return(uncommon_)

暂无
暂无

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

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