|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# 数据结构 - 1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "数据结构是计算机存储、组织数据的方式,简单来说是指相互之间存在一种或多种特定关系的数据元素的集合。\n",
- "\n",
- "Python中的数据结构设计的非常巧妙,使用起来非常方便,几乎绝大多数的数据结构都可以通过`list`, `tuple`, `dict`, `string`, `set`等表示,因此用户几乎不需要自己定义数据结构,仅仅使用Python内置的数据结构即可实现非常复杂的算法和操作。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 1. 列表"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "列表是最常用的数据结构,可以把它看作用方括号括起来的数据序列,数据之间用逗号分隔。这些数据都可以通过调用其索引值来访问。\n",
- "\n",
- "`list`的声明只需将变量等同于`[ ]`或`list`即可。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "a = []"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'list'>\n"
- ]
- }
- ],
- "source": [
- "print(type(a))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "可以直接将数据序列分配给列表x,如下所示。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['apple', 'orange', 'peach']\n"
- ]
- }
- ],
- "source": [
- "x = ['apple', 'orange', 'peach']\n",
- "print(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 1.1 索引"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**MOTE: 在Python中,索引从`0`开始。**\n",
- "\n",
- "因此,现在包含两个元素的列表`x`的apple索引值为`0`,orange索引值为`1`。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'apple'"
- ]
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "x[0]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "索引也可以按照相反的顺序进行,如果想先访问最后一个元素,索引从`-1`开始。因此,索引`-1`对应是`peach`,索引`-2`对应的是`orange`。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'peach'"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "x[-1]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "正如你可能猜到的一样,x[0] = x[-2], x[1] = x[-1]。这个概念可以扩展到更多包含元素的列表。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "y = ['carrot','potato']"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在这里我们已经声明过两个列表`x`和`y`,每一个包含自己的数据。现在,这两个列表可以再一次被放入另一个列表`z`中,这个列表被称为`嵌套列表`。\n",
- "\n",
- "NOTE:**这是和很多其他计算机语言不同的地方,不要求列表的元素是相同类型,因此编程的时候会非常方便,这也是为什么Python对人类比较友好的原因。**"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[['apple', 'orange', 'peach'], ['carrot', 'potato'], 'Test']\n"
- ]
- }
- ],
- "source": [
- "z = [x,y, 'Test']\n",
- "print(z)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'orange'"
- ]
- },
- "execution_count": 16,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "z[0][1]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如何获得嵌套列表中的某个元素?以访问上述嵌套列表中数据'apple'为例:\n",
- "* 首先在索引为0处,有一个列表`['apple','orange']` 而在索引为1处有另外一个列表`['carrot','potato']` 。\n",
- "* 因此z[0] 应该给我们第一个包含'apple'的列表。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['apple', 'orange', 'peach']\n"
- ]
- }
- ],
- "source": [
- "z1 = z[0]\n",
- "print(z1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "现在观察z1并不是一个嵌套列表,因此为了获得'apple',z1的索引应该为0。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'apple'"
- ]
- },
- "execution_count": 18,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "z1[0]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在python中,你可以通过每次并排写索引值来访问“apple”,而不是像上面那样做。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'apple'"
- ]
- },
- "execution_count": 19,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "z[0][0]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如果列表中有一个列表,那么您可以通过执行 z[ ][ ][ ] 来访问最里面的值。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 1.2 切片"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "索引只限于访问单个元素,而切片则是访问列表内的一系列数据。换句话说,`切片`返回的是一个列表。\n",
- "\n",
- "切片是通过定义切片列表中需要的父列表中的第一个元素和最后一个元素的索引值来完成的。它被写成`parentlist[a: b]`,其中`a`,`b`是父列表的索引值。如果`a`或`b`未定义,则认为该索引值是`a`未定义时的第一个值,以及`b`未定义时的最后一个值。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2, 3, 4]\n",
- "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
- "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
- "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
- ]
- }
- ],
- "source": [
- "num = [1,2,3,4,5,6,7,8,9]\n",
- "print(num[1:4])\n",
- "print(num[0:])\n",
- "print(num[:])\n",
- "print(num)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3, 4]\n",
- "[5, 6, 7, 8, 9]\n"
- ]
- }
- ],
- "source": [
- "print(num[0:4])\n",
- "print(num[4:])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "您还可以使用固定长度或步长对父列表进行切片。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[1, 4, 7]"
- ]
- },
- "execution_count": 26,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "num[0:9:3]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 1.3 列表的内置函数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "为了找到列表的长度或者列表中元素的数量,我们可以使用**len( )**。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "9"
- ]
- },
- "execution_count": 27,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "len(num)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如果列表包含所有的整数元素,那么 **min( )** 和 **max( )** 给出列表中的最大值和最小值。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "1"
- ]
- },
- "execution_count": 28,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "print(num)\n",
- "min(num)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "9"
- ]
- },
- "execution_count": 29,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "max(num)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "列表可以通过添加\"`+`\"来连接。生成的列表将包含添加的列表的所有元素。结果列表将不是嵌套列表。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 30,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[1, 2, 3, 5, 4, 7]"
- ]
- },
- "execution_count": 30,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "[1,2,3] + [5,4,7]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "可能会出现这样的需求,需要检查预定义列表中是否存在特定的元素。考虑下面的列表。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "names = ['Earth','Air','Fire','Water']"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "检查“Fire”和“Rajath”是否出现在列表名称中。传统的方法是使用for循环遍历列表并使用if条件。但在python中,你可以使用\" a在b中\"的概念,如果a在b中出现,它会返回\"True\"如果不是,它会返回\"False\""
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "False"
- ]
- },
- "execution_count": 32,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'Fir' in names"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 33,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'Fire' in names"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 34,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "False"
- ]
- },
- "execution_count": 34,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'fire' in names"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在一个有字符串作为元素的列表中,**max( )** 和 **min( )** 可以使用。**max( )** 会返回一个ASCII码最大的元素而最小的元素会在使用**min( )** 返回。注意,每次只考虑每个元素的第一个索引,如果它们的值相同,则考虑第二个索引,依此类推。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 35,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "mlist = ['bzaa','ds','nc','az','z','klm']"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "z\n",
- "az\n"
- ]
- }
- ],
- "source": [
- "print(max(mlist))\n",
- "print(min(mlist))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "这里考虑每个元素的第一个索引,因此z有最高的ASCII值,因此它被返回,最小的ASCII值是a。但是如果数字被声明为字符串呢?"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 37,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "nlist = ['1','94','93','1000']"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "94\n",
- "1\n"
- ]
- }
- ],
- "source": [
- "print(max(nlist))\n",
- "print(min(nlist))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "即使数字是在字符串中声明的,也会考虑每个元素的第一个索引,并相应地返回最大值和最小值。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "但是如果你想找到给予字符串长度的 **max( )** 字符串元素,那么我们要在 **max( )** 和 **min( )** 中声明参数'key=len'。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Earth\n",
- "Jet\n"
- ]
- }
- ],
- "source": [
- "names = ['Earth','Jet', 'Air','Fire','Water']\n",
- "print(max(names, key=len))\n",
- "print(min(names, key=len))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "但是即使'Water'的长度为5。**max()** 或 **min()** 函数返回第一个元素当两个或者多个元素具有相同的长度。\n",
- "\n",
- "可以使用任何其他内建函数或lambda函数(后面将讨论)来代替len。\n",
- "\n",
- "通过使用**list()** 函数,一个字符串可以被转化成列表。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 39,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['h', 'e', 'l', 'l', 'o']"
- ]
- },
- "execution_count": 39,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "list('hello')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**append( )** 被用来在列表的最后添加一个元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 40,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "lst = [1,1,4,8,7]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 43,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 1, 1, 1]\n"
- ]
- }
- ],
- "source": [
- "lst.append(1)\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**count( )** 用于计算列表中出现的特定元素的数量。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 50,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0"
- ]
- },
- "execution_count": 50,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lst.count(999)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**append( )** 函数也可以被用来在末尾添加一整个列表。观察可以发现最终得到的列表是嵌套列表。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 45,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "lst1 = [5,4,2,8]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 46,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 1, 1, 1, [5, 4, 2, 8]]\n"
- ]
- }
- ],
- "source": [
- "lst.append(lst1)\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "但是如果嵌套列表不是需要的,那么可以使用**extend()** 函数。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 47,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 1, 1, 1, [5, 4, 2, 8], 5, 4, 2, 8]\n"
- ]
- }
- ],
- "source": [
- "lst.extend(lst1)\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**index( )** 被用来找到一个特殊元素的索引值。注意如果有许多个元素具有相同的值那么元素第一个索引值会被返回。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 48,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0"
- ]
- },
- "execution_count": 48,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lst.index(1)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 51,
- "metadata": {},
- "outputs": [
- {
- "ename": "ValueError",
- "evalue": "999 is not in list",
- "output_type": "error",
- "traceback": [
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
- "\u001b[0;32m<ipython-input-51-67053dd5b65b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlst\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m999\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[0;31mValueError\u001b[0m: 999 is not in list"
- ]
- }
- ],
- "source": [
- "lst.index(999)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**insert(x,y)** 用于在指定的索引值x处插入元素y。**append( )** 函数使得它只能插在最后。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 52,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 'name', 1, 1, 1, [5, 4, 2, 8], 5, 4, 2, 8]\n"
- ]
- }
- ],
- "source": [
- "lst.insert(5, 'name')\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 53,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "lst.insert(-1, 10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 54,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 'name', 1, 1, 1, [5, 4, 2, 8], 5, 4, 2, 10, 8]\n"
- ]
- }
- ],
- "source": [
- "print(lst)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 55,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "15"
- ]
- },
- "execution_count": 55,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "len(lst)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 56,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "lst.insert(15, 20)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 57,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 'name', 1, 1, 1, [5, 4, 2, 8], 5, 4, 2, 10, 8, 20]\n"
- ]
- }
- ],
- "source": [
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**insert(x,y)** 插入但不替换元素。如果希望用另一个元素替换该元素,只需将值赋给该特定索引。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 58,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 'Python', 1, 1, 1, [5, 4, 2, 8], 5, 4, 2, 10, 8, 20]\n"
- ]
- }
- ],
- "source": [
- "lst[5] = 'Python'\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**pop( )** 函数返回列表中的最后一个元素。这类似于堆栈的操作。因此,说列表可以作为堆栈使用是正确的。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 61,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[1, 1, 4, 8, 7, 'Python', 1, 1, 1, [5, 4, 2, 8], 5, 4, 2]"
- ]
- },
- "execution_count": 61,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lst.pop()\n",
- "lst"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "可以指定索引值来弹出与该索引值对应的元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 49,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 8, 7, 'Python', 1, [5, 4, 2, 8], 5]\n"
- ]
- }
- ],
- "source": [
- "lst.pop(2)\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 62,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 'Python', 1, 1, 1, [5, 4, 2, 8], 5, 4, 2]\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "4"
- ]
- },
- "execution_count": 62,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "print(lst)\n",
- "lst.pop(-2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 63,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 'Python', 1, 1, 1, [5, 4, 2, 8], 5, 2]\n"
- ]
- }
- ],
- "source": [
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**pop( )** 用于根据可分配给变量的元素的索引值来删除元素。还可以通过使用**remove()** 函数指定元素本身来删除元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 64,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 1, 1, 1, [5, 4, 2, 8], 5, 2]\n"
- ]
- }
- ],
- "source": [
- "lst.remove('Python')\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "可以替代 **remove** 但是使用索引值的函数是 **del**。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 66,
- "metadata": {
- "scrolled": true
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 1, 1, 1, [5, 4, 2, 8], 5, 2]\n",
- "[1, 4, 8, 7, 1, 1, 1, [5, 4, 2, 8], 5, 2]\n"
- ]
- }
- ],
- "source": [
- "print(lst)\n",
- "del(lst[1])\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 67,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 1, 1, [5, 4, 2, 8], 5, 2]\n"
- ]
- }
- ],
- "source": [
- "del(lst[1:4])\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "可以使用**reverse()** 函数反转列表中出现的所有元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 68,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2, 5, [5, 4, 2, 8], 1, 1, 1, 1]\n"
- ]
- }
- ],
- "source": [
- "lst.reverse()\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "注意嵌套列表 [5,4,2,8] 被视为父列表lst的单个元素。因此在嵌套列表里的元素是不可以被翻转的。\n",
- "\n",
- "Python提供了内置函数 **sort( )** 去按升序排列元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 70,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 4, 7, 8, 8, 10]\n"
- ]
- }
- ],
- "source": [
- "lst = [8, 7, 1, 4, 8, 10]\n",
- "lst.sort()\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "对于降序,因为默认情况下反向条件为False。因此,将其更改为True将按降序排列元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 59,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[10, 8, 8, 4, 1]\n"
- ]
- }
- ],
- "source": [
- "lst.sort(reverse=True)\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "相似地对于包含字符串元素的列表, **sort( )** 会根据他们的ASCII值以升序的方式排列而通过确定reverse=True可以让他们以降序的方式排列。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 74,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['apple', 'orange', 'peach']\n",
- "['peach', 'orange', 'apple']\n"
- ]
- }
- ],
- "source": [
- "names = ['apple', 'orange', 'peach']\n",
- "names.sort()\n",
- "print(names)\n",
- "names.sort(reverse=True)\n",
- "print(names)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如果要根据长度排序我们应该像图示的一样确定key=len。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 72,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['peach', 'apple', 'orange']\n",
- "['orange', 'peach', 'apple']\n"
- ]
- }
- ],
- "source": [
- "names.sort(key=len)\n",
- "print(names)\n",
- "names.sort(key=len,reverse=True)\n",
- "print(names)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 1.4 复制一个列表"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "大多数新的python程序员都会犯这个错误,即**对象的赋值和拷贝的差异**。考虑以下的例子:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 75,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "lista= [2,1,4,3]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 76,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2, 1, 4, 3]\n"
- ]
- }
- ],
- "source": [
- "listb = lista # 对象赋值\n",
- "print(listb)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "这里,我们声明了一个列表,lista = [2,1,4,3]。通过赋值将该列表复制到listb,并复制该列表。现在我们对lista执行一些随机操作。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 77,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2, 1, 4]\n",
- "[2, 1, 4, 9]\n"
- ]
- }
- ],
- "source": [
- "lista.pop()\n",
- "print(lista)\n",
- "lista.append(9)\n",
- "print(lista)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 78,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2, 1, 4, 9]\n"
- ]
- }
- ],
- "source": [
- "print(listb)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "虽然没有对listb执行任何操作,但它也发生了变化。这是因为您将lista、listb指向相同的内存空间。那么如何解决这个问题呢?\n",
- "\n",
- "在切片中我们已经看到parentlist[a:b]从父列表返回一个起始索引a和结束索引b的列表,如果a和b没有被提及,那么默认情况下它会包含第一个到最后一个元素。我们在这里使用相同的概念。通过这样做,我们将lista的数据作为变量分配给listb。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 79,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "lista = [2,1,4,3]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 80,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2, 1, 4, 3]\n"
- ]
- }
- ],
- "source": [
- "listb = lista[:]\n",
- "print(listb)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 81,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2, 1, 4]\n",
- "[2, 1, 4, 9]\n"
- ]
- }
- ],
- "source": [
- "lista.pop()\n",
- "print(lista)\n",
- "lista.append(9)\n",
- "print(lista)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 82,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2, 1, 4, 3]\n"
- ]
- }
- ],
- "source": [
- "print(listb)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "还有其他什么方法能够拷贝一个对象到一个新的变量名字?"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 2. 元组"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "元组与列表相似,但唯一大的区别是列表中的元素可以更改,而**元组中的元素不能更改**。为了更好地理解,请回忆**divmod()** 函数。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 84,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "(3, 1)\n",
- "<class 'tuple'>\n"
- ]
- },
- {
- "ename": "TypeError",
- "evalue": "'tuple' object does not support item assignment",
- "output_type": "error",
- "traceback": [
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
- "\u001b[0;32m<ipython-input-84-aeef14ba6d28>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mxyz\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mxyz\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mxyz\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
- ]
- }
- ],
- "source": [
- "xyz = divmod(10,3)\n",
- "print(xyz)\n",
- "print(type(xyz))\n",
- "xyz[0]=10"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "这里的商必须是3余数必须是1。当10除以3时,这些值不能改变。因此,divmod以元组的形式返回这些值。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "要定义元组,将一个变量分配给paranthesis()或tuple()。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 85,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "tup = ()\n",
- "tup2 = tuple()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如果想直接声明元组,可以在数据的末尾使用逗号。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 76,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "(27,)"
- ]
- },
- "execution_count": 76,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "27,"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "27乘以2得到54,但是乘以一个元组,数据重复两次。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 77,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "(27, 27)"
- ]
- },
- "execution_count": 77,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "2*(27,)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在声明元组时可以分配值。它接受一个列表作为输入并将其转换为元组,或者接受一个字符串并将其转换为元组。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 78,
- "metadata": {
- "scrolled": true
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "(1, 2, 3)\n",
- "('H', 'e', 'l', 'l', 'o')\n"
- ]
- }
- ],
- "source": [
- "tup3 = tuple([1,2,3])\n",
- "print(tup3)\n",
- "tup4 = tuple('Hello')\n",
- "print(tup4)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "它遵循与列表相同的索引和切片。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 41,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2\n",
- "('H', 'e', 'l')\n"
- ]
- }
- ],
- "source": [
- "print(tup3[1])\n",
- "tup5 = tup4[:3]\n",
- "print(tup5)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 2.1 将一个元组映射到另一个元组"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 90,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "(a,b,c)= ('alpha','beta','gamma')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 91,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "alpha beta gamma\n"
- ]
- }
- ],
- "source": [
- "print(a,b,c)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 92,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "(c, b, a) = (a, b, c)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 93,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "('R', 'a', 'j', 'a', 't', 'h', 'K', 'u', 'm', 'a', 'r', 'M', 'P')\n"
- ]
- }
- ],
- "source": [
- "d = tuple('RajathKumarMP')\n",
- "print(d)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 2.2 元组内置函数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**count()** 函数计算元组中存在的指定元素的数量。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 94,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "3"
- ]
- },
- "execution_count": 94,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "d.count('a')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**index()** 函数返回指定元素的索引。如果元素大于1,则返回该指定元素的第一个元素的索引"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 95,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "1"
- ]
- },
- "execution_count": 95,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "d.index('a')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 3. 集合"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "集合主要用于消除序列/列表中的重复数字。它还用于执行一些标准的集合操作。\n",
- "\n",
- "set被声明为set(),它将初始化一个空集。set([sequence])也可以被执行来声明一个包含元素的集"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'set'>\n"
- ]
- }
- ],
- "source": [
- "set1 = set()\n",
- "print(type(set1))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{1, 2, 3, 4}\n"
- ]
- }
- ],
- "source": [
- "set0 = set([1,2,2,3,3,4])\n",
- "print(set0)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{1, 2, 3, 4}\n"
- ]
- }
- ],
- "source": [
- "set1 = set((1,2,2,3,3,4))\n",
- "print(set1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "重复两次的元素2,3只会出现一次。因此在一个集合中,每个元素都是不同的。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 3.1 内置函数"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "set1 = set([1,2,3])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "set2 = set([2,3,4,5])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**union( )** 函数返回一个并集合,该集合包含两个集合的所有元素,但是没有重复。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{1, 2, 3, 4, 5}"
- ]
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "set1.union(set2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**add()** 将向集合中添加一个特定的元素。注意,新添加的元素的索引是任意的,可以放在末尾不需要的任何位置。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{0, 1, 2, 3, 10}\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "{0, 1, 2, 3, 5, 10}"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "print(set1)\n",
- "set1.add(5)\n",
- "set1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**intersection( )** 函数输出一个交集合,该集合包含两个集合中的所有元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{2, 3, 5}"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "set1.intersection(set2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**difference( )** 函数输出一个集合,其中包含在set1中而不在set2中的元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{0, 1, 2, 3, 5, 10}\n",
- "{2, 3, 4, 5}\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "{0, 1, 10}"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "print(set1)\n",
- "print(set2)\n",
- "set1.difference(set2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**pop( )** 是用来移除集合中的任意元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "set1=set([10, 9, 1, 2, 4])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{10, 2, 4, 1}\n",
- "{2, 4, 1}\n"
- ]
- }
- ],
- "source": [
- "print(set1)\n",
- "set1.pop()\n",
- "print(set1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**remove( )** 函数从集合中删除指定的元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{1, 4}"
- ]
- },
- "execution_count": 22,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "set1.remove(2)\n",
- "set1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**clear( )** 用于清除所有元素并将其设置为空集。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "set()"
- ]
- },
- "execution_count": 23,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "set1.clear()\n",
- "set1"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.8.5"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
- }
|