欢迎光临散文网 会员登陆 & 注册

Python笔记-7 字典

2023-06-27 11:25 作者:littersho  | 我要投稿
# # 列表是有序的对象集合,可以用角标获取list中的元素
# 可以用角标获取list中的元素
# brand=['李宁','鸿星尔克','361度']
# slogan=['一切皆有可能','TO BE No.1','多一度热爱']
# print(brand[0])
# print(slogan[2])
# # 元组是有序的
# t1 = (1,2,'3')
# print (t1[1])
# 集合是无序的,不支持索引操作的
# sit1 = {1,2,6,'a',1,2}
# print (sit1[0])
 
# brand=['李宁','鸿星尔克','361度']  #作为key

# slogan=['一切皆有可能','TO BE No.1','多一度热爱'] #作为value

字典的每个键值 key=>value 对用冒号 : 分割,每个对之间用逗号(,)分割,整个字典包括在花括号 {} 中

键必须是唯一的,但值则不必。

值可以取任何数据类型,但键必须是不可变的,如字符串,数字。



# dict={'李宁': '一切皆有可能', '鸿星尔克': 'TO BE No.1','361度':'多一度热爱'}
# print("李宁的slogan是: ", dict['李宁'])
#
# ####################################################
#
#
# ############创建方式及注意点#############
# 创建字典  字典用花括号({ })表示
# people_01 = {'Name': '小明', 'Age': 3}
#
# # 可以定义空字典
# people ={}
# print(type(people))
#
# #也可以用dict函数通过关键字的参数来创建字典:
# d = dict(name='wzw', number=22)
# print(d)
#
# people_01 = {'Name': '小明', 'Age': 3, 'Name': '菜鸟'} #唯一性
# print ("这个人的名字: ", people_01['Name'])
# people_02 = {['下雨', '晴天']: '小明', 'Age': 3, 'Name': '菜鸟'} #不可变
#
# # 字典里嵌套列表
# tem = {'北京': [22, '多云'], '上海': [23, '晴天'], '深圳': [23, '小雨'], '广州': [23, '阴天']}
# print(tem)
#
# # 打印上海天气整体情况
# print(tem['上海'])
#
# # 打印上海最高温度
# print(tem['上海'][0])
 
############基本操作和内置函数#############
# # 访问字典里的值  字典中访问值:字典名+方括号+方括号内填要取的值的键。 注意和get方法的区别
# people_01 = {'Name': '小明', 'Age': 3, 'Name': '菜鸟'}
# print ("这个人的名字: ", people_01['Name'])
# #print ("这个人的年龄: ", people_01['Country'])
# # get是个更宽松的访问字典项的方法,当get访问一个不存在的键时,不会报错而会得到None值
# print(people_01.get('Name'))
# print(people_01.get('Country'))
 
# 修改字典
# people_01 = {'Name': '小明', 'Age': 3}
# people_01['Age'] = 7 # 更新
# people_01['Country'] = "China" # 添加  前提是字典中不存在相同的键,如果存在相同的键,则为覆盖(同上)值操作。 = update() 方法
# print(people_01)
# print ("这个人的年龄: ", people_01['Age'])
# print ("这个人来自: ", people_01['Country'])
 
# # update() 方法向字典插入指定的项目
# car = {'brand': '宝马','model': '911','year': 1963}
# car.update({'color': 'White'})
# print(car)
# # #还可以用该方法合并两个字典
# car = {'brand': '宝马','model': '911','year': 1963}
# car1 = {'color': 'White','prince': '100万','year': 2021}
# car.update(car1)
# print(car)
 
# #len()方法返回字典中键—值对的数量
# print (len(car))
#
 
# # 检查字典中中是否有含有某键的项  key in 字典名
#
# people_01 = {'Name': '小明', 'Age': 3,'num':[1,2,3]}
# print('Age' in people_01)
# print('Country' in people_01)
 
# #del 删除字典元素
# people_01 = {'Name': '小明', 'Age': 3,'num':[1,2,3]}
# del people_01['Age'] #删除键值对使用 del 语句,这样会永久删除对应的键值对
# print(people_01)
# print('Age' in people_01)
 
# # pop  pop(key[,default])
# # 注意del 语句和 pop() 函数作用相同,pop() 函数有返回值,返回删除的key对应的value。
# # default: 如果没有 key,返回 default 值
#
# people_01 = {'Name': '小明', 'Age': 3,'num':[1,2,3]}
# people_01.pop('Age')
# #pop_obj=people_01.pop('Age')
# print(people_01)
# print('Age' in people_01)
# #print(pop_obj)
# #print(people_01.pop(('Cou'),'本操作删除的key不存在'))
 
# #内置的str()函数可以将指定的值转换为字符串
# people_01 = {'Name': '小明', 'Age': 3}
# people_0s =str(people_01)
# print(people_0s)
# print(type(people_0s))
# print(type(people_01))
  
#####################################################
# 遍历字典
#

# #  1、遍历所有的键-值对 使用函数 items(): 

dict.items() 把字典中每对 key 和 value 组成一个元组,并把这些元组放在列表中返回 

# print(people_01.items())

# people_01 = {'Name': '小明', 'Age': 3, 'Country': 'China','num':[1,2,3]}
# for key,value in people_01.items():
#     print (key, value)
 
# #  2、遍历字典中的所有键  使用函数   keys():  #print(people_01.keys())
# people_01 = {'Name': '小明', 'Age': 3, 'Country': 'China','num':[1,2,3]}
# for key in people_01.keys():
#     print(key)
  
# #  3、遍历了字典中的所有键,那肯定可以拿到所有的值了,  使用键获取值的方法 字典名[键名]: 效果同1
# people_01 = {'Name': '小明', 'Age': 3, 'Country': 'China','num':[1,2,3]}
# for key in people_01.keys():
#     print("\n"+key.title())
#     print(people_01[key])
 
# #  4、遍历字典中的所有值  使用函数   values(): 以列表形式返回字典中的所有值。
# people_01 = {'Name': '小明', 'Age': 3, 'Country': 'China','num':[1,2,3]}
# print (people_01.values())
 
#  5、使用函数   sorted()按key值对字典排序:
# people_01 = {'5': '小明', '3': 3, 'A': 'China','n':[1,2,3], 'F': 'N'}
# print(sorted(people_01.keys()))
# print(sorted(people_01.keys(),reverse=True))
#
# # for key in sorted(people_01.keys()):
# #     print("\n"+key.title())
  
#################其他操作##################
# #clear()方法, dict.clear() 该函数没有任何返回值
# people_01 = {'Name': '小明', 'Age': 3,'num':[1,2,3]}
# print ("开始的键值对数量 : %d" %  len(people_01))
# people_01.clear()
# print ("clear后的数量 : %d" %  len(people_01))
# print(people_01)
 
#copy()方法, dict.copy()
# people_01 = {'Name': '小明', 'Age': 3,'num':[1,2,3]}
# people_02 = people_01.copy()
# print(people_02)
 
##注意:直接赋值和 copy 的区别
# people_01 = {'Name': '小明', 'Age': 3,'num':[1,2,3]}
# people_02 = people_01.copy()  # 浅拷贝:深拷贝父对象(一级目录),子对象(二级目录)不拷贝,还是引用
# people_03 = people_01  #  直接赋值 引用对象
# # 修改 data 数据
# people_01['user'] = 'root' #增加key
# people_01['num'].remove(1)   #修改value
#
# # 输出结果
# print(people_01)
# print(people_02)
# print(people_03) # people_01的引用(别名),所以输出结果都是一致的
 
###fromkeys()方法创建字典
###dict.fromkeys(seq[, value])
###参数  seq -- 字典键值列表。   value -- 可选参数, 设置键序列(seq)的值。
dict={}
dict1 = dict.fromkeys(('Google', 'jingdong', 'Taobao'), 100) #value可不写
print(dict1)
 
dict.fromkeys((1,2,3),'number')# 注意只用来创建新字典,不负责保存。当通过一个字典来调用 fromkeys 方法时,如果需要后续使用一定记得给他复制给其他的变量。
print(dict)
 
dict2=dict.fromkeys((1,2,3),'number')
print(dict2)
  
# 练习:
#userinfo = {'Andy':'111','Ellis':'222','Heywood':'333'}
# #模拟用户登录判断
# userinfo = {'Andy':'111','Ellis':'222','Heywood':'333'}
#
# username = input('请输入用户名:')
# password = input('请输入密码:')
#
# #判断用户输入的登录信息,先判断用户输入的username是否在字典中(key(用户名)是否在字典里)并且判断字典中key所对应的value是否和用户输入的password相等。
# if username in userinfo and userinfo[username] == password:
#     print('登录成功!')
# else:
#     print('登录失败!')


Python笔记-7 字典的评论 (共 条)

分享到微博请遵守国家法律