今日的话题,什么样的学习方式才是正确的,我始终相信勤能补拙

元组

元组和列表相似,但是元组没有增删改的操作,元组的标记是(),

tuple1 = ()
#这就是定义了一个元组
tuple2 = 1,2,3,4,5
#这样对一个变量赋值,变量也是一个元组
tuple3 = (1,2,3,4)
#元组中不仅仅可以放数字,同样也可以放文本,类似的"True"这些也可以放进去,但是元组有一点比较特殊。
tuple4 = ("abc")
#大家可以猜测一下这是个什么类型的数据,他不是元组类型的数据,是字符类型的。那么为什么会出现这种情况呢,原因是,当括号中只有一个数据时候,系统会默认这个小括号为优先级,并不会认为他是一个元组序列,所以当括号中只有一个元素的时候,需要在后边给加上逗号。
tuple5 = ("abc",)
print(type(tuple5))------<class 'tuple'>

使用元组可以解决一些较为简单的问题:比如当识别哪些月份是31天还是30天的时候,因为月份都是固定天数(除了二月),那么使用range()函数比较费劲,我们就可以使用元组实现。

manth = int(input('请输入一个月份数字:'))
if manth in (4,6,9,11)
    print(month ,"有30天")
#这里使用了in 的包含关系,以及元组

解包和打包

# 当给一个变量 赋值的时候使用逗号分隔开多个数据  这个多个数据进行打包的操作  打包成元组赋值给变量
values = 11, 23, 45, 61, 72, 38, 49
print(type(values))  # <class 'tuple'>
​
# 解包  把一个容器中多个数据赋值给多个变量
a, b = 11, 23
print(a, b)

字典

字典是使用键值对进行数据的存储,是通过键定位值的,要求键是不允许重复的, 键的内容一旦确定不允许发生变换
字典是无序的可变序列
    无序:代表元素没有位置编号,也就是不同过下标获取数据 只能通过键获取数据值
    可变:数据值可以修改  可以增加新的键值对  删除键值对
    # 获取数据的话通过标记名取值
print(air_temp['11点'])
# 如果键不存在就报错
# 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()
​
# 遍历
# 直接遍历字典 遍历的是字典中的键  [直接对字典进行成员操作 获取的是键]
for ele in air_temp:
    print(ele, air_temp.get(ele))
​
# 等价于
print(air_temp.keys())  # 字典中所有的键  dict_keys(['7点', '9点', '14点', '16点', '18点', '19点'])
for k in air_temp.keys():
    print(k)
​
print('============')
# 单独拿值
print(air_temp.values())  # dict_values([17, 20, 30, 25, 22, 20])
for v in air_temp.values():
    print(v)
​
print('============')
# 获取字典中键值组合
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)
print('============')
for k, v in air_temp.items():
    print(k, v)
练习:
    1. data = "hello nice to meet you nice to meet you too good fine nice"
        统计每个单词出现的个数
    2. nums = [82, 71, 65, 43, 29, 72, 64, 58, 87, 39]
        将十位大于个位的归为一类   另外一种自成一类
"""
data = "hello nice to meet you nice to meet you too good fine nice"
# 获取单词
words = data.split(' ')
print(words)
count_dict1 = {}
for w in words:
    if w not in count_dict1:
        count_dict1.setdefault(w, 1)
    else:
        count_dict1[w] += 1
print(count_dict1)
​
nums = [82, 71, 65, 43, 29, 72, 64, 58, 87, 39]
classify_dict2 = {'gt': [], 'other': []}
for ele2 in nums:
    if ele2 // 10 % 10 > ele2 % 10:
        classify_dict2['gt'].append(ele2)
    else:
        classify_dict2['other'].append(ele2)
print(classify_dict2)
​
​
# 字典数据的过滤
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})
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值