Python——字典

Python 字典

字典是使用键值对进行数据的存储,是通过键定位值的,要求键是不允许重复的, 键的内容一旦确定不允许发生变换

字典是无序的可变序列
无序:代表元素没有位置编号,也就是不同过下标获取数据 只能通过键获取数据值
可变:数据值可以修改 可以增加新的键值对 删除键值对

air_temp = {'7点': 17, '9点': 20, '11点': 26, '14点': 30, '16点': 25, '18点': 22}
print(air_temp) # {'7点': 17, '9点': 20, '11点': 26, '14点': 30, '16点': 25, '18点': 22}
# 如果键不存在就报错
print(air_temp['10点'])
# KeyError: '10点'
# 获取数据值推荐方式
print(air_temp.get('11点'), air_temp.get('10点'))   # 26 None
# 获取键值对的个数
print(len(air_temp))  # 6
# 添加新的键值对
air_temp.setdefault('19点', 20)  # 影响的是原数据
print(air_temp)
# 移除键值对
air_temp.pop('11点')
print(air_temp)
# 修改
air_temp['19点'] = 18
print(air_temp)
# 清空字典
air_temp.clear()
# 遍历
# 直接遍历字典 遍历的是字典中的键  [直接对字典进行成员操作 获取的是键]
air_temp = {'7点': 17, '9点': 20, '11点': 26, '14点': 30, '16点': 25, '18点': 22}
for ele in air_temp: # 遍历键
    print(ele, air_temp.get(ele)) # 通过键获取对应的值
# 输出结果:{'7点': 17, '9点': 20, '11点': 26, '14点': 30, '16点': 25, '18点': 22}

# 等价于
print(air_temp.keys())  # 字典中所有的键  dict_keys(['7点', '9点', '14点', '16点', '18点', '19点'])
for k in air_temp.keys():
    print(k)
# 输出结果:
7914161819
# 获取字典中键值组合
print(air_temp.items())  # dict_items([('7点', 17), ('9点', 20), ('14点', 30), ('16点', 25), ('18点', 22), ('19点', 20)])
for item in air_temp.items():
    print(item)
# 输出结果:
('7点', 17)
('9点', 20)
('14点', 30)
('16点', 25)
('18点', 22)
('19点', 18)

# 拆包
for k, v in air_temp.items(): # 拆包
    print(k, v)
#输出结果:
717
920
1430
1625
1822
1918

案例

1.词频统计(词、字符、数字等出现的个数,或者对数据进行归类)

	统计每个字符出现的个数 字符——个数=====>使用字典数据类型展示数据

思路:
1. 遍历字符串,获取每个字符
2. 把字符出现的个数存储在字典中

s = 'good good study day day up'
count_dict = {} # 空字典
for ch in s : # 遍历获取每个字符
    if ch not in count_dict: # 直接对字典进行成员运算获取键
        # 代表第一次出现
        count_dict.setdefault(ch,1) # 第一次出现,添加到字典中
    else:
        # 次数 +1
        count_dict[ch] = count_dict.get(ch) + 1
print(count_dict)
# 输出结果:
{'g': 2, 'o': 4, 'd': 5, ' ': 5, 's': 1, 't': 1, 'u': 2, 'y': 3, 'a': 2, 'p': 1}

归类

问题一:偶数为一类,奇数为一类
nums = [19,27,38,41,25,66,24,32,51]
# 偶数为一类,奇数为一类 【字典中会又两组键对】
classify_dict={'evev':[],'odd':[]}
# 遍历
for ele in nums:
    if ele % 2 == 0:
        classify_dict['evev'].append(ele)
    else:
        classify_dict['odd'].append(ele)
print(classify_dict) # {'evev': [38, 66, 24, 32], 'odd': [19, 27, 41, 25, 51]}
问题二: 计算奇数和偶数各有几个
nums = [19,27,38,41,25,66,24,32,51]
classify_dict={'evev':0,'odd':0} # 计算个数,初始值设置为 0,每出现一个满足条件的数据+1
for ele in nums:
    if ele % 2 == 0:
        classify_dict['evev'] += 1 #次数+1
    else:
        classify_dict['odd'] += 1
print(classify_dict) # {'evev': 4, 'odd': 5}

练习

1.data = "hello nice to meet you nice to meet you too good fine nice ",统计每个字母出现的个数

data = 'hello nice to meet you nice to meet you too good fine nice '
datas = {}
for i in data: # 遍历data中每个字母
    if i not in datas: #判断是否是第一次出现
        datas.setdefault(i,1) # 如果是第一次出现,添加到新字典,并将值设置为 1
    else:
        datas[i] = datas.get(i) + 1 # 如果不是第一次出现,则键值 + 1
print(datas)

输出结果:

{‘h’: 1, ‘e’: 9, ‘l’: 2, ‘o’: 9, ’ ': 13, ‘n’: 4, ‘i’: 4, ‘c’: 3, ‘t’: 5, ‘m’: 2, ‘y’: 2, ‘u’: 2, ‘g’: 1, ‘d’: 1, ‘f’: 1}

2.nums=[82,71,65,43,29,72,64,58,87,39]将十位大于个位的数归为一类,另一种自成一类

nums=[82,71,65,43,29,72,64,58,87,39]
nums_dict={'a':[],'b':[]} # 将数归为两类,设置两个键,键值为空用来存放分类后的值
for i in nums:
    if i//10 > i%10:
        nums_dict['a'].append(i) # 将满足条件的值添加到 a键中
    else:
        nums_dict['b'].append(i)
print(nums_dict)

输出结果:

{‘a’: [82, 71, 65, 43, 72, 64, 87], ‘b’: [29, 58, 39]}

字典数据的过滤

dict0 = {'hello': 1, 'nice': 3, 'to': 2, 'meet': 2, 'you': 2, 'too': 1, 'good': 1, 'fine': 1}
print(dict0)
# 找到次数为1的单词信息  以键值对形式展示
new_dict = {}
for k, v in dict0.items():
    if v == 1:
        new_dict.setdefault(k, v)
print(new_dict)

字典推导式

注意:字典中存放的是键值对 key:value

print({k: v for k, v in dict0.items() if v == 1})
# 单词中包含e的词频信息
new_dict1 = {}
for k, v in dict0.items():
    if 'e' in k:
        new_dict1.setdefault(k, v)
print(new_dict1)

print({k: v for k, v in dict0.items() if 'e' in k})

输出结果:{‘hello’: 1, ‘nice’: 3, ‘meet’: 2, ‘fine’: 1}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值