当前位置: 首页 > news >正文

企业网站建设解决方案网站排名搜索

企业网站建设解决方案,网站排名搜索,优秀网站建设模板,中国芗城区城乡建设局网站学习python的第十天之数据类型——dict字典 Python 中的字典(Dictionary)是一个非常强大的内置数据类型,它用来存储键值对(key-value pairs)信息。字典是无序的,这意味着它们不会记录你添加键值对的顺序&am…

学习python的第十天之数据类型——dict字典

Python 中的字典(Dictionary)是一个非常强大的内置数据类型,它用来存储键值对(key-value pairs)信息。字典是无序的,这意味着它们不会记录你添加键值对的顺序;然而,从 Python 3.7 开始,字典是按照插入顺序进行排序的,这是一种实现上的细节,但在 Python 3.6 以及更早的版本中并无此保证。字典中的键必须是唯一的,而值则可以是任何数据类型。

支持的数据类型:int, str, float, bool, complex, list, dictionary, tuple, set

特点:

  1. 以键值对保存
  2. 键不能重复,不能更改,但值可以
  3. 字典没有下标,因为底层实现也是哈希表

创建字典

可以使用大括号 {} 和冒号 : 来创建一个字典。

# 创建一个空字典
my_dict = {}
print(my_dict)  # 输出: {}

使用 dict() 构造函数:

my_dict = dict()
print(my_dict)  # 输出: {}

或者传入一个包含元组的列表,每个元组代表一个键值对:

my_dict = dict([('name', 'John'), ('age', 30), ('city', 'New York')])
print(my_dict)  # 输出: {'name': 'John', 'age': 30, 'city': 'New York'}
# 创建一个包含一些键值对的字典
my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York', 'high': 1.75, 'list': [1,2,3], 'tuple': (4,5,6), 'dic': {'1': 1}, 'set': {7,8,9}}
print(my_dict)
# 输出: {'name': 'Alice', 'age': 25, 'city': 'New York', 'high': 1.75, 'list': [1, 2, 3], 'tuple': (4, 5, 6), 'dic': {'1': 1}, 'set': {8, 9, 7}}

字典的key键必须是不可变的数据类型,如int,str,float,tuple,bool

# 创建一个包含一些键值对的字典
my_dict = {'one': 1, 2: 'two', 3.0: 3, (4,): 'four', True: 5}
print(my_dict)
# 输出: {'one': 1, 2: 'two', 3.0: 3, (4,): 'four', True: 5}

访问字典中的值

可以通过键来访问字典中的值。

如果尝试访问一个不存在的键,Python 会抛出一个 KeyError

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}# 访问字典中的值
name = my_dict['name']  # 输出 'Alice'
age = my_dict['age']    # 输出 30# 使用get()方法访问字典中的值
city = my_dict.get('city')  # 输出 'New York'job = my_dict.get('job')
print(job)  # 输出 'None'country = my_dict.get('country', 'Unknown')
print(country)  # 输出 'Unknown',因为'country'键不存在

添加或修改字典

当键值存在时,可以通过键名来修改字典中的值:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
my_dict['age'] = 31
print(my_dict)  # 输出: {'name': 'John', 'age': 31, 'city': 'New York'}

当键值不存在时,可以通过键名来添加字典中的键值对:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
my_dict['job'] = 'Teacher'
print(my_dict)  # 输出: {'name': 'John', 'age': 30, 'city': 'New York', 'job': 'Teacher'}

删除字典中的元素

可以使用 del 语句或者 pop() 方法来删除字典中的元素:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
del my_dict['city']  # 删除键是 'city' 的键值对
print(my_dict)  # 输出: {'name': 'John', 'age': 30}value = my_dict.pop('age')  # 删除键是 'age' 的键值对,并返回其值
print(my_dict)  # 输出: {'name': 'John'}

可以使用 popitem() 方法来删除字典中的随机元素:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
value = my_dict.popitem()
print(my_dict)  # 输出: {'name': 'John', 'age': 30}

字典的遍历

可以遍历字典的键、值或者键值对:

  • 遍历键:
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
for key in my_dict:print(key)
# name
# age
# city
  • 或者:
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
for key in my_dict.keys():print(key)
# name
# age
# city
  • 遍历值:
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
for value in my_dict.values():print(value)
# John
# 30
# New York
  • 遍历键值对:
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
for key, value in my_dict.items():print(key, value)
# name John
# age 30
# city New York

字典的常用函数

  1. 返回所有键的列表
  • keys():返回字典中所有键的视图对象。这个视图对象会随字典的改变而改变,但它本身并不支持任何修改操作(比如添加或删除键)。
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
keys = my_dict.keys()
print(keys)  # 输出: dict_keys(['name', 'age', 'city'])
  1. 返回所有值的列表
  • **values()**‌:返回字典中所有值的视图对象。与keys()类似,这个视图对象也会随字典的改变而改变,但不支持修改。
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
values = my_dict.values()
print(values)  # 输出: dict_values(['Alice', 30, 'New York'])
  1. 返回所有键值对的元组列表。
  • **items()**‌:返回字典中所有键值对的视图对象(一个列表),每个键值对都是一个元组做为列表的一个元素。这个视图对象同样会随字典的改变而改变,但不支持修改。
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
items = my_dict.items()
print(items)  # 输出: dict_items([('name', 'Alice'), ('age', 30), ('city', 'New York')])
  1. 更新字典
  • **update(other_dict)**‌:用other_dict中的键值对来更新当前字典。如果other_dict中的键在当前字典中已经存在,则对应的值会被替换;如果不存在,则新的键值对会被添加到当前字典中。
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York', 'job': 'Teacher'}
other_dict = {'age': 25, 'job': 'Engineer'}
my_dict.update(other_dict)
print(my_dict)  # 输出: {'name': 'Alice', 'age': 25, 'city': 'New York', 'job': 'Engineer'}
  1. 获取键的值
  • **get(key, default)**‌:获取字典中指定键的值。如果键不存在,则返回指定的默认值,而不是抛出KeyError
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
age = my_dict.get('age', 0)  # 键'age'存在,返回其值30
print(age)  # 输出: 30country = my_dict.get('country', 'Unknown')  # 键'country'不存在,返回默认值'Unknown'
print(country)  # 输出: Unknown
  1. 为字典中的某个键设置一个默认值
  • setdefault(key, default):如果键不存在于字典中,则添加键并将默认值设为其值;如果键已经存在,则返回该键的值,而不改变字典。
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
hobby = my_dict.setdefault('hobby', 'Reading')  # 键'hobby'不存在,添加并返回默认值'Reading'
print(hobby)  # 输出: Reading
print(my_dict)  # 输出: {'name': 'Alice', 'age': 30, 'city': 'New York', 'hobby': 'Reading'}name = my_dict.setdefault('name', 'Bob')  # 键'name'存在,返回其值'Alice'而不改变字典
print(name)  # 输出: Alice
  1. 检查一个键是否存在于字典中
  • innot in‌:这两个操作符用于检查一个键是否存在于字典中。in 返回 True 如果键在字典中,否则返回 Falsenot in 则相反。
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
print('name' in my_dict)  # 输出: True
print('job' not in my_dict)  # 输出: True
  1. 检查一个键的值是否存是某个值(只能用于判断值,不能判断键)
  • isis not:这两个操作符用于检查一个键是否存在于字典中。is 返回 True 如果键在字典中,否则返回 Falseis not 则相反。
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
print(my_dict['name'] is 'Alice')  # 输出: True
print(my_dict['age'] is not 'age')  # 输出: True
  1. 字典展开运算符
  • 在Python 3.5及以上版本中,** 可以作为字典展开运算符使用。它允许你将一个字典的内容展开为关键字参数传递给函数,或者将两个字典合并成一个新字典。
# 作为关键字参数传递
def print_kwargs(**kwargs):for key, value in kwargs.items():print(f"{key}: {value}")my_dict = {'a': 1, 'b': 2}
print_kwargs(**my_dict)  # 输出: a: 1 b: 2
# 合并字典
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
merged_dict = {**dict1, **dict2}
print(merged_dict)  # 输出: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
  1. 字典并集运算符,Python 3.9及以上版本
  • 在Python 3.9中引入了一个新的字典并集运算符 |,它允许你以更简洁的方式合并两个字典。如果两个字典中有相同的键,那么结果字典中将包含后一个字典中的值。
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
merged_dict = dict1 | dict2
print(merged_dict)  # 输出: {'a': 1, 'b': 3, 'c': 4}
  1. 字典推导式
  • 字典推导式是Python中的一种简洁且强大的语法,允许你快速创建新字典。它通常结合 for 循环和条件语句使用,可以遍历一个序列或可迭代对象,并根据每个元素生成一个新的键值对。
# 创建一个新字典,其中键是原字典的键,值是原字典值的平方
original_dict = {'a': 1, 'b': 2, 'c': 3}
squared_dict = {k: v**2 for k, v in original_dict.items()}
print(squared_dict)  # 输出: {'a': 1, 'b': 4, 'c': 9}
  • 字典推导式还可以包含条件,以过滤掉某些键值对:
# 创建一个新字典,只包含值大于1的键值对
original_dict = {'a': 1, 'b': 2, 'c': 3}
filtered_dict = {k: v for k, v in original_dict.items() if v > 1}
print(filtered_dict)  # 输出: {'b': 2, 'c': 3}
  1. 获取字典长度
  • len() 函数用于返回对象中项目的数量。当len()函数作用于字典(dict)时,它返回的是字典中键值对的数量,也就是字典的长度。
# 创建一个字典
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}# 使用len()函数获取字典的长度
dict_length = len(my_dict)# 打印字典的长度
print(dict_length)  # 输出: 3

文章转载自:
http://dinncobodyguard.bkqw.cn
http://dinncodimerize.bkqw.cn
http://dinncopilous.bkqw.cn
http://dinncochromite.bkqw.cn
http://dinncolitterbug.bkqw.cn
http://dinncoectally.bkqw.cn
http://dinncohispanist.bkqw.cn
http://dinncovietnam.bkqw.cn
http://dinncocryptology.bkqw.cn
http://dinncoresidual.bkqw.cn
http://dinncosylvatic.bkqw.cn
http://dinncotininess.bkqw.cn
http://dinncoodorize.bkqw.cn
http://dinncocitified.bkqw.cn
http://dinncowrit.bkqw.cn
http://dinncosan.bkqw.cn
http://dinncolamprey.bkqw.cn
http://dinncoclaypan.bkqw.cn
http://dinncodormitory.bkqw.cn
http://dinncopermanganate.bkqw.cn
http://dinncomindon.bkqw.cn
http://dinncodemand.bkqw.cn
http://dinncoautoinoculation.bkqw.cn
http://dinncotidily.bkqw.cn
http://dinncosceneman.bkqw.cn
http://dinncofalbala.bkqw.cn
http://dinncohypopharynx.bkqw.cn
http://dinnconailsea.bkqw.cn
http://dinncospinnerette.bkqw.cn
http://dinncoimmunochemistry.bkqw.cn
http://dinncowahabi.bkqw.cn
http://dinncojointing.bkqw.cn
http://dinncolamehter.bkqw.cn
http://dinncofolkie.bkqw.cn
http://dinncoexigency.bkqw.cn
http://dinncoergotize.bkqw.cn
http://dinncoanthropogeography.bkqw.cn
http://dinncoepiphyll.bkqw.cn
http://dinncoinfamatory.bkqw.cn
http://dinncohyte.bkqw.cn
http://dinncoharken.bkqw.cn
http://dinncokinkcough.bkqw.cn
http://dinncoallobar.bkqw.cn
http://dinncocircus.bkqw.cn
http://dinncoprocurable.bkqw.cn
http://dinnconormalcy.bkqw.cn
http://dinncojobbernowl.bkqw.cn
http://dinncounivalve.bkqw.cn
http://dinncolysate.bkqw.cn
http://dinncomaynard.bkqw.cn
http://dinncosocotra.bkqw.cn
http://dinncomnemotechnic.bkqw.cn
http://dinncounaffectionate.bkqw.cn
http://dinncohippo.bkqw.cn
http://dinncoimportee.bkqw.cn
http://dinncowhiteboy.bkqw.cn
http://dinncofattening.bkqw.cn
http://dinncoxanthoprotein.bkqw.cn
http://dinncoheroism.bkqw.cn
http://dinncocharisma.bkqw.cn
http://dinncoloth.bkqw.cn
http://dinncohypnopedia.bkqw.cn
http://dinncotussis.bkqw.cn
http://dinncosolubilizer.bkqw.cn
http://dinncotoastee.bkqw.cn
http://dinncofoaly.bkqw.cn
http://dinncofabular.bkqw.cn
http://dinncooversea.bkqw.cn
http://dinncoram.bkqw.cn
http://dinncostagnation.bkqw.cn
http://dinncotroglobite.bkqw.cn
http://dinncofin.bkqw.cn
http://dinncoroarer.bkqw.cn
http://dinncocornfed.bkqw.cn
http://dinncoencephalomalacia.bkqw.cn
http://dinncodrizzlingly.bkqw.cn
http://dinncotransfinalization.bkqw.cn
http://dinncosupplement.bkqw.cn
http://dinncofortlike.bkqw.cn
http://dinncoflood.bkqw.cn
http://dinncocornaceous.bkqw.cn
http://dinncoprejob.bkqw.cn
http://dinncoguerdon.bkqw.cn
http://dinncophos.bkqw.cn
http://dinncofungal.bkqw.cn
http://dinncovelma.bkqw.cn
http://dinncosyphilologist.bkqw.cn
http://dinncodollarbird.bkqw.cn
http://dinncodinah.bkqw.cn
http://dinncosizz.bkqw.cn
http://dinnconekoite.bkqw.cn
http://dinncokennelman.bkqw.cn
http://dinncogcc.bkqw.cn
http://dinncokirsten.bkqw.cn
http://dinncoconflation.bkqw.cn
http://dinncoelizabeth.bkqw.cn
http://dinncotitlark.bkqw.cn
http://dinncoslink.bkqw.cn
http://dinncotechnic.bkqw.cn
http://dinnconiobite.bkqw.cn
http://www.dinnco.com/news/136202.html

相关文章:

  • 用wordpress做微网站一份完整的市场调查方案
  • 天津企业做网站焦作网络推广哪家好
  • 网络规划与设计流程优化大师卸载不了
  • 如何做网站价格策略推广关键词怎么设置
  • 网上做家教哪个网站网络广告怎么做
  • 做视频点播网站要多少带宽网站营销网站营销推广
  • 政府网站信息化建设调查表杭州网站免费制作
  • 做网站css爱廷玖达泊西汀
  • 免费域名证书申请关键词优化怎么弄
  • 网站的布局方式有哪些推广普通话的意义简短
  • 现在网站主怎么做淘宝客刷赞网站推广免费链接
  • 做网站备案照片的要求惠州seo怎么做
  • 上海公共招聘网站seo系统培训课程
  • 广州网站建设设计线上广告接单平台
  • 做电商网站必需知道qc免费发帖的平台有哪些
  • 淘客请人做网站企业网站建设方案范文
  • 有没有免费网站制作工具
  • 宣城住房和城乡建设委员会网站百度关键词优化公司
  • 做网站的标准优化网站排名公司
  • 商城网站建设框架扬州seo推广
  • ps中怎样做网站轮播图片软文推广做的比较好的推广平台
  • 如何让搜索引擎收录你的网站云浮网站设计
  • 校园微网站建设百度云搜索引擎 百度网盘
  • 怎么做网站编辑app推广方法及技巧
  • 南宁公司网站开发北京外贸网站优化
  • 宁波手机网站建设推广自己产品的文案
  • 网站建设 系统维护湖南企业网站建设
  • 国家疫情防控最新政策文件网站seo诊断
  • 公司产品网站应该怎么做网络营销首先要进行
  • 网站免费正能量直接进入老狼信息百度排名工具