|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055 |
- {
- "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": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "a = []"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "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": 3,
- "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": [
- "在Python中,索引从`0`开始。因此,现在包含两个元素的列表`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`对应是橙色,索引`-2`对应的是苹果。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'peach'"
- ]
- },
- "execution_count": 6,
- "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": 7,
- "metadata": {},
- "outputs": [],
- "source": [
- "y = ['carrot','potato']"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在这里我们已经声明过两个列表`x`和`y`每一个包含自己的数据。现在,这两个列表可以再一次被放入另一个也具有自己的数据的列表`z`中。列表中的这个列表被称为`嵌套列表`,这就是数组的声明方式,我们将在后面看到。**这是和很多其他计算机语言不同的地方,不要求列表的元素是相同类型,因此编程的时候会非常方便,这也是为什么Python对人类比较友好**"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "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": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'orange'"
- ]
- },
- "execution_count": 9,
- "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": 10,
- "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": 11,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'apple'"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "z1[0]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在python中,你可以通过每次并排写索引值来访问“apple”,而不是像上面那样做。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'apple'"
- ]
- },
- "execution_count": 12,
- "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": 13,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[3, 2, 3]\n",
- "[2, 3, 2, 3, 4, 5, 6, 7, 8, 9]\n",
- "[2, 3, 2, 3, 4, 5, 6, 7, 8, 9]\n",
- "[2, 3, 2, 3, 4, 5, 6, 7, 8, 9]\n"
- ]
- }
- ],
- "source": [
- "num = [2,3,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": 14,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2, 3, 2, 3]\n",
- "[4, 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": 15,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[2, 3, 6]"
- ]
- },
- "execution_count": 15,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "num[:9:3]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 1.3 列表的内置函数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "为了找到列表的长度或者列表中元素的数量,我们可以使用**len( )**。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "10"
- ]
- },
- "execution_count": 16,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "len(num)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如果列表包含所有的整数元素,那么 **min( )** 和 **max( )** 给出列表中的最大值和最小值。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2, 3, 2, 3, 4, 5, 6, 7, 8, 9]\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "2"
- ]
- },
- "execution_count": 17,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "print(num)\n",
- "min(num)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "9"
- ]
- },
- "execution_count": 18,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "max(num)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "列表可以通过添加\"`+`\"来连接。生成的列表将包含添加的列表的所有元素。结果列表将不是嵌套列表。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[1, 2, 3, 5, 4, 7]"
- ]
- },
- "execution_count": 19,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "[1,2,3] + [5,4,7]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "可能会出现这样的需求,您可能需要检查预定义列表中是否存在特定的元素。考虑下面的列表。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "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": 21,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "False"
- ]
- },
- "execution_count": 21,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'Fir' in names"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 22,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'Fire' in names"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "False"
- ]
- },
- "execution_count": 23,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'fire' in names"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在一个有字符串作为元素的列表中,**max( )** 和 **min( )** 可以使用。**max( )** 会返回一个ASCII码最大的元素而最小的元素会在使用**min( )** 返回。注意,每次只考虑每个元素的第一个索引,如果它们的值相同,则考虑第二个索引,依此类推。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {},
- "outputs": [],
- "source": [
- "mlist = ['bzaa','ds','nc','az','z','klm']"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "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": 26,
- "metadata": {},
- "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": 29,
- "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": 30,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['h', 'e', 'l', 'l', 'o']"
- ]
- },
- "execution_count": 30,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "list('hello')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**append( )** 被用来在列表的最后添加一个元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {},
- "outputs": [],
- "source": [
- "lst = [1,1,4,8,7]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 1]\n"
- ]
- }
- ],
- "source": [
- "lst.append(1)\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**count( )** 用于计算列表中出现的特定元素的数量。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 35,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "3"
- ]
- },
- "execution_count": 35,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lst.count(1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**append( )** 函数也可以被用来在末尾添加一整个列表。观察可以发现最终得到的列表是嵌套列表。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {},
- "outputs": [],
- "source": [
- "lst1 = [5,4,2,8]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 37,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8]]\n"
- ]
- }
- ],
- "source": [
- "lst.append(lst1)\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "但是如果嵌套列表不是需要的,那么可以使用**extend()** 函数。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 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": 39,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0"
- ]
- },
- "execution_count": 39,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lst.index(1)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 40,
- "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-40-344da5bcf759>\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[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": 41,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 'name', 1, [5, 4, 2, 8], 5, 4, 2, 8]\n"
- ]
- }
- ],
- "source": [
- "lst.insert(5, 'name')\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "metadata": {},
- "outputs": [],
- "source": [
- "lst.insert(-1, 10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 43,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 'name', 1, [5, 4, 2, 8], 5, 4, 2, 10, 8]\n"
- ]
- }
- ],
- "source": [
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**insert(x,y)** 插入但不替换元素。如果希望用另一个元素替换该元素,只需将值赋给该特定索引。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 44,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 4, 8, 7, 'Python', 1, [5, 4, 2, 8], 5, 4, 2, 10, 8]\n"
- ]
- }
- ],
- "source": [
- "lst[5] = 'Python'\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**pop( )** 函数返回列表中的最后一个元素。这类似于堆栈的操作。因此,说列表可以作为堆栈使用是正确的。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 48,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[1, 1, 4, 8, 7, 'Python', 1, [5, 4, 2, 8], 5]"
- ]
- },
- "execution_count": 48,
- "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": 50,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 8, 7, 'Python', 1, [5, 4, 2, 8], 5]\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "[5, 4, 2, 8]"
- ]
- },
- "execution_count": 50,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "print(lst)\n",
- "lst.pop(-2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 51,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 8, 7, 'Python', 1, 5]\n"
- ]
- }
- ],
- "source": [
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**pop( )** 用于根据可分配给变量的元素的索引值来删除元素。还可以通过使用**remove()** 函数指定元素本身来删除元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 52,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 1, 8, 7, 1, 5]\n"
- ]
- }
- ],
- "source": [
- "lst.remove('Python')\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "可以替代 **remove** 但是使用索引值的函数是 **del**。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 56,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 7, 1, 5]\n",
- "[1, 1, 5]\n"
- ]
- }
- ],
- "source": [
- "print(lst)\n",
- "del(lst[1])\n",
- "print(lst)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "可以使用**reverse()** 函数反转列表中出现的所有元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 57,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[5, 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": 58,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 4, 8, 8, 10]\n"
- ]
- }
- ],
- "source": [
- "lst = [1, 4, 8, 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": 62,
- "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": 63,
- "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": 64,
- "metadata": {},
- "outputs": [],
- "source": [
- "lista= [2,1,4,3]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 65,
- "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": 66,
- "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": 67,
- "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": 68,
- "metadata": {},
- "outputs": [],
- "source": [
- "lista = [2,1,4,3]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 70,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2, 1, 4, 3]\n"
- ]
- }
- ],
- "source": [
- "listb = lista[:]\n",
- "print(listb)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 71,
- "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": 72,
- "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": 73,
- "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-73-9f8df9d106cc>\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[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[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[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": 75,
- "metadata": {},
- "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": 79,
- "metadata": {},
- "outputs": [],
- "source": [
- "(a,b,c)= ('alpha','beta','gamma')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 80,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "alpha beta gamma\n"
- ]
- }
- ],
- "source": [
- "print(a,b,c)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 81,
- "metadata": {},
- "outputs": [],
- "source": [
- "(c, b, a) = (a, b, c)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 82,
- "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": 83,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "3"
- ]
- },
- "execution_count": 83,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "d.count('a')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**index()** 函数返回指定元素的索引。如果元素大于1,则返回该指定元素的第一个元素的索引"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 84,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "1"
- ]
- },
- "execution_count": 84,
- "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": 85,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'set'>\n"
- ]
- }
- ],
- "source": [
- "set1 = set()\n",
- "print(type(set1))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 86,
- "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": 87,
- "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": 101,
- "metadata": {},
- "outputs": [],
- "source": [
- "set1 = set([1,2,3])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 102,
- "metadata": {},
- "outputs": [],
- "source": [
- "set2 = set([2,3,4,5])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**union( )** 函数返回一个并集合,该集合包含两个集合的所有元素,但是没有重复。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 90,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{1, 2, 3, 4, 5}"
- ]
- },
- "execution_count": 90,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "set1.union(set2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**add()** 将向集合中添加一个特定的元素。注意,新添加的元素的索引是任意的,可以放在末尾不需要的任何位置。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 94,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{0, 1, 2, 3}\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "{0, 1, 2, 3}"
- ]
- },
- "execution_count": 94,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "print(set1)\n",
- "set1.add(0)\n",
- "set1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**intersection( )** 函数输出一个交集合,该集合包含两个集合中的所有元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 95,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{2, 3}"
- ]
- },
- "execution_count": 95,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "set1.intersection(set2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**difference( )** 函数输出一个集合,其中包含在set1中而不在set2中的元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 96,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{0, 1, 2, 3}\n",
- "{2, 3, 4, 5}\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "{0, 1}"
- ]
- },
- "execution_count": 96,
- "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": 97,
- "metadata": {},
- "outputs": [],
- "source": [
- "set1=set([10, 9, 1, 2, 4])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 99,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{4, 9, 10}\n"
- ]
- }
- ],
- "source": [
- "set1.pop()\n",
- "print(set1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**remove( )** 函数从集合中删除指定的元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 103,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{1, 3}"
- ]
- },
- "execution_count": 103,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "set1.remove(2)\n",
- "set1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**clear( )** 用于清除所有元素并将其设置为空集。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 104,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "set()"
- ]
- },
- "execution_count": 104,
- "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.7.9"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
- }
|