在Python中计算字典中不同键的数量 [英] Counting the number of distinct keys in a dictionary in Python

查看:65
本文介绍了在Python中计算字典中不同键的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本字典,将关键字映射到该关键字的重复项,但是我只想要一个不同单词的列表,所以我想计算关键字的数量.有没有一种方法可以计算关键字的数量,或者还有另一种方法我应该寻找不同的单词?

I have a a dictionary mapping keywords to the repetition of the keyword, but I only want a list of distinct words so I wanted to count the number of keywords. Is there a way to count the number of keywords or is there another way I should look for distinct words?

推荐答案

len(yourdict.keys())

或者只是

len(yourdict)

如果您希望计算文件中的唯一单词,则可以使用 <代码>设置 并喜欢

If you like to count unique words in the file, you could just use set and do like

len(set(open(yourdictfile).read().split()))

这篇关于在Python中计算字典中不同键的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆