Tandy
Tandy

Reputation: 3

How do I get a number for each value in my list?

New to Python and programming in general. I'm trying to create a program that will pull device counts from Cisco UCM. Currently, I can get the program to print me out a list of models from CUCM, but ultimately I would like to see how many of each model occurs. For example, if the CUCM server has 5 8845's and 3 8865's, I would like Python to quickly display that information.

Here is my current code:

if __name__ == '__main__':

    resp = service.listPhone(searchCriteria={'name':'SEP%'}, returnedTags={'model': ''})

    model_list = resp['return'].phone
    for phone in model_list:
        print(phone.model)

I've tried to create a DataFrame from Pandas but couldn't get it to work. I think the issue is that I haven't stored the phone.model portion as a variable, but am unable to figure out how to do that.

My goal would be to eventually have an output that reads something like:

8845 - 5
8865 - 3

Thanks in advance for the help!

Upvotes: 0

Views: 115

Answers (2)

tgyger
tgyger

Reputation: 241

after playing with CUCM output, i did it like this:

modellist={}
for phone in resp['return']["phone"]:
    if phone["model"] in modellist.keys():
        modellist[phone["model"]] += 1
    else:
        modellist[phone["model"]] = 1


for phone, count in modellist.items():
    print(phone, " - " ,count)

Upvotes: 0

Andrey Fedorov
Andrey Fedorov

Reputation: 9689

It doesn't look like you need Pandas here, plain old Python can write a helper like counts below —

from collections import defaultdict


def counts(xs):
    counts = defaultdict(int)
    for x in xs:
        counts[x] += 1
    return counts.items()

And then you can use it like so —

models = ['a', 'b', 'c', 'c', 'c', 'b']

for item, count in counts(models):
    print(item, '-', count)

The output will be —

a - 1
b - 2
c - 3

Upvotes: 1

Related Questions