《Python语言程序设计》课件-第六章(中英文课件)_第1页
《Python语言程序设计》课件-第六章(中英文课件)_第2页
《Python语言程序设计》课件-第六章(中英文课件)_第3页
《Python语言程序设计》课件-第六章(中英文课件)_第4页
《Python语言程序设计》课件-第六章(中英文课件)_第5页
已阅读5页,还剩92页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Python语言程序设计《字典的定义》PythonLanguageProgrammingDictionaryDefinitions同现实中的字典相似,Python的字典利用”Key-Value”机制对存入字典的数据进行快速查找定位。案例字典的定义Similartothedictionaryinreality,Python'sdictionaryusesthe"KeyValue"mechanismtoquicklysearchandlocatethedatastoredinthedictionary.Casedictionarydefinitions案例字典的定义dict1={'张三':25,'李四':16,'王五':40}d={key1:value1,key2:value2}【例】一个简单的字典实例字典注意事项:(1)键必须是唯一的,但值可以不唯一。(2)值可以取任何数据类型,但键必须是不可变的,如字符串、数字或元组。(3)字典的键值是“只读”的,所以不能对键和值分别进行初始化。键用列表类型可以吗?Dict1={'ZhangSan':25,'LiSi':16,'WangWu':40}d={key1:value1,key2:value2}[Example]AsimpleexampleofadictionarythatDictionaryNotes.(1)Thekeymustbeunique,butthevaluemaynotbeunique.(2)Thevaluecantakeanydatatype,butthekeymustbeimmutable,suchasastring,numberortuple.(3)Thekeysandvaluesofthedictionaryare"read-only",sothekeysandvaluescannotbeinitializedseparately.Isitokaytousealisttypeforkeys?Casedictionarydefinitions案例字典的定义dic={}dic.keys=(1,2,3,4,5,6)dic.values=("a","b","c","d","e","f")【例】值是只读的,不可以修改AttributeError:'dict'objectattribute'keys'isread-onlyAttributeError:'dic'objectattribute'values'isread-onlydic={}dic.keys=(1,2,3,4,5,6)dic.values=("a","b","c","d","e","f")[Example]Thevalueisread-onlyandcannotbemodifiedAttributeError:'dict'objectattribute'keys'isread-onlyAttributeError:'dic'objectattribute'values'isread-onlyCasedictionarydefinitions问题1:键必须是唯一还是不唯一的?唯一的问题2:字典中的Value值可以重复吗?提问可以Question1:Dokeyshavetobeuniqueornon-unique?TheoneandonlyQuestion2:Canthevalueinthedictionaryberepeated?AskaquestionYes,youcan问题3:字典中的键用列表类型可以吗?提问值可以取任何数据类型,但键必须是不可变的,如字符串、数字或元组Question3:Isitoktousealisttypeforkeysinadictionary?Thevaluecantakeanydatatype,butthekeymustbeimmutable,suchasastring,number,ortupleAskaquestionPython语言程序设计《访问字典里的值》PythonLanguageProgrammingAccesstoDictionaryValues案例访问字典里的值dict={'Name':'DerisWeng','Age':7,'Class':'First'}print("dict['Name']:",dict['Name'])print("dict['Age']:",dict['Age'])访问字典中的值可以通过dict[Key]的方式,即把相应的键放到方括弧[]中进行访问。【例】通过直接引用访问字典中的值dict['Name']:DerisWengdict['Age']:7Toaccessthevaluesinthedictionarydict={'Name':'DerisWeng','Age':7,'Class':'First'}print("dict['Name']:",dict['Name'])print("dict['Age']:",dict['Age'])Thevaluesinthedictionarycanbeaccessedbydict[Key],thatis,placingthecorrespondingkeyinthesquarebracket[].[Example]Accessingavalueinadictionarybydirectreferencedict['Name']:DerisWengdict['Age']:7Case案例访问字典里的值dict={'Name':'DerisWeng','Age':7,'Class':'First'}print("dict['Christopher']:",dict['Christopher'])如果使用字典里没有对应的键,则访问数据时会报错。【例】访问字典中没有的键Traceback(mostrecentcalllast):File"test1.py",line5,in<module>print("dict['Christopher']:",dict['Christopher'])KeyError:'Christopher'dict={'Name':'DerisWeng','Age':7,'Class':'First'}print("dict['Christopher']:",dict['Christopher'])Ifyouuseadictionarythatdoesnothaveacorrespondingkey,youwillgetanerrorwhenaccessingthedata.[Example]AccessingakeythatisnotinthedictionaryTraceback(mostrecentcalllast):File"test1.py",line5,in<module>print("dict['Christopher']:",dict['Christopher'])KeyError:'Christopher'ToaccessthevaluesinthedictionaryCase案例访问字典里的值dict={'Name':'DerisWeng','Age':7,'Class':'First'}print(“dict[‘Christopher’]:”,dict.get(‘Christopher’))利用get方法访问【例】利用get方法,访问字典中没有的键dict[‘Christopher’]:Nonedict={'Name':'DerisWeng','Age':7,'Class':'First'}print(“dict[‘Christopher’]:”,dict.get(‘Christopher’))Accesswithgetmethod[Example]Usethegetmethodtoaccesskeysnotinthedictionarydict[‘Christopher’]:NoneToaccessthevaluesinthedictionaryCase问题:根据key得到value的方法有哪几种?dict[Key]dict.get(Key)提问Question:Whatarethemethodstogetthevalueaccordingtothekey?dict[Key]dict.get(Key)AskaquestionPython语言程序设计《修改字典》PythonLanguageProgrammingModifyingDictionaries案例修改字典dict={'Name':'DerisWeng','Age':7,'Class':'First'}dict['Age']=18;

dict['School']="WZVTC"

print("dict['Age']:",dict['Age'])print("dict['School']:",dict['School'])通过直接引用赋值的方式对字典进行修改【例】修改字典dict['Age']:18dict['School']:WZVTC#更新Age#添加信息CaseModifythedictionarydict={'Name':'DerisWeng','Age':7,'Class':'First'}dict['Age']=18;

dict['School']="WZVTC"

print("dict['Age']:",dict['Age'])print("dict['School']:",dict['School'])modificationstothedictionarybydirectreferenceassignment[Example]Modifythedictionarydict['Age']:18dict['School']:WZVTC#UpdateAge#Addinformation问题:对字典进行修改时,key在字典中存在与不存在会有什么区别?如果dict[Key]中的Key在字典中不存在,则赋值语句将向字典中添加新内容,即增加新的键值对;如果dict[Key]中的Key在字典中存在,则赋值语句将修改字典中的内容。提问Question:Whenmodifyingadictionary,whatisthedifferencebetweentheexistenceandnonexistenceofakeyinthedictionary?Ifthekeyindict[Key]doesnotexistinthedictionary,theassignmentstatementwilladdnewcontenttothedictionary,thatis,addanewkeyvaluepair;Ifthekeyindict[Key]existsinthedictionary,theassignmentstatementwillmodifythecontentinthedictionary.AskaquestionPython语言程序设计《删除字典元素》PythonLanguageProgrammingDeletingDictionaryElements31可以用del命令删除单一的元素或者删除整个字典,也可以用clear清空字典。【例】删除字典元素dict={'Name':'Runoob','Age':7,'Class':'First'}

deldict[‘Name’]#删除键‘Name’dict.clear()#删除字典deldict#删除字典print(“dict[‘Age’]:”,dict[‘Age’])print(“dict[‘School’]:”,dict[‘School’])会发生异常,因为执行del操作后字典不再存在。案例删除字典元素32Youcanusethedelcommandtodeleteasingleelementordeletetheentiredictionary,orclearthedictionary.[Example]Deletingdictionaryelementsdict={'Name':'Runoob','Age':7,'Class':'First'}

Deldict['Name']#Deletekey'Name'Dict.clear()#DeletedictionaryDeldict#Deletedictionaryprint(“dict[‘Age’]:”,dict[‘Age’])print(“dict[‘School’]:”,dict[‘School’])Anexceptionoccursbecausethedictionarynolongerexistsafterthedeloperation.DeletingdictionaryelementsCasePython语言程序设计《字典键的特性》PythonLanguageProgrammingCharacterizationofDictionaryKeys35两个重要的点需要记住:【例】重复出现的情况dict={'Name':'DerisWeng','Age':7,'Name':'DerisWeng'}

print(“dict[‘Name’]:”,dict[‘Name’])dict[‘Name’]:DerisWeng1)不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住。案例字典键的特性36Twoimportantpointstoremember.[Example]Repeatedoccurrencesdict={'Name':'DerisWeng','Age':7,'Name':'DerisWeng'}

print(“dict[‘Name’]:”,dict[‘Name’])dict[‘Name’]:DerisWeng1)Donotallowthesamekeytoappeartwice.Ifthesamekeyisassignedtwiceduringcreation,thelattervaluewillberemembered.CharacterizationofdictionarykeysCase37两个重要的点需要记住:【例】不能用列表作为键dict={['Name‘]:'DerisWeng','Age':7}

print(“dict[‘Name’]:”,dict[‘Name’])报错2)键必须不可变,所以可以用数字,字符串或元组充当,而用列表就不行。案例字典键的特性38[Example]Youcan'tusealistasakeydict={['Name‘]:'DerisWeng','Age':7}

print(“dict[‘Name’]:”,dict[‘Name’])Reportanerror2)Thekeymustbeimmutable,soitcanbefilledwithnumbers,stringsortuples,butnotwithlists.CharacterizationofdictionarykeysCaseTwoimportantpointstoremember.Python语言程序设计39《字典的方法》PythonLanguageProgramming40TheDictionaryApproach41【字典的方法】知识点6dict.clear()

删除字典内所有元素dict.copy()

返回一个字典的浅复制dict.fromkeys()

创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值dict.get(key,default=None)

返回指定键的值,如果值不在字典中返回default值keyindict

如果键在字典dict里返回true,否则返回false2字典知识要点42[Dictionarymethod]Knowledgepoint6dict.clear()

Deleteallelementsinthedictionarydict.copy()

Returnsashallowcopyofadictionarydict.fromkeys()

Createanewdictionary,usetheelementsinthesequenceseqasthedictionarykeys,andvalistheinitialvaluecorrespondingtoallthekeysinthedictionarydict.get(key,default=None)

Returnthevalueofthespecifiedkey.Ifthevalueisnotinthedictionary,returnthedefaultvaluekeyindict

Ifthekeyreturnstrueinthedictionarydict,otherwiseitreturnsfalse2DictionaryKnowledgePoints43radiansdict.items()

以列表返回可遍历的(键,值)元组数组radiansdict.keys()

以列表返回一个字典所有的键radiansdict.setdefault(key,default=None)

和get()类似,但如果键不存在于字典中,将会添加键并将值设为defaultradiansdict.update(dict2)

把字典dict2的键/值对更新到dict里radiansdict.values()

以列表返回字典中的所有值2字典知识要点[Dictionarymethod]Knowledgepoint644radiansdict.items()

Returnsanarrayoftraversable(key,value)tuplesasalistradiansdict.keys()

Returnsallthekeysofadictionaryasalistradiansdict.setdefault(key,default=None)

Similartoget(),butifthekeydoesnotexistinthedictionary,thekeywillbeaddedandthevaluewillbesettodefaultradiansdict.update(dict2)

Updatethekey/valuepairofdict2todictradiansdict.values()

Returnsallthevaluesinthedictionaryasalist2DictionaryKnowledgePoints[Dictionarymethod]Knowledgepoint645【字典的方法】知识点6pop(key[,default])

删除字典给定键key所对应的值,返回值为被删除的值。key值必须给出。否则,返回default值。popitem()

随机返回并删除字典中的一对键和值。2字典知识要点46pop(key[,default])

Deletethevaluecorrespondingtothekeygiveninthedictionary.Thereturnvalueisthedeletedvalue.Thekeyvaluemustbegiven.Otherwise,thedefaultvalueisreturned.popitem()

Randomlyreturnsandremovesapairofkeysandvaluesfromthedictionary.2DictionaryKnowledgePoints[Dictionarymethod]Knowledgepoint6Python语言程序设计47《字典内置函数》PythonLanguageProgramming48DictionaryBuilt-inFunctions49【字典内置函数】知识点7函数及描述实例len(dict)

计算字典元素个数,即键的总数。>>>dict=

{'Name':

'Runoob',

'Age':7,

'Class':

'First'}>>>len(dict)3str(dict)

输出字典,以可打印的字符串表示。>>>dict=

{'Name':

'Runoob',

'Age':7,

'Class':

'First'}>>>str(dict)"{'Name':'Runoob','Class':'First','Age':7}"type(variable)

返回输入的变量类型,如果变量是字典就返回字典类型。>>>dict=

{'Name':

'Runoob',

'Age':7,

'Class':

'First'}>>>type(dict)<class

'dict'>2字典知识要点50[DictionaryBuilt-inFunctions]Knowledgepoint7functionsanddescriptionsInstancelen(dict)

Countsthenumberofdictionaryelements,i.e.,thetotalnumberofkeys.>>>dict=

{'Name':

'Runoob',

'Age':7,

'Class':

'First'}>>>len(dict)3str(dict)

Outputsthedictionaryasaprintablestring.>>>dict=

{'Name':

'Runoob',

'Age':7,

'Class':

'First'}>>>str(dict)"{'Name':'Runoob','Class':'First','Age':7}"type(variable)

Returnsthetypeoftheinputvariable,orthedictionarytypeifthevariableisadictionary.>>>dict=

{'Name':

'Runoob',

'Age':7,

'Class':

'First'}>>>type(dict)<class

'dict'>2DictionaryKnowledgePointsPython语言程序设计51《集合的定义+添加集合元素》PythonLanguageProgramming52DefinitionofaCollection+AddingCollectionElements知识要点53【集合的定义】知识点1集合:确定的一堆“东西(元素)”,无序的,不重复的思考:对比列表?KnowledgePoints54[Definitionofset]Knowledgepoint1Set:adefinitepileof"things(elements)",unordered,non-repeatingThink:Compareandcontrastlists?知识要点55set1={'张三','李四','王五'}【例】定义一个简单的集合实例集合注意事项:(1)所有元素放在花括号{}里。(2)元素间以逗号(,)分隔。(3)可以有任意数量的元素(4)不能有可变元素(例如,列表、集合、字典)【集合的定义】知识点1【例】定义一个空集合set1=set()注意:不能使用{}定义空集合,因为{}表示空字典KnowledgePoints56Set1={'ZhangSan','LiSi','WangWu'}[Example]DefineasimplecollectioninstancethatAssemblyNotes.(1)Allelementsareplacedincurlybraces{}.(2)Elementsareseparatedbyacomma(,).(3)canhaveanynumberofelements(4)Cannothavemutableelements(e.g.,lists,sets,dictionaries).[Example]Defineanemptysetthatset1=set()Caution.Youcan'tuse{}todefineanemptycollection,Since{}denotestheemptydictionary,the[Definitionofset]Knowledgepoint1知识要点57【添加集合元素】知识点2添加集合元素有两种方式1.使用add方法2.使用update方法123name={'tom','danny'}name.add('smith')print(name){'tom','danny','smith'}【例】使用add(元素值)

添加单个元素KnowledgePoints58[Addcollectionelement]Knowledgepoint2Therearetwowaystoaddcollectionelements1Usetheaddmethod2.Usetheupdatemethod123name={'tom','danny'}name.add('smith')print(name){'tom','danny','smith'}[Example]Useadd(elementvalue)toaddasingleelement知识要点59【添加集合元素】知识点2添加集合元素有两种方式1.使用add方法2.使用update方法123name={'tom','danny'}name.update(['king','james'])print(name){'king','james','tom','danny'}【例】使用update(任意列表或集合)

添加多个元素123name={'tom','danny'}name.update(['king','james'],{'kevin','smith'})print(name){'kevin','james','king','smith','tom','danny'}注意:update参数可以是任意多个列表或集合KnowledgePoints60Therearetwowaystoaddcollectionelements1Usetheaddmethod2.Usetheupdatemethod123name={'tom','danny'}name.update(['king','james'])print(name){'king','james','tom','danny'}[Example]Useupdate(anylistorset)toaddmultipleelements123name={'tom','danny'}name.update(['king','james'],{'kevin','smith'})print(name){'kevin','james','king','smith','tom','danny'}Caution.Theupdateparametercanbeanynumberoflistsorcollections[Addcollectionelement]Knowledgepoint2Python语言程序设计61《删除集合元素》PythonLanguageProgramming62DeletionofSetElements知识要点63【删除集合元素】知识点3删除集合元素有四种方式 1.使用remove方法2.使用discard方法

3.使用pop方法4.使用clear方法123name={'king','james','tom','danny'}name.remove('james')print(name){'danny','tom','king'}【例】使用remove(指定元素)

删除指定元素KnowledgePoints64Therearefourwaystodeleteelementsofacollection,the

1.Usetheremovemethod2.Usethediscardmethod

3.Usepopmethod4.Useclearmethod123name={'king','james','tom','danny'}name.remove('james')print(name){'danny','tom','king'}[Example]Useremovetodeletethespecifiedelement[Deletesetelements]Knowledgepoint3知识要点65删除集合元素有四种方式 1.使用remove方法2.使用discard方法

3.使用pop方法4.使用clear方法123name={'king','james','tom','danny'}name.discard('james')print(name){'danny','tom','king'}【例】使用discard(指定元素)

删除指定元素【删除集合元素】知识点3KnowledgePoints66[Deletesetelements]Knowledgepoint3Therearefourwaystodeleteelementsofacollection,the

1.Usetheremovemethod2.Usethediscardmethod

3.Usepopmethod4.Useclearmethod123name={'king','james','tom','danny'}name.discard('james')print(name){'danny','tom','king'}[Example]Usediscardtodeletethespecifiedelement知识要点67【删除集合元素】知识点3删除集合元素有四种方式 1.使用remove方法2.使用discard方法

3.使用pop方法4.使用clear方法123name={'king','james','tom','danny'}name.remove('mike')print(name)Traceback(mostrecentcalllast):File"test.py",line28,in<module>name.remove('mike')KeyError:'mike'注意:remove(指定元素)使用discard(指定元素)

的区别区别:若集合不存在指定元素。remove()会引发报错,discard()正常运行KnowledgePoints68Therearefourwaystodeleteelementsofacollection,the

1.Usetheremovemethod2.Usethediscardmethod

3.Usepopmethod4.Useclearmethod123name={'king','james','tom','danny'}name.remove('mike')print(name)Traceback(mostrecentcalllast):File"test.py",line28,in<module>name.remove('mike')KeyError:'mike'Note:Thedifferencebetweenremove(specifyingtheelement)anddiscard(specifyingtheelement)Difference:Ifthespecifiedelementdoesnotexistintheset.Remove()willcauseanerror,anddiscard()runsnormally[Deletesetelements]Knowledgepoint3知识要点69删除集合元素有四种方式 1.使用remove方法2.使用discard方法

3.使用pop方法4.使用clear方法123name={'king','james','tom','danny'}print(name.pop())#随机返回一个元素print(name)tom{'danny','james','king'}【例】使用pop()

随机删除并返回一个元素注意:因为是随机返回所以每次运行结果会不一样【删除集合元素】知识点3KnowledgePoints70Therearefourwaystodeleteelementsofacollection,the

1.Usetheremovemethod2.Usethediscardmethod

3.Usepopmethod4.Useclearmethod123name={'king','james','tom','danny'}Print(name.pop())#Returnanelementrandomlyprint(name)tom{'danny','james','king'}[Example]Usepop()torandomlydeleteandreturnanelementNote:Sincethereturnisrandomized,thesotheresultswillbedifferentforeachrun[Deletesetelements]Knowledgepoint3知识要点71删除集合元素有四种方式 1.使用remove方法2.使用discard方法

3.使用pop方法4.使用clear方法123name={'king','james','tom','danny'}name.clear()print(name)set()【例】使用clear()

清空集合内所有元素注意:set()表示空集合【删除集合元素】知识点3KnowledgePoints72Therearefourwaystodeleteelementsofacollection,the

1.Usetheremovemethod2.Usethediscardmethod

3.Usepopmethod4.Useclearmethod123name={'king','james','tom','danny'}name.clear()print(name)set()[Example]Clearallelementsinthesetwithclear()Note:set()representsanemptyset[Deletesetelements]Knowledgepoint3Python语言程序设计73《访问集合元素》PythonLanguageProgramming74AccesstoSetElements知识要点75【访问集合元素】知识点4访问集合元素有三种方式 1.使用pop方法2.使用in操作符

3.使用for循环12name={'king','james','tom','danny'}print(name[0])Traceback(mostrecentcalllast):File"test.py",line41,in<module>print(name[0])TypeError:'set'objectisnotsubscriptable首先必须注意一点:集合是无序的,因此索引也没有意义。所以无法使用索引或者切片的方式访问集合元素不可下标KnowledgePoints76[Accessingsetelements]Knowledgepoint4Therearethreewaystoaccesscollectionelements 1.Usepopmethod2.Useinoperator

3.Usetheforloop12name={'king','james','tom','danny'}print(name[0])Traceback(mostrecentcalllast):File"test.py",line41,in<module>print(name[0])TypeError:'set'objectisnotsubscriptableFirstofall,itisimportanttonotethatcollectionsareunorderedandthereforeindexesaremeaningless.SoitisnotpossibletoaccesstheelementsofacollectionusingindexesorslicesNobidsmaybeplaced知识要点77访问集合元素有三种方式 1.使用pop方法2.使用in操作符

3.使用for循环123name={'king','james','tom','danny'}print(name.pop())#随机返回一个元素print(name)tom{'danny','james','king'}【例】使用pop()

随机删除并返回一个元素注意:pop除了随机返回一个元素外,返回的元素也会被删除掉【访问集合元素】知识点4KnowledgePoints78Therearethreewaystoaccesscollectionelements,the 1.Usepopmethod2.Useinoperator

3.Usetheforloop123name={'king','james','tom','danny'}Print(name.pop())#Returnanelementrandomlyprint(name)tom{'danny','james','king'}[Example]Usepop()torandomlydeleteandreturnanelementNote:Popnotonlyrandomlyreturnsanelement,Thereturnedelementswillalsobedeleted[Accessingsetelements]Knowledgepoint4知识要点79访问集合元素有三种方式 1.使用pop方法2.使用in操作符

3.使用for循环12345name={'king','james','tom','danny'}if'james'inname:print('此元素在集合内')else:print('此元素不在集合内')此元素在集合内【例】使用in

操作符判断某一个元素是否在集合内注意:notin用于判断某个元素是否不在集合内【访问集合元素】知识点4KnowledgePoints80Therearethreewaystoaccesscollectionelements,the 1.Usepopmethod2.Useinoperator

3.Usetheforloop12345name={'king','james','tom','danny'}if'james'inname:

Print('theelementisinthecollection')else:

Print('Thiselementisnotinthecollection')thiselementisintheset[Example]UsetheinoperatortodeterminewhetheranelementisinthesetNote:notinisusedtojudgewhetheranelementisnotintheset[Accessingsetelements]Knowledgepoint4知识要点81访问集合元素有三种方式 1.使用pop方法2.使用in操作符

3.使用for循环123name={'king','james','tom','danny'}foriinname:print(i)dannykingtomjames【例】使用for

循环变量访问所有集合元素【访问集合元素】知识点4KnowledgePoints82Therearethreewaystoaccesscollectionelements,the 1.Usepopmethod2.Useinoperator

3.Usetheforloop123name={'king','james','tom','danny'}foriinname:print(i)dannykingtomjames[Example]Usetheforloopvariabletoaccessallsetelements[Accessingsetelements]Knowledgepoint4Python语言程序设计83《集合和列表的转化》PythonLanguageProgramming84TransformationofSetsandLists知识要点85【集合和列表的转化】知识点5123name_list={'king','james','tom','danny'}name=set(name_list)print(name){'james','king','tom','danny'}【例】使用内置函数set()将列表转化为集合12str_set=set('abcd')print(str_set){'a','d','b','c'}因为字符串=字符的列表所以本质上也是列表转化集合KnowledgePoints86[Transformationofsetsandlists]Knowledgepoint5123name_list={'king','james','tom','danny'}name=set(name_list)print(name){'james','king','tom','danny'}[Example]Usethebuilt-infunctionset()toconvertalisttoaset12str_set=set('abcd')print(str_set){'a','d','b','c'}Sincestring=listofcharactersSoessentiallyit'salsoalistintoacollectionPython语言程序设计87《集合的运算》PythonLanguageProgramming88OperationsonSets知识要点89【集合的运算】知识点6四种基本的集合运算:子集、并集、交集、差集123456name_1={'king','james',

'tom','danny'}name_2={'tom','danny','smith','kevin'}name_3={'king','james'}print(name_3<name_1)print(name_3<name_2)print(name_3.issubset(name_1))TrueFalseTrue【例】使用<操作符

或者issubset方法进行子集运算name_1name_3KnowledgePoints90[Operationsonsets]Knowledgepoint6Thefourbasicsetoperations:subset,union,intersection,anddifference123456name_1={'king','james',

'tom','danny'}name_2={'tom','danny','smith','kevin'}name_3={'king','james'}print(name_3<name_1)print(name_3<name_2)print(name_3.issubset(name_1))TrueFalseTrue[Example]Usethe<operatororissubsetmethodtoperformsubsetoperationsname_1name_3知识要点91【集合的运算】

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论