您的位置:首页 > 编程语言 > Python开发

Python 利用字典计算数据中不重复的数量

2018-03-22 14:58 447 查看
因为字典的键必须是唯一的,那么将数据作为键值,那么键的数量就是不重复的数量

datas = "x1 x2 x2 x3 x4 x5 x6 x7 x7 x8 x9"
for data in datas.split():
print(data)

dict = {data: None for data in datas.split()}#data为键,None为键对应的值(任意值)
print("dict=",dict)
print("不重复的长度:",len(dict))


输出:

x1
x2
x2
x3
x4
x5
x6
x7
x7
x8
x9
dict= {'x1': None, 'x7': None, 'x4': None, 'x9': None, 'x6': None, 'x2': None, 'x5': None, 'x8': None, 'x3': None}
不重复的长度: 9
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: