这里是一个使用Python的示例代码,来按照数量显示结果列表:
# 示例数据
data = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
# 使用字典来计数
count_dict = {}
for item in data:
if item in count_dict:
count_dict[item] += 1
else:
count_dict[item] = 1
# 按照数量降序排序
sorted_count = sorted(count_dict.items(), key=lambda x: x[1], reverse=True)
# 打印结果列表
for item, count in sorted_count:
print(item, count)
输出结果:
apple 3
banana 2
orange 1
这段代码首先使用一个字典来计数列表中每个元素出现的次数。然后,通过对字典进行排序,按照数量降序排列。最后,遍历排序后的结果列表,打印每个元素和对应的数量。
上一篇:按照数量平均分割文件
下一篇:按照顺时针排序点的C#代码