Python和程序设计的新手。我正在尝试创建一个程序,该程序将从Cisco UCM中提取设备数量。目前,我可以从CUCM中获取该程序以打印出模型列表,但是最终我希望了解每种模型有多少种。例如,如果CUCM服务器有5 8845和3 8865,我希望Python快速显示该信息。
这是我当前的代码:
if __name__ == '__main__':
resp = service.listPhone(searchCriteria={'name':'SEP%'}, returnedTags={'model': ''})
model_list = resp['return'].phone
for phone in model_list:
print(phone.model)
我曾尝试从Pandas创建一个DataFrame,但无法使其正常工作。我认为问题在于我尚未将phone.model部分存储为变量,但是无法弄清楚该怎么做。
我的目标是最终获得一个输出,类似于:
8845 - 5
8865 - 3
提前感谢您的帮助!
这里看起来好像不需要熊猫,普通的旧Python可以像下面的counts
一样写一个助手-
from collections import defaultdict
def counts(xs):
counts = defaultdict(int)
for x in xs:
counts[x] += 1
return counts.items()
然后您可以像这样使用它-
models = ['a', 'b', 'c', 'c', 'c', 'b']
for item, count in counts(models):
print(item, '-', count)
输出将是-
a - 1
b - 2
c - 3
讨厌的方式:-) .....我认为您需要访问CUCM输出,例如“ for resp ['return'] [“ phone”]“]
看看Andrey Fedorov的帖子,并为他投票!我的代码不是很好,我只是关注CUCM的数据结构,这是
{“ return”:{“ phone”:[{“ key”:value},{“ key”:value}]}}]]
for phone in resp['return']["phone"]:
counter = 0
model = phone['model']
for phone in resp['return']["phone"]:
if model == phone['model']:
counter +=1
modelcount.append([str(model), str(counter)])
unique = list(set(tuple(entry) for entry in modelcount))
for entry in unique:
print(entry[0] + " - " + entry[1])