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

查看:27
本文介绍了在 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)

如果你想计算文件中的唯一词,你可以使用 set 并喜欢

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天全站免登陆