signed

QiShunwang

“诚信为本、客户至上”

Python-统计列表中出现数字的次数并以字典的形式输出

2021/5/14 20:48:41   来源:
def num(l):

    #创建空字典
    d = {}
    
    #遍历列表
    for i in l:

        #找出列表中int类型的数据
        if type(i) is int:

            if i not in d:
                d[i] = 1
            else:
                d[i] += 1
    return d


l = [1, 2, 'abx', 3, 2, 45, 45, 1, 'ieo']
print(num(l))

打印效果

{1: 2, 2: 2, 3: 1, 45: 2}