14.python中的集合

时间:2021-08-09 17:41:19

  什么是集合?正如其字面的意思,一堆东西集中合并到一起。乍一听貌似和容器没什么差别,嗯,好吧,集合也算是一种容器。

  在学习这个容器有什么不同之前,先看看集合是如何创建的:

a = set()   #可变集合
b = frozenset() #不可变集合
print a
print b

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbkAAAA6CAIAAACiZbC9AAAHLElEQVR4nO2dy5XjKBSGCYsQJpEiDFUEzc6rmQy0LfaOQhtl4CSYhYTEU2BbluXq7zt9+pRtBBe4/FzQS1gAAKgh3m0AAMAHgFYCANS5XyvHq5TX8dlyByU6IXrzbD45zm/hMZhe7NAOu5R103K7MQdVb+qWNO+gtZ1396hdG+R13nKkH76SSCsnn/4W4luooXDITcuL9qpulDtEfKs7uu6hnh6vUnRhWZ7Nc5fEFj7K2QZnS+8EiZWZe0cZO+pL24EPGua6o9Ro241501/V4dSS5nj8ulfZ16NuWu6V2121OEnORxNo5agvUt+stdYOqjwVeMnsnHjygPEqN11h1Ffv1wf8xhstpndT9KCdzcZll1jYytMWvpDG3plZJ/OlFi+rjh84jFcpsmPjoMYMe/C1R1l7b9D0fo/K1zSsxZ5t+LqcDyfQSj8c8+xLOjhe5DZqZZTPA37jHxIfHjTog8vw5y18HbfW3pkTp4Hei6oTBg6mF6pXmej1mMZ8rJSHbbs3aHq7R1W9pZTmbDm/gUAr220yKmoCf108f1Rmiv6+heh/5j+mf+vQ1foixPcaA85L7EkU5v2dOM2UMIltdZjAt9CoToheqc7lc4SFAZmjLnoMN7CCNCn3eEwwmWe0ct02CePBuXTXJmb+snxUXNbU7OHacMpNXqTo/9UXIS5SdkL2X7KbW2wpbjU4adU4Tc6esAd/CrWY9yKWPZxMvze1Rlj3QYlOqV6Ib6X6cp8OKvDDxDNLZaW+kTZImmbZsJqmrmxNox6stkbWe4/POXaDyujeRWof1Eprem/nKx3G6yLR6HTQLh87V7fpexc6rVFhmsba7BLbXGOJiS3slAlzfqWFIUtIuBQ6qYm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3E2llqlyjlpNK+luLxJUA8HeRrsGnuHHRRT+M3Iwik7M0TVrJqXAA+ATiuFKvK233/HnpRHAc47jSmFXnErFs0UqkEgA+gkgrl0vGvSAyexm5jL6xlusrAeD3wn07AAB1uB8cAKAOWgkAUAetBACog1YCANRBKwEA6qCVAAB10EoAgDpoJQBAnc/VSq57B4DjyGll/g7Gc8H9lABwJKlWjlrOD8Y479PSeF8FABxL9pls55JI3lcBAG/n/FrJc4UB4P0EWuk9kU0IoX7mMybKzD84oYneKhEcl00zZaS0lvnHCPO+CgA4N9W40ihfy6xdn/7rPQV4+cFTyujNE0bN2Sz5874KAPgYWrSyJFXBT6Fypm+eyB7F+yoA4DN4QCszceXyp9On9M0TqVbyvgoA+BhCrfQ2HqUe/TgvXoWX4sF0D1OZNYEy7k/1w/sqAOBz4L4dAIA6n6uVAADHgVYCANRBKwEA6qCVAAB10EoAgDpoJQBAHbQSAKDO/5KqMaswWzU7AAAAAElFTkSuQmCC" alt="" />

  集合分为两种,一种是不可变的,一种是可变的,两者的差异后面会分析。

  不过,我们创建了两个空的集合貌似么什么意思。

  为了使其有意义,我们就先来看集合最重要的功能:去重

a = ('aaa',123,123,123)
b = set(a)
print b

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdgAAAAlCAIAAABuwG+9AAAGyElEQVR4nO2dy5XjKhCGCYsQJhEThhzBsPPqTgbaNntHoY0ycBK6CwmJogqEZfTo7v87c/q0Pah4Fb+KEnarAQAAwKmosxsAAAC/nU1C3D+1fvafVt0Z1SjVuk/tSFy/hcfgWlVhHKrU9bI6P5idWR/qkjJnUDrO1T2q6oDs5y1H+uH3hAvxuGDuSt2V6RJXvax+2GBcnfGXqLt5wy82uVH/1KqhdQVtnuY7buFWrrbyS2aHFDZumh3jht4+yi7c2DA/HalByw/my95W12pJmeMJ+75KXY96WV3L2lu9uIjln0MsxL19aPsahmEYOpO+iQXFhqnw6F79U2f9rLfP4H83OGWwFF3rg4vO+jY7b461sJSPW7gjhb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trDxJzwfiwq+RwmGTP3eAjUNMa1Sj912dRp59BYjR+xkBGNS6T6EU4g3Iv5px1e9NNcN6xkUYjzBFTO9I4O9O64eVcUFc8p521nSV+SD0z1Wb5+QGdeu4/3powifIMSmViywnvPcyyfHMtWd3X5cNP1vFNBEFKFxzM3MJUyHx6C+ty5JnN33w+tGLfV/fdadZOi67xY84Of/vVvdNHnLvlqeU1mB6vkwj6Wi0EvxPmmSXMqdUqn1r69vyA1Y3vmgAAgJOBEAMAwMlAiAEA4GQgxAAAcDIQYgAAOBkIMQAAnAyEGAAATqa6EPf2FnwPgVFKKaX0zqcdnVFVjgwyO73VYw+Wt0kHAQDgcyoLsTNEc509RrOiaqvb6a114Sv9jU6KAwAuT1UhZgrFhdiZoMQcb84Rpw+hSQy6XmsVGc7YoUJcT/gBAGD4RIhjcZISBESIJ5ENvjpt3uM7o5T5CvWtt7pOtqEKrK+1ciEAAPCBEAuqy8WJRcRhmdTvKWMnUnLTAQCAjQhCvKQHSHwavA4SCCojpitCvECTAs68k5hIwR+zlbyTNAYhBgDsBhdiLjGzToap0c8j4tj6WsG3mK9/65cUEGIAwI6IqYkx4tVhBrco/mWPsFaF2JnU0baPlW5ptbdT8o4ME2IcnAAA1EOIiO2SgPB/ZmWOWfs+joidC/42RKzEeSEO1ay3ljysWxR6myL7q5aHfiXvpIiFGDoMAKgIF2J+pizxuQYthJK5c8Tx0TSSaVY++SGE3tuOik1h/e2mqfH8OzI4RwwA2JGLf7Ku1hnhreCTdQCA/cF3TQAAwMlAiAEA4GQgxAAAcDIQYgAAOBkIMQAAnAyEGAAATuZ/mXQa5QSdb6YAAAAASUVORK5CYII=" alt="" />

  可以看到元祖a中的重复的元素123在集合中只剩下一个了。所以,我们总结集合的概念:

  集合就是将不同的元素放在一起组成一个集合对象。(不影响原来的对象)

  此时,有机智的同学看到集合里面有个方括号,立马想到了列表,并试图用索引来取值:

a = ('aaa',123,123,123)
b = set()
print b[0]

aaarticlea/png;base64,iVBORw0KGgoA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/LJ9ajnVTnW7zvPrKg4BAiFzPqSwPumRI6avj3GWP0nYsqG7gs65aixs7y31XLb1nhtpfnPqFeNqdfHJ/USDzu2/sGqX9SlXyWm3YWLAev2dp5ZpgQjw69nil8kLQ7RuU/DqwjjGEZkPKFr8MhsT7fMm8n1eBiQmWnQO/ndFbp3VmalrlYmXfe8+Xg6fXFX1cXKMuibjKl4dYg0K9SnFeDlf5Yv05U9RwWdS1YaNc6PlCla9Z96qNrxe6pG+Y9Pu8QFYqPrlTL2lqCyeiWpEzedFudH0PTlaR4qtLOXkhDlELrjYlm0g7+0xJUuqNatY2Wl+ma1rlapNe3fjDQbboM+U1lRq8M2qmWvWGrFQvqYXlVzXkOlrihkDnemMys6x2tyo6dNYvSxj1Z9F4WmNpPnlRslBMwC2fq/koQpBAWhjSVaChJZ7qe9gbQoI/ArW+mUzkA921IhaiYubSsncXw2dUV88x3x+GSlm4Pkl38MGAICPAX4ZAAD6An4ZAAD6An4ZAAD6An4ZAAD6An4ZAAD6An4ZAAD6An4ZAAD6An4ZAAD6An4ZAAD6An4ZAAD6An4ZAAD6An4ZAAD6An4ZAAD64v81/OUZDbQnmgAAAABJRU5ErkJggg==" alt="" />

  然而并不可以。

  因为集合是无序的,和字典一样。

  那么是否我们可以将集合看作是一种特殊的字典,只不过没有键,所以不能通过键去取值而已

  某种意义上是可以的,不过要注意,集合分为可变集合和不可变集合两种,不可变的集合就不能像字典一样修改元素。

  另外补充一点:

a = 'abcdaabbccdd'
b = set(a)
print b

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbgAAAArCAIAAACFLQq9AAAHBElEQVR4nO2czZXrKBCFCYsQJhEThhzBY+fVTAbaNntHoY0ycBKaBUKioPgxQmp3v/udOX3G/VBRFHBVlOQWCwAAgCziux0AAIBPp1Uo56eUz/lo75MSgxCjOWqH4/M9vAYzig5x6NLXS8t8MCdVDnVNm++gNs7dV1TXgJy3Wq5chyfACqVd0Hch7kJNiQtfWj60N26j3CXirt6Yt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AoAKEEAIACEEoAACgAoQQAgAIQSgAAKAChBACAAv8DtRApwsgNdmQAAAAASUVORK5CYII=" alt="" />

  因为字符串也是序列,所以也可以用来去重,但是一般没什么用就是。

  那么,对字典去重会怎么样?我们在字典中了解到了字典会自动处理键名重复的问题,那么值能进行去重吗?

a = {'a':123,'b':123}
b = set(a)
print b

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAboAAAAqCAIAAABKhAklAAAGrklEQVR4nO2cwZXrKBBFCYsQJhEThhzBZ6fNzGSgbbN3FNqQgZPQLCQkCgooy5La3fPu+afPtxtBAcWjKEmt/gYAACBATQAAAARALgEAQATkEgAARLwnl/6h9cO/a8NoVKfU4N6th+PzLbwGN6gDxuGQtp5W1wdzNO2hlpT5DqTjfLhHHTog53nLlX54AhW5nN36rtRdmbFcprdR750Jl6i7eWH2dk22f2jV0bYim5dZSS3cy6etT8nskMLGLbNj3ORtL7twp2FhOkqDVh/Mp701V5SkzPXEfW9yrEc9rT6qtpd68SE1X0RRLr3ttX1O0zRNoylvCFGxaSk8O4F/6Ko3ePuIfrvDdaIF44awUY822OxCdZmFUt628ESEs7OwbelrL07rThw++IdW7PK4aDDpDJ571TS9Gjp9v0fxPaW9OHIMz6v5KopyGQdlkYnZHKenXaFcJvXscJ34kvRyMqY7z+PvW3geT+nsLIXzcO+k7tDwwQ3KDIaJYa8ZzH2t7Lbt1dDp2z2q6S2lMp9W83UU5VJuljPJKMQH5OWjcXMMeFdq+Fr+M//bVq+1vVL3LRJcztqzLiy5nrTMXDCLcC0tEFvoTKfUYEwX6rnCQgJzVW89TWaRMjmvOA3Z0hm53PInNCpcWg9j4pYvy1elbc3DTg+Jc22612r4x/ZK9Vp3Sg833S0jtja3GZyNalqGs4fO4FehF0tSYk3mMPMuGg3a99GozphBqbsxQ3lOR0P8MPPMUlu5b+QDkpdZM1fz7sX2NJnB5miw3nt9zakbNFb3O2p7gFxOboiyYPlK3k6Lzubrdv3Yhe7N34cAaosN8zLTxJ613SN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Yy4zP1IuoZcAgIiaXIb7M+EOzbTeFQmftxJxinCPXDK3W47BGWWMIZ0omgC5BACUqchlLhZr4i9O6x0SXa6XHK5QzswS3E6HQi4BADXqh3FHw7I4mKzGktlNkvZhfKv7cLkUCnEml7g5DgCIqEWXdjtyhz9cv0Zo3qfRpXObtGR6KY0uvdVFUdv9JKa2fk90CbUEAMRU5HJNJ0YBH/tEt2aCwupzlxxLIHu76VKWceeTmNa62cKW9uG5SwBAjW95q2dXbXuexBTXjbd6AAAt8M44AACIgFwCAIAIyCUAAIiAXAIAgAjIJQAAiIBcAgCACMglAACIgFwCAIAIyCUAAIj4D4dMdcb/8RhWAAAAAElFTkSuQmCC" alt="" />

  看来还是对键的去重,并没有什么用,除非要将所以的键组合成一个集合,其去重意义不大。看业务需要吧。

  这里,我对去重的总结是:去除重复的引用,相信看过我在python序列中的说明的人能明白这是什么意思。

  知道去重的本质之后,我们有这个需求,我有两个容器,我想得到这两个容器去重后的结果。

  那我这样写:

a = ('scolia',123,456,666)
b = ('scolia',666)
c = set(a,b)
print c

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdEAAABbCAIAAACj/SjGAAAM8UlEQVR4nO2dy7HruBGGmc04BCQkZGHdCIydVhMC11i4ygvtHMGoXMMIRknAC+LdjQcpEtLR/F+dOnUuL9kAmsTPZlNCTwYAAMAopnd34CiW+0U9392Jvzc4BQC0GaC5ep6m6yQfGw97yA1HPaS4L5t79mU8lfiVuppu6TlqP4u6Sf26GQC+mKC5Dzn9muzP9eCZo+c9U7r7KEz1AHVajxv3nSAG3PwAqBM09660n3sPdexD4rma+5DTfITkPpU6RHfe2vpQzWX6rOXRN2wAvoooztUmzD2tD1WfUzX3qBjtsFjvra2P1Fz2qGzje70KwMeR5XOjGaKlTfMt6hbl+9b0368pCi0XdcsyEvYQ/5ip50nOcvo1TTdlN3XYcUF3I7Eb+vxU4jqJm5iu4jKL0BbpT0ikXIV6usHaLWxbBW9Qyy1v2Oz2XYngjWbrBc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/wxx/1USTX4aq2Ff/sCzBpx6gPe41DcP9u1DXXXRBauhw/P2fS+3lkxOYoekrzVmbaVnZW9KFGuKPitXc3eoMZbKmtPicwNYd6nmezm2JJu9O4+6jcQt5n5hAfC9/yQiFec9lR9PiQvfCykbap97A/Y/CQL67rD34eDc0lH1eiNWxovZzo5n8pxU3laLQRbTV5oaIP259wlItzxSZvsJWKstbZo2yOpV3DLYmF6+Ni1YrxPPFhj8dMxymjfd5yP9byWtTcynVYGQUd6b5bSzXOLXgsrvz03je6YCSx5roiN9njfF5bTNxoDZsoX5l/VixKbNG0WnKTJ/lcrsBPT05gf0Wf1FJ8lH8EDvncHm/E440GQlovHHWVcq50Mq059GQtR6zR2Syl/227lOdzOR82PRadstonn6I+//M3wZ9lYmrt+eqNf4p/VEYRn6/4CrwGaY5HQUdK6zb1DZZm5Bse03H16+05NPBTaX8nIv3E5e7n/a8E3jiDbY/nh9JZt+mNPQQ/norm6pnELGsMi3vyCrxxEj5bSp5yPoXP7yH4WPDdXwAAGAc0FwAAxgHNBQCAcUBzAQBgHNBcAAAYBzQXAADGAc0FAIBxQHMBAGAc0FwAABgHNBcAAMYBzQUAgHFAcwEAYBzQXAAAGAc0FwAAxgHNBQCAcUBzAQBgHNBcAAAYBzQXAADGAc0FAIBxQHMBAGAc0FwAABjH/wGpM45BAYW85gAAAABJRU5ErkJggg==" alt="" />

  结果是大红字甩脸,说只要一个参数,结果给了两个。

  好吧,首先看看其内部是怎么接受参数的:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAANIAAABECAIAAAB7xPxLAAAEkklEQVR4nO2dTXKjMBCF+0reaTfbOcJ4H13DN9AdJmcYLjC+xJils3Clio0X2aRKCrMAgaDFnyE0JI/6FokiaKl5fm7iqja9vr66jxcA1oQgO7A+kB0QALIDAkB2QADIDggA2QEBIDsgwL5lZ93ZPJ0u7vrlg34xRsnuYg5ERHRMNpbrRB9MGlmSdZmmnOiNL9i6TMfGJ2HTk9LP4tvfL2Pdz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m27mPF5ue13W7ibLza568gEbfd951flQGBl1zZCoGo/N3pDH7KuA95tfvOt9b2+mjJt92vfm9PPWvQe/2h2kVZ/4fEPw4/mEz/a2qu8I/vJ7KGKI95lsZ4N3reU7Cd+QpshuOXictqL8H9+W2YXWdsvueLPsv+9CiPnPNY77qaD+fUiyRDn78+BnzsMWLWdnoq1H74t6+JANuBwSA7IAAkB0QALIDAkB2QADIDggA2QEBIDsgAGQHBIDsgAD/AXPLctcDVoNvAAAAAElFTkSuQmCC" alt="" />

  首先,这里的__init__是构造函数,在类的创建时会讲。现在只要知道我们的参数是传给 seq=() 的,而按照这里的写法,应该是可以传一个元祖的。

  那么是不是就可以这样写:

a = ('scolia',123,456,666)
b = ('scolia',666)
c = set((a,b))
print c

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdoAAAAhCAIAAADxpP2WAAAH4UlEQVR4nO1czZXrKgymLEp4jYQynAoeu6zu68DbsE8VXjw6SBPchY1BSGDsYI890XfumTPxxULo57OQyQjXEPYl5ct+KmVQohOiNy00SnF+DY+B6UUDOzSZ661l2ZiDWjZ1zZifQK2dm0dUU4PsFy1HxuEVIKpHjmlzF+Iu1JAf89CRdY3yt4i7WhEdm4LJvqTo4FyRzpPXUw234mz5X+MdMFiZyTvKOKsfdTduVMy7I2e0sjHf+raYsTVjjke89kW0jai3lq2krVrFSSRfFbV0bPVD6rdzzrlB5R9o0TA3DR6DzL5kMdqsfkX/uyE0o4Q0vS80Bu11Nl4c0rAWH2u4Iyq9MyGUJPMqdltOXP7YlxRk+h1kTOjBfe9ybm3p9/MRRa8UrqKlDfeTfFnU0nFcVEYmQDGUdgMq6TiRsyE041vS24HPNvYrPtdwP7xrvTMNxuXqTsuB5Y/pheoVUYMfY8xts2zWbW3p9+MRtRgtuTFnk3xh1NJx/bKNSqwcNxCmj8qMNexdiP45/TL+C+yg9UOIe6hkp17EyDtTry0dMw5EFbqGA2INjeqE6JXqvJwjNAQg7npoC5uJYAzGmqAEJQlBx6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,但结果并不是我们想象的那样。

  看来集合的去重是针对一个对象的,那么我们就可以将集合总结成:

  集合是去除一个对象(序列或类序列的字典)中,元素的重复引用的。

  所以我们想要实现这个需要,首先要将两个对象拼接起来:

a = ('scolia',123,456,666)
b = ('scolia',666)
c = set(a+b)
print c

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAa0AAAAtCAIAAACS86MMAAAHq0lEQVR4nO2by5HDKBCGCYsQNhEThhzBcvNldzPQdbg7Ch1WtQk4Ce1BD2i6eViWZI/1fzU1NdZAAw38NAir/wAA4Nyod1cAAADeDHQQAHB21F8AAHBu1AAAAOcGOggAODvQQQDA2XlZB/u71vf+VSudUY1SrXvVjsTn1/AYXKs28MMmZT2szjuzM2VX16R5B7V+3nxEbeqQ/UbLkeOwmrwOjuP1qtRVmS6d5maDZjkzZ1FX80S3rOrF/q5VQ8sK6jy5O67hWj5t4tX0Dkls3NQ7xg29vdVlXFmxuTtSTss782EvxalSk+Z4wrYX2XZEPazeytpTrfgQyy+R08He3rR9DMMwDJ1JS3iQbJgSj73b33W2m3t7D/67YkwEM8G189La2bnObjbHaljLyzXckcremfCL8NKK3ZoTLvj9XStx3B/kTNqD++YahmeDnfePKLmltBVb+nA/y6+R08EwjArKZp0XbzwrdTCys2JMhFni7MRZK7fGr9dwPx61vTMl5gHaTs2hC75rlWmNEHUe48x1payu27PBzttHVHG0pNJ8muVXyelgfXnORM0L96rTR+PGqO2qVPsz/TH++Glp7U2pq4/dpm3vOOGn85Q4zZiQxaSWJghr6EyjVGtMM9s5ooYEIdfN9vTAiKThPDMayCIs6KA/yqBx3FT67BM3PUznissa3U73a6M1fdOq/dvelLpp3SjdXnQzeWwpzleYeTVOI9WH9uBPohXT+cByriL0e5U3aNs7oxpjWqWuxrTpPu0MGYdsZKbK4mODO4SnWQ6RxmVJbGnUg0VviKP3eMvxMCjMbp5xGx0cXBucNPEp6jduzvIJuXxs5nqPz+eQx0dzPM0wiNted4/lI65hYxy1vGcNKUsotxQ6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xgXqscxVZ5QyP+Gk661+UgmdWYz3zrG568wsiM9sSHmumidHs2G5rFffu30HYDc20UFB9PiMYfFgmCb1d8pYsTrGJHVwXfzFc9U8OZ49dRBCCL6Ugg76vSmJzoLPwe5VZbSsoIMeqiVBYFfLGEguxp29XDRpgzPK2Kd3rzxXzZP1pD3PN+GaRtpByXHcLZtJAB0EZyGvg9Jhk57VxM++1+PB2HopYabGk/wteXy5ziht/x2cWZTjiQAuzsXtrLWcKo573qvtfPSpbR+VVewLv0KU3AodBGehuC8eIwodnt5VRX/sTL2og/5sTahD5fQj0SkLeZxR5oe8wam3HOfidtZazrcl9HxK4LKnCuI5Q9X7J6aDeGUMvpRCPGj97pfGIMMw9H0cgzjnZwkTwrwOhlOst5a8J1nz8iHQCGbH2/OtKVvmuWqerBVE0fPLX3aDeLD89inWQcgg+FbyOsgvsyQu2UoX13L3B+M7MTSOW87xeOBZs9n0tZ7FKXnSFmhk2TLPVfNk3W0TyfP8Jjo9HwyvCHkR5rHfFGheLjp/qx33B8FZ+EXfJ9nvbexvtLwn+D4JOBn4fjEA4OxABwEAZwc6CAA4O9BBAMDZgQ4CAM4OdBAAcHaggwCAswMdBACcHeggAODs/A8SBf3ECYv8BgAAAABJRU5ErkJggg==" alt="" />

  关于序列的拼接不只一种,详情看我之前的博文。

  而不同类型的序列先要强制转换成一种,否则报错

a = ('scolia',123,456,666)
b = ['scolia',666]
c = set(a+tuple(b))
print c

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbMAAAAwCAIAAAAU3AMSAAAHyklEQVR4nO2bPbbzJhCGWRZLyEbMMuQidehcJTtQe+l90qZXEe3AmyCFhICZASFdSfaX+z5H5x5bF4b/V8MIq7//GXHhwoULV3qpt9cAFy5cuD7tUt3vFhcuXLhwpZfyAAAAcqCMAABAgTICAABllzKOT62f43eLHozqlOrdd+1IfH4Nr8H16oB+OKSsl9X1zhzMele3pHkHrf18+Iw6tEPOmy1XzsOD4Mo4zeC7UndlhkKul9UPm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we15rJKrB7ER7sTXrlnmuljt7JVLs+eWTPcBnXH+vRZURwgh+JlwZ+VGawjFg6SBd7TwjPZGT+3pLPJA7py1b1FjrIFfFiF2imuuWea6WO/vOukg9z8/K53HG9IBSlGXuH87O6O2m6+fucZ4RAO8//jcw5735/RUtnwl+AwNAAn43DQAAFCgjAABQoIwAAECBMgIAAAXKCAAAFCgjAABQoIwAAECBMgIAAAXKCAAAFCgjAABQoIwAAECBMgIAAAXKCAAAlP8A9jf1ok7ZDPQAAAAASUVORK5CYII=" alt="" />

  下面我们来开始学习其内置方法吧。


  老办法,先看看帮助文档。

Help on class set in module __builtin__:

class set(object)
| set() -> new empty set object
| set(iterable) -> new set object
|
| Build an unordered collection of unique elements.
|
| Methods defined here:
|
| __and__(...)
| x.__and__(y) <==> x&y
|
| __cmp__(...)
| x.__cmp__(y) <==> cmp(x,y)
|
| __contains__(...)
| x.__contains__(y) <==> y in x.
|
| __eq__(...)
| x.__eq__(y) <==> x==y
|
| __ge__(...)
| x.__ge__(y) <==> x>=y
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __gt__(...)
| x.__gt__(y) <==> x>y
|
| __iand__(...)
| x.__iand__(y) <==> x&=y
|
| __init__(...)
| x.__init__(...) initializes x; see help(type(x)) for signature
|
| __ior__(...)
| x.__ior__(y) <==> x|=y
|
| __isub__(...)
| x.__isub__(y) <==> x-=y
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| __ixor__(...)
| x.__ixor__(y) <==> x^=y
|
| __le__(...)
| x.__le__(y) <==> x<=y
|
| __len__(...)
| x.__len__() <==> len(x)
|
| __lt__(...)
| x.__lt__(y) <==> x<y
|
| __ne__(...)
| x.__ne__(y) <==> x!=y
|
| __or__(...)
| x.__or__(y) <==> x|y
|
| __rand__(...)
| x.__rand__(y) <==> y&x
|
| __reduce__(...)
| Return state information for pickling.
|
| __repr__(...)
| x.__repr__() <==> repr(x)
|
| __ror__(...)
| x.__ror__(y) <==> y|x
|
| __rsub__(...)
| x.__rsub__(y) <==> y-x
|
| __rxor__(...)
| x.__rxor__(y) <==> y^x
|
| __sizeof__(...)
| S.__sizeof__() -> size of S in memory, in bytes
|
| __sub__(...)
| x.__sub__(y) <==> x-y
|
| __xor__(...)
| x.__xor__(y) <==> x^y
|
| add(...)
| Add an element to a set.
|
| This has no effect if the element is already present.
|
| clear(...)
| Remove all elements from this set.
|
| copy(...)
| Return a shallow copy of a set.
|
| difference(...)
| Return the difference of two or more sets as a new set.
|
| (i.e. all elements that are in this set but not the others.)
|
| difference_update(...)
| Remove all elements of another set from this set.
|
| discard(...)
| Remove an element from a set if it is a member.
|
| If the element is not a member, do nothing.
|
| intersection(...)
| Return the intersection of two or more sets as a new set.
|
| (i.e. elements that are common to all of the sets.)
|
| intersection_update(...)
| Update a set with the intersection of itself and another.
|
| isdisjoint(...)
| Return True if two sets have a null intersection.
|
| issubset(...)
| Report whether another set contains this set.
|
| issuperset(...)
| Report whether this set contains another set.
|
| pop(...)
| Remove and return an arbitrary set element.
| Raises KeyError if the set is empty.
|
| remove(...)
| Remove an element from a set; it must be a member.
|
| If the element is not a member, raise a KeyError.
|
| symmetric_difference(...)
| Return the symmetric difference of two sets as a new set.
|
| (i.e. all elements that are in exactly one of the sets.)
|
| symmetric_difference_update(...)
| Update a set with the symmetric difference of itself and another.
|
| union(...)
| Return the union of sets as a new set.
|
| (i.e. all elements that are in either set.)
|
| update(...)
| Update a set with the union of itself and others.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T

set

  可分为以下几类:

  1.运算符相关

  3.内置函数相关(省略)

  4.普通内置方法


1.运算符相关

  不知道你是否还记得数学中的集合,就是那个交集、补集什么的,忘记了请自己重修。

下面用一个表格总结:

数学符号 python符号 说明
 ∩  & 交集,如a&b
 ∪  | 并集,如a|b
 - 或 \  - 差补或相对补集
 △  ^ 对称差分
 ⊂  < 真子集
 ⊆  <= 子集
 ⊃  > 真超集
 ⊇  >= 超集
 =  == 等于,两个集合相等
 ≠  != 不等于
 ∈  in 属于,是里面的元素
 ∉  not in 不属于

  这部分内容只能呼叫数学老师了。我就不讲了。


2.普通内置方法

  虽说在帮助文档中列出了一堆方法,但集合是分为可变的和不可变的,所以有些方法,如修改操作只有可变的集合可以使用,所以内置方法又可以分为两种:

1.所有集合都能使用的:

方法 说明
a.issubset(b) 如果a是b的子集,则返回True,否则返回False
a.issuperset(b) 如果a是b的超集,则返回True,否则返回False
a.intersection(b) 返回a和b的交集
a.union(b) 返回a和b的并集
a.difference(b) 返回一个新集合,该集合是集合a去除和b元素部分后的
a.symmetric_difference(b) 返回一个新集合,该集合是a或b的成员,但不是a和b共有的成员
a.copy() 返回一个浅拷贝。

2.可修改集合才能用的:

方法 说明
a.add() 在集合里添加新的对象
a.clear() 清空集合里的所有对象
a.pop() 删除a中的任意元素,并返回,为空时报错
a.remove(obj) 在a中删除obj这个元素,没找到时报错
a.discard(obj) 在a中删除obj这个元素,没找到时什么都不做,返回None
a.update(b) 用b中的元素修改a,此时a包含a或b的成员。相当与合并
a.intersection_update(b) 用a和b的交集更新a集合
a.difference_update(b) 从a中移除和b一样的元素
a.symmetric_difference(b) 将a修改成a和b的对称差分

  这里交并补、子集超集之类的基本数学概念就不再重复了。这里讲下差补和对称差分:

1.差补

a =set( 'scolia')
b = set('good')
print a - b
print a.difference(b)

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZ0AAAA5CAIAAABiR2G5AAAHbUlEQVR4nO2czZXzqBKGCYsQJhEThjqCYefVnQy0bfaOQhtl4CR0FwLET4EQjWQbv8+Z02fsD4qigFJRwrAFAAD6gh0oOz84f8x/bXESbGBsVH+VQ/H+Gl6DGlkDOzRp6yl53piT2Dd1SZlXUGrn5jOqqUHOmy1XzkMf69fW+ffD2A8TU6LwU/K7dNRUwlRhP+KAmatGZX5wNvhtOTpr84Ua1vJuC6lkdLzCQunREWqZ5b2s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AHwLuAS8E94AD8DHgHvDXirXCcQ84AM3APeCF4B5wAD4G3AN+WDjuAQfgzcE94N8sGb83AH2C34cCAHoDfg0A0BvwawCA3oBfAwD0BvwaAKA34NcAAL0BvwYA6A34NQBAb/wfRGYmOxKt9vIAAAAASUVORK5CYII=" alt="" />

  实际上就是干掉了 a 中的 'o',而之所以干掉 'o' 是因为两个集合里面都有 'o'。

  本想用数学公式来解释一番的,然而我的数学也已经遗忘到太平洋里去了。

2.对称差分

a =set( 'scolia')
b = set('good')
print a ^ b
print a.symmetric_difference(b)

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbMAAABFCAIAAABPM59gAAAIMUlEQVR4nO2dO5brLBZGGZaG0BMphqEKOm4yR90zUFrkHoUSzcCTUAd6wQEkjJHsqn/v5VXr2hcOr8OnA5KNav9tePHixYuX+1IjAAD4oIwAABKUEQBAUqSMw71p7sOrRfdatUp19lU7MT6/htdgO1WhH6qU9TDNfmf2+rirc9K8g9x+ru5RVTvkPG+50g8rESrj5MHfSn0r3SdyPUxzM05DrV6yqG/9xEAVjetwb1Trl+XUeR4AWcNSPm0q5oyOl1jbeXS0HQdzy8tYWLFlOFKdtt+ZD/N1OHly0lyP2/ZD6nrUwzS1rD3Vig+xfCJSGQdza8xjHMdx7HVa5p1k45x4Gu/h3uwO/GDuzv8WeIkzN2y3XH57s9TZLuaCGubycg1PJHN0ZrYL9dqK05rjBgXDvVHRmXBRZ/ojeG6ucXw2IHq/R8Vb6reiZh+eZ/lMpDK6oZZTm2A45XI1UxmFnQIvcbPI7F73FS6oX6/heTxyR2dOHAZxJzXHDwpsp3SnI5HpNZ1ZVkpx3Z4NiN7uUYfekkrzaZbPRSpjfg2sFg12V7jzW22nyO5bqe5n/sf02iaqMTelvrf4bl4sTxIw78vINFPCIG41fgK3hla3SnVat4udK2roEcl1M4O/8eSlCXnGP7wLdUQZtw0QP9abS1/6xM4fpnPJsqZu91d5k7Xm1qjuv+am1K1pWtV0X00799ha3FbhoF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mg7my/lOrVZKKaUaM0zvDh8wyklTlgvLf87yYJrJv7aPPfcDKKayMlrdeL/1bfxf/m4OvTYnTVmuupanSTnNyF9RZyErdSwv0lTf8jNpBmOs+67h8X14marKGDilo4yDaQ59PydNWa4zLFut9M+vqbMbTNW17GruW3rDV8ZSbQZwKVdG6Y+xFZEfM/4xypaNf4+390PgiW+vEfx+ipUxIoOhP6KM/wDe3g8512iA54go43LbZLlxMq77Scv7LYW7x1SijJE99FqcZ3pqftM01U3HO7aW2ZPE4iwZivhhHJQR6hMqY+hV6xaPu4FTJWZcs1R35cHMunWG5cYMTgnVOK83TrJ5quV8sygj1Ce6mp6DokXV3EhmN0IMdr6PV9Ob7ZNUpjon6tca5p5wB+HXKeMY+GGKQBm5PQ0vE4kZzbZmXn4pfb0tOAwyZrR288FAGnNjxr34q2zibRbDiOJly40Zpmlbt87nPS2Yk/HTLId+mEKOMMIIrxMq4xq4OBM/+kht+CjbwfOMMeaw4OurSQUHpc9g+E+Zx1O8YllrnYx0X61zOoQusnxsttCy4yvV+znmh6mUPM8ItbnyOzBF1kqej/ullt3gKBoofWCd32iZ78DAifC96Q8iLwQDgNNBGQEAJCgjAIAEZQQAkKCMAAASlBEAQIIyAgBIUEYAAAmnHWD511rmSW84DU47KMvFaQfjyGkH8HfhtIPiXJx2MMFpB/AH4bSDYvgRwIm39wOnHUB9OO2gGObfxNv7gV+uhfpw2sHzcNqBtM5pB/DX4LSDAsucdnCBZU47gHfCaQfFljnt4ALLOZ3BaQdQH047KLDMaQcXWOa0A3gnnHZQapnTDjjtAP4unHbwOZY57eB523wHBs6B701/EJx2APAhoIwAABKUEQBAgjICAEhQRgAACcoIACBBGQEAJCgjAIAEZQQAkKCMAAASlBEAQIIyAgBIUEYAAAnKCAAgQRkBACQoIwCABGUEAJD8HzJMC53YwYImAAAAAElFTkSuQmCC" alt="" />

  将干掉两种的交集,然后把剩下来的拿出去组成新集合。

  


好了,集合就先讲到这里了,这么多东西我也不一定全部记得住,要用的时候再来查就行了。

参考资料:戳这里

14.python中的集合的更多相关文章

  1. python 中的集合set

    python中,集合(set)是一个无序排列,可哈希, 支持集合关系测试,不支持索引和切片操作,没有特定语法格式, 只能通过工厂函数创建.集合里不会出现两个相同的元素, 所以集合常用来对字符串或元组或 ...

  2. Python中的集合类型分类和集合类型操作符解析

    集合类型    数学上,把set称作由不同的元素组成的集合,集合(set)的成员通常被称作集合元素(set elements).    Python把这个概念引入到它的集合类型对象里.集合对象是一组无 ...

  3. python学习之【第七篇】:Python中的集合及其所具有的方法

    1.前言 python中的集合set与列表类似,它们最大的区别是集合内不允许出现重复元素,如果在定义时包含重复元素,会自动去重. 集合是无序的,集合中的元素必须是不可变类型.集合可以作为字典的key. ...

  4. python中的集合

    在python中,普通集合是可变数据类型 通过以下案例说明: >>> s = {1, 2, 3, 4} >>> id(s) 2108634636808 >&g ...

  5. 8、python中的集合

    集合是python中无序.可变的数据结构.集合与字典类似,集合中的元素必须是可哈希的(等同于字典中的键),也就是说集合中的元素是唯一.不可变的数据类型.这里前面说集合可变,后面又说集合中的元素不可变是 ...

  6. python中的集合、元组和布尔

    #元组,元组跟列表一样,只不过列表可读可写,而元组一般用来只读,不修改#python中不允许修改元组的数据,也包括不能删除其中的元素. t1 = ('a','b','c','d','s','a') & ...

  7. Python 中的集合 --set

    前言 在Python中,我们用[]来表示列表list,用()来表示元组tuple,那{}呢?{}不光可用来定义字典dict,还可以用来表示集合set. 集合 set 集合(set)是一个无序的不重复元 ...

  8. 2018&period;8&period;14 python中的内置函数&lpar;68个&rpar;

    主要内容: python中68个内置函数的功能及使用方法

  9. python中set集合

    一.set集合的特性 访问速度快 天生解决重复问题 二.set变量申明 s1 = set() s2 = set([1,2,3]) 备注:第二种方式在set类中直接传入一个序列. 三.set类中方法大全 ...

随机推荐

  1. 基于Three&period;js的360X180度全景图预览插件

    基于Three.js的360X180度全景图预览插件 时间 2015-08-12 10:01:10  HTML5中国 原文  http://www.html5cn.org/article-8621-1 ...

  2. Android 加入一个动作按钮

    在XML中声明一个动作按钮 所有的动作按钮和其他的可以利用的items都定义在menu资源文件夹中的XML文件中.为了增加一个动作按钮到工具栏,需要在工程 /res/menu/ 目录下面创建一个新的X ...

  3. C&num;多线程之二:ManualResetEvent和AutoResetEvent

    初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和Ma ...

  4. BITED数学建模七日谈之三:怎样进行论文阅读

    前两天,我和大家谈了如何阅读教材和备战数模比赛应该积累的内容,本文进入到数学建模七日谈第三天:怎样进行论文阅读. 大家也许看过大量的数学模型的书籍,学过很多相关的课程,但是若没有真刀真枪地看过论文,进 ...

  5. posix thread条件变量

    概述 等待条件变量总是返回锁住的互斥量. 条件变量的作用是发送信号,而不是互斥. 与条件变量相关的共享数据是“谓词”,如队列满或队列空条件. 一个条件变量应该与一个谓词相关.如果一个条件变量与多个谓词 ...

  6. Opencv各个版本的万能头文件

    每次下载opencv的新版本时,都需要重新写头文件,更改链接库配置,很麻烦有木有?下面这个头文件是我在别人的代码中淘出来的,很不错,与大家分享~(具体作者忘记了,不好意思啊) 作者很巧妙地利用Open ...

  7. SQL生成一年每一天的时间列表的几种方法

    工作好几年了,一直没有写博客,准备捡起来...   以下脚本适用环境:SQL SERVER (starting with 2012)   1.构建序列:   /*1-1:利用交叉连接,推荐下列这种写法 ...

  8. 文件操作命令&lpar;move&rpar;

    move命令: // 描述: 将一个或多个文件从一个目录移动到另一个目录. // 语法: move [{/y | /-y}] [<Source>] [<Target>] // ...

  9. ubuntu16&period;04降级内核版本至3&period;13&period;0-85

    1.前言 采用ubuntu16.04在编译实验课程的实例代码simplefs 时,发现编译时报如下错误: 因此需要将ubuntu16.04的内核版本降级为 3.13.0-85-generic 2.修改 ...

  10. Linux下搜索文件

    使用linux系统难免会忘记文件所在的位置,可以使用以下命令对系统中的文件进行搜索.搜索文件的命令为"find":"locate":"whereis& ...