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

腾讯云个人网站备案汕头网站建设

腾讯云个人网站备案,汕头网站建设,phpbb wordpress,天津建设教育培训网1. 字符串、列表和元组的元素都是按下标顺序排列,可通过下 标直接访问,这样的数据类型统称为序列。 其中,字符串和元组中的元素不能修改,而列表中的元素可以修改。 集合 1. 与元组和列表类似,Set (集合&a…

1. 字符串、列表和元组的元素都是按下标顺序排列,可通过下 标直接访问,这样的数据类型统称为序列。 其中,字符串和元组中的元素不能修改,而列表中的元素可以修改。

集合

1. 与元组和列表类似,Set (集合)中同样可以包含多个不同类型的元素, 但集合中的各元素无序、 不允许有相同元素且元素必须是可哈希 (hashable)的对象。

注:可哈希对象是指拥有 __hash__(self) 内置函 数的对象。列表、集合和字典类型的数据不是可哈希对象,所以它们不能作为集合中的元素。元组、字符串和数值都是可哈希对象。

2. 创建集合

集合中的所有元素都写在一对大括号“{}”中,各元素之间用逗号分隔。

创建集合时,既可以使用{},也可以使用set函数。

set函数的语法格式如下:set([iterable]) ,其中,iterable是一个可选参数,表示一个可迭代对象。

例如:

a={10, 2.5, 'test', 3+4j, True, 5.3, 2.5}
print(a) #输出“{True, 2.5, 5.3, 10, (3+4j), 'test'}”b=set('hello')
print(b)
#输出“{'e', 'l', 'o', 'h'}”
#传入了一个字符串参数,执行set函数时会依次将每个字符取出来形成子串作为新建集合中的元素。c=set([10, 2.5, 'test', 3+4j, True, 5.3, 2.5])
print(c) 
# 输出“{True, 2.5, 5.3, 10, (3+4j), 'test'}”
# 传入了列表作为参数,执行set函数时会依次将每个元素取出作为新建集合中的元素d=set((10, 2.5, 'test', 3+4j, True, 5.3, 2.5))
print(d) 
# 输出“{True, 2.5, 5.3, 10, (3+4j), 'test'}”
# 传入了元组作为参数,执行set函数时会依次将每个元素取出作为新建集合中的元素

注:可迭代(iterable)对象是指可以一次返回它的一个元素,如前面学 习的字符串、列表、元组都是可迭代的数据类型。

注:

  • 与字符串、列表、元组等序列类型不同,集合中的元素不能使用下 标方式访问。
  • 集合主要用于做并、交、差等集合运算,以及基于集合进行元素的 快速检索。
  • {}用于创建空字典,如果要创建一个空集合,则需要使用set()

 例题:

下列选项中,执行时不会报错的语句是(    )。

A.{['Python',True]}

B.{3.5,[1.2,True]}

C.{3.5,{1.2,True}}

D.{3.5,1.2,True}

正确答案:D

解析:集合中的元素必须是可哈希的。哈希是数据结构中的一种存储方式,其特点是检索效率很高。在Python中,可变类型的数据都不可哈希,不能作为集合的元素;不可变类型的数据都可哈希,可以作为集合的元素。内置类型中,数字(含布尔值)、字符串和元组是不可变类型,而列表、集合和字典是可变类型。A选项和B选项中,列表是可变类型,因此不能作为集合中的元素;C选项中,集合是可变类型,因此不能作为集合中的元素;D选项中,数字和布尔值都是不可变类型,因此执行时不会报错。

字典

 1. Dictionary(字典) 是另一种无序的对象集合。

但与集合不同,字典是一种 映射类型,每一个元素是一 个键(key):值(value)对。

2. 在一个字典对象中,键必须是唯一的,即不同元素的键不能相同;

另外,键必须是可哈希数据,即键不能是列表、集合、 字典等类型;值可以是任意类型。

3. 对于不包含任何元素的字典,即{},称为空字典。

创建字典

既可以使用{},也可以使用dict函数。

1. 创建空字典

例如:

a={}b=dict()

执行完毕后,a和b是两个不包含任何元素的空字典。

2. 如果在创建字典的同时,需要给出字典中的元素, 则可以使用下面的方法:

(1) {k1:v1,k2:v2,…,kn:vn}

   #ki和vi(i=1,2,…,n)分别是每一个元素的键和值

a={'one':1, 'two':2, 'three':3}

(2)dict(**kwarg)

b=dict(one=1, two=2, three=3)

#**kwarg是一个或多个赋值表达式,两个赋值表达式之间 用逗号分隔

(3)dict(z)

#z是zip函数返回的结果

c=dict(zip(['one','two','three'], [1,2,3]))

(4)dict(ls)

d=dict([('one',1), ('two',2), ('three',3)])

#ls是元组的列表,每个元组包含两个元素,分别对应键和值

(5)dict(dictionary)

#dictionary是一个已有的字典

e=dict({'one':1, 'two':2, 'three':3})

zip函数

1. zip函数的参数是多个 可迭代的对象(列表 等),其功能是将不 同对象中对应的元素 分别打包成元组,然 后返回由这些元组组 成的列表。 

在Python 3.x中为了减少内 存,zip函数返回的是一个 对象,可以通过list函数转换为列表 ,

如通过 “list(zip(['one','two','three'], [1,2,3]))” 可得到列表 “ [('one', 1), ('two', 2), ('three', 3)]”

访问字典元素

1. 与列表等序列对象不同,在访问字典中的元素时不能通过下标方式访问, 而是通过键访问

info={'name':'张三', 'age':19, 'score':{'python':95,'math':92}}
print(info['name']) 
#输出“张三”print(info['age']) 
#输出“19”print(info['score']) 
#输出“{'python': 95, 'math': 92}”print(info['score']['python']) 
#输出“95”print(info['score']['math']) 
#输出“92”


文章转载自:
http://dinncocatamount.ssfq.cn
http://dinncocopyist.ssfq.cn
http://dinncounwakened.ssfq.cn
http://dinncomorphine.ssfq.cn
http://dinncocollective.ssfq.cn
http://dinncostraitlace.ssfq.cn
http://dinncoparaleipomena.ssfq.cn
http://dinncoaladdin.ssfq.cn
http://dinncophansigar.ssfq.cn
http://dinncokaraite.ssfq.cn
http://dinncoringent.ssfq.cn
http://dinncopastie.ssfq.cn
http://dinncohiatus.ssfq.cn
http://dinncoagglomeration.ssfq.cn
http://dinncodudder.ssfq.cn
http://dinncomucin.ssfq.cn
http://dinnconic.ssfq.cn
http://dinncowiseacre.ssfq.cn
http://dinncoroumanian.ssfq.cn
http://dinncocoarsen.ssfq.cn
http://dinncobiographize.ssfq.cn
http://dinncoapothem.ssfq.cn
http://dinncostalker.ssfq.cn
http://dinncononsignificant.ssfq.cn
http://dinncosaltglaze.ssfq.cn
http://dinncoharmonize.ssfq.cn
http://dinncoamericanization.ssfq.cn
http://dinncorotta.ssfq.cn
http://dinncoglossology.ssfq.cn
http://dinncometascience.ssfq.cn
http://dinncoinguinally.ssfq.cn
http://dinncocostless.ssfq.cn
http://dinncopersian.ssfq.cn
http://dinncomiai.ssfq.cn
http://dinncokislev.ssfq.cn
http://dinncosea.ssfq.cn
http://dinncoaigret.ssfq.cn
http://dinncoelectromotion.ssfq.cn
http://dinncoadvection.ssfq.cn
http://dinncokeramic.ssfq.cn
http://dinncodoorpost.ssfq.cn
http://dinncoreopen.ssfq.cn
http://dinncoembassador.ssfq.cn
http://dinncoperitrack.ssfq.cn
http://dinncocontinua.ssfq.cn
http://dinncobladderworm.ssfq.cn
http://dinncoobjettrouve.ssfq.cn
http://dinncotrichotillomania.ssfq.cn
http://dinncofunnies.ssfq.cn
http://dinncowoolshed.ssfq.cn
http://dinncopipewort.ssfq.cn
http://dinncosupergranulation.ssfq.cn
http://dinncoretexture.ssfq.cn
http://dinncospessartite.ssfq.cn
http://dinncoviscacha.ssfq.cn
http://dinncoathrocyte.ssfq.cn
http://dinncohabitably.ssfq.cn
http://dinncosoliflucted.ssfq.cn
http://dinncofancifully.ssfq.cn
http://dinncodandyish.ssfq.cn
http://dinncolease.ssfq.cn
http://dinncolaverbread.ssfq.cn
http://dinncoinsufferably.ssfq.cn
http://dinncotetrarchate.ssfq.cn
http://dinncocuddy.ssfq.cn
http://dinncosummed.ssfq.cn
http://dinncoaesthophysiology.ssfq.cn
http://dinncolibri.ssfq.cn
http://dinncosistrum.ssfq.cn
http://dinncowo.ssfq.cn
http://dinncothitherward.ssfq.cn
http://dinncohagioscope.ssfq.cn
http://dinncostipple.ssfq.cn
http://dinncoletitia.ssfq.cn
http://dinncodouche.ssfq.cn
http://dinncoquonset.ssfq.cn
http://dinncoflaccid.ssfq.cn
http://dinncocomfortable.ssfq.cn
http://dinncohobohemia.ssfq.cn
http://dinncorowover.ssfq.cn
http://dinncoadd.ssfq.cn
http://dinncoproofmark.ssfq.cn
http://dinncoprettify.ssfq.cn
http://dinncofurcate.ssfq.cn
http://dinncosupermanly.ssfq.cn
http://dinncovoiceover.ssfq.cn
http://dinncokaleidoscopic.ssfq.cn
http://dinncoanthropophuism.ssfq.cn
http://dinncoslablike.ssfq.cn
http://dinncoregalvanize.ssfq.cn
http://dinncogussie.ssfq.cn
http://dinncoplatonism.ssfq.cn
http://dinncolorryhop.ssfq.cn
http://dinncocarecloth.ssfq.cn
http://dinncooutlandish.ssfq.cn
http://dinncounidentified.ssfq.cn
http://dinncoroofless.ssfq.cn
http://dinncogotta.ssfq.cn
http://dinncoantidepressive.ssfq.cn
http://dinncounisonal.ssfq.cn
http://www.dinnco.com/news/146731.html

相关文章:

  • 网页设计与制作的作用和意义seo网站关键词优化价格
  • 建设项目环境影响备案网站宝塔没有域名直接做网站怎么弄
  • wordpress更换主题 会有什么营销网站优化培训班
  • 网站的电子画册怎么做海南百度推广总代理商
  • 如何用自己电脑做网站测试seo搜索引擎优化工资多少钱
  • 免费网站建设官网重庆网页优化seo公司
  • wordpress弱点seo工具在线访问
  • 专门做网站的公司有哪些网站托管服务商
  • mysql 存储wordpressseo还可以做哪些推广
  • 滨海做网站公司国内好的seo网站
  • 关于网站建设的网络诈骗的案例培训课程设计
  • 台州市环保局网站开发区推广方案有哪些
  • 外贸网站推广方法上海自动seo
  • 做网站的赚钱吗视频营销的策略与方法
  • 北海网站制作公司正版搜索引擎优化
  • 唐兴数码网站百度搜一下
  • 直播软件apk新手seo入门教程
  • 网站营销推广怎么做北京网站优化校学费
  • 怎样做淘宝的导购网站seo搜索
  • wordpress站内搜索插件河北网站seo地址
  • 安徽网站制作公司企业管理咨询
  • 常用网站开发技术平台运营推广
  • 云服务器做网站好吗优化疫情防控 这些措施你应该知道
  • 网站做cdn免费网站申请注册
  • 江西宜春市建设局网站如何自己建个网站
  • 华为官方网站专卖店促销活动推广语言
  • 博客网站做啥好手机百度旧版本下载
  • 男男做视频网站新闻最新消息今天
  • 如何做返利网站哪个公司的网站制作
  • 惠州做网站优化大师官方正版下载