Ungaziguqula njani iiListi kwiPython

NguJohn Paul Mueller

Ungawuguqula umxholo woluhlu njengoko kufuneka ngePython. Ukulungisa uluhlu kuthetha ukutshintsha ungeno oluthile, ukongeza ungeno olutsha, okanye ususe ungeno esele lukhona. Ukwenza le misebenzi, ngamanye amaxesha kufuneka ufunde ungeniso. Umxholo wokulungiswa ufumaneka kwisicatshulwa seCRUD, esimele ukudala, ukufunda, ukuhlaziya kunye nokucima. Nalu uluhlu lwemisebenzi enxulumene neCRUD:



fakela ()Yongeza ungeno olutsha esiphelweni soluhlu.

cacisa ()Isusa lonke ungeniso kuluhlu.

ikopi ()Yenza ikopi yoluhlu lwangoku kwaye uyibeke kuluhlu olutsha.



yandisa (): Yongeza izinto ezikuluhlu esele zikhona nakuluhlu lwangoku.

faka (): Yongeza ungeno olutsha kwindawo echaziweyo kuluhlu.

pop ()Isusa ungeniso esiphelweni soluhlu.



susa ()Isusa ungeniso kwindawo ekhankanyiweyo kuluhlu.

Amanyathelo alandelayo abonisa indlela yokwenza imisebenzi yokuguqula kunye noluhlu.

1Vula iwindow yePython Shell.

Uyabona iPython ekhawulezileyo.

MbiniChwetheza Uluhlu1 = [] kwaye ucinezele u-Enter.

IPython yenza uluhlu olunamagamaUluhlu1yeyakho.

Qaphela ukuba iibrakethi zesikwere azinanto.Uluhlu1ayiqulathanga naliphi na ungeniso. Unokwenza uluhlu olungenanto onokuthi ulugcwalise ngolwazi kamva. Ngapha koko, oku ngokuchanekileyo luluhlu oluninzi oluqala kuba uhlala ungazi ukuba loluphi ulwazi abaza kuba nalo de umsebenzisi asebenzisane noluhlu.

3Chwetheza len (Uluhlu1) kwaye ucofe u-Enter.

Inkqubo ye-len ()Iziphumo zomsebenzi 0. Xa usenza isicelo, ungakhangela uluhlu olungenanto usebenzisa ilen ()umsebenzi. Ukuba uluhlu alunanto, ngekhe wenze imisebenzi enjengokususa izinto kuyo kuba akukho nto inokuyisusa.


Ukuchwetheza uLen (uluhlu1) kubasebenzisi bePython banokujonga ukuba ngaba banoluhlu olungenanto kwisicelo sabo.

4Chwetheza Uluhlu1.append (1) kwaye cinezela u-Enter.

Jonga uluhlu olungenanto njengoko kufuneka kwisicelo sakho.

5Chwetheza len (Uluhlu1) kwaye ucofe u-Enter.

Inkqubo ye-len ()Umsebenzi ngoku uxela ubude be-1.


Xa abasebenzisi bechwetheza uludwe1 [0] kwiPython baya kubona ixabiso eligcinwe kwicandelo 0 loluhlu.

6Chwetheza Uluhlu1 [0] kwaye ucinezele u-Enter.

Uyalibona ixabiso eligcinwe kwinto 0 yoluhlu1.

7Chwetheza Uluhlu1.insert (0, 2) kwaye ucofe u-Enter.

Inkqubo ye-faka ()Umsebenzi ufuna iimpikiswano ezimbini. Impikiswano yokuqala sisalathiso sokufakwa, esiyi-elementi engu-0 kule meko. Impikiswano yesibini yinto ofuna ukuyifaka ngelo xesha, eyi-2 kule meko.


I-Python inokongeza izinto kuluhlu xa uchwetheza uluhlu1.insert (0,2) kwaye ucinezela ukungena.

8Chwetheza Uluhlu1 kwaye ucofe u-Enter.

IPython yongeze enye into kuLuhlu1. Nangona kunjalo, usebenzisafaka ()umsebenzi ukuvumela ukuba ungeze into entsha ngaphambi kwento yokuqala.

9Uhlobo List2 = Uluhlu1.copy () kwaye cinezela u-Enter.

Uluhlu olutsha,Uluhlu2Ikopi echanekileyo ye-Uluhlu1. Ukukopa kuhlala kusetyenziswa ukwenza ingxelo yethutyana yoluhlu esele lukhona ukuze umsebenzisi enze uhlengahlengiso lwethutyana kulo kunokuba kuluhlu lwantlandlolo. Xa umsebenzisi egqibile, usetyenziso lunokucima uluhlu lwexeshana okanye ulukhuphele kuluhlu lwantlandlolo.

Ipilisi yeorenji engqukuva mylan 345

10Chwetheza Uluhlu1.extend (Uluhlu2) kwaye cinezela u-Enter.

Iikopi zePython zonke izinto ezikwiUluhlu2kude kube sekupheleniUluhlu1. Ukwandiswa kuhlala kusetyenziswa ukudibanisa uluhlu ezimbini.


IPython ibonisa utshintsho olwenziwe ngabasebenzisi kuluhlu xa bechwetheza uluhlu1.

Shumi elinanyeChwetheza Uluhlu1 kwaye ucofe u-Enter.

Uyabona ukuba ikopi kunye neenkqubo ezandisiweyo zisebenzile.Uluhlu1ngoku iqulethe amaxabiso 2, 1, 2, kunye no-1.


Ukuba abasebenzisi bathayipha uList1.pop (), iPython iya kulisusa ixabiso ekugqibeleni koluhlu.

12Chwetheza Uluhlu1.pop () kwaye ucinezele u-Enter.

I-Python ibonisa ixabiso elingu-1. 1 wayegcinwe ekupheleni koluhlu, kwayepop ()ihlala isusa amaxabiso esiphelweni.

13Chwetheza Uluhlu1.susa (1) kwaye ucinezele u-Enter.

Ngeli xesha, Python isusa into kwi-element 1. Ngokungafaniyo ne-pop ()function, thesusa ()Umsebenzi awubonakalisi ixabiso lento isusileyo.

14Chwetheza uluhlu1.clear () kwaye ucofe u-Enter.

Sebenzisacacisa ()kuthetha ukuba uluhlu alunakuba naziphi na izinto ngoku.

Shumi elinantlanuChwetheza len (Uluhlu1) kwaye ucofe u-Enter.

Uyabona ukuba iziphumo ziyi-0.Uluhlu1ayinanto kwaphela. Okwangoku, uzamile zonke iindlela zokulungisa ezibonelelwa luluhlu. Sebenza kunyeUluhlu1ezinye usebenzisa le misebenzi eyahlukeneyo de uzive ukhululekile ukwenza utshintsho kuluhlu.

16Vala ifestile yeShell yePython.

Ndiyakuvuyela ngomsebenzi owenziwe kakuhle!