【发布时间】:2018-05-08 00:41:55
【问题描述】:
我正在尝试找出在列表列表中最常出现值的方法。我尝试使用 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})
【问题讨论】:
-
别害羞,你会喜欢 Counter。
-
我很想看看它是如何工作的。我的意思是它能够按颜色提取计数。只需要显示最大值
-
我现在需要帮助解决 NoneType 不可迭代错误
-
每当我使用 Mythic Rare 时它都有效,但在我使用 Uncommon 时无效