Back to SDS/2 Parametric Scripts

 

nums = [1,4,10,23,4,6,14,33,6,12,24,34,1,3,6,23,8,19,20,23]

def top_list(items, top):

    dd = {}

    for item in items:

        if item in dd:

            dd[item]=dd[item]+1

        else:

            dd[item]=1

    itemLst = [(dd[key],key) for key in dd]

    itemLst.sort(lambda a, b: cmp(b, a))

    print itemLst

    for i in range(min(top, len(itemLst))):

        yield itemLst[i][1]

 

for i in top_list(nums, 4):

    print i,

 

'''   

>>>  [(3, 23), (3, 6), (2, 4), (2, 1), (1, 34), (1, 33), (1, 24), (1, 20), (1, 19), (1, 14), (1, 12), (1, 10), (1, 8), (1, 3)]

23 6 4 1

'''

 

''' Additional ways to create the dictionary

wdict = {}

for word in words:

    try:

        wdict[word] += 1

    except KeyError:

        wdict[word] = 1

 

wdict = {}

get = wdict.get

for word in words:

    wdict[word] = get(word, 0) + 1

 

wdict.setdefault(key, []).append(new_element)

'''