Python Lists

Muchidzidzo ichi tichadzidza nezve Python Lists; maitiro ekugadzira rondedzero, kuwana zvinhu, kubvisa zvinhu, kudzima rondedzero uye zvichingodaro.

MuPython, Rondedzero dzinovakwa uchishandisa mabhureki akaenzana [] uye chinhu chimwe nechimwe chiri mune chinyorwa chakaparadzaniswa nekoma ,.

Python zvinyorwa zvinogona kuva nemhando dzakasiyana dzakasiyana dzezvinhu, saka hadzisi dzese dzinofanirwa kuve tambo, kana manhamba. Semuenzaniso, tinogona kuve nerondedzero ine mhando dzakasanganiswa:

mixedTypesList = ['a', True, 1, 1.0]

Maitiro Ekugadzira Rondedzero

colorsList = ['red', 'green', 'blue'] print(colorsList)

Kubuda:

['red', 'green', 'blue']

Maitiro Ekuwana Zvinhu zveRondedzero

Rangarira: chinhu chekutanga mune rondedzero chiri pane index 0.

colorsList = ['red', 'green', 'blue'] print(colorsList[2])

Kubuda:



blue

Kuwana Range reZvinhu (Kuchekerera)

Tinogona kudoma huwandu hwezvinhu kubva pane runyorwa nekutsanangura iyo yekutanga index uye yekupedzisira index. Isu tinoshandisa iyo : Anoshanda.

Ongorora: Mumuenzaniso unotevera, zvinobuda zvinobva pane index 1 (inosanganisirwa) kune index 4 (isina kubviswa)

colorsList = ['red', 'green', 'blue', 'orange', 'yellow', 'white'] print(colorsList[1:4])

Kubuda:

['green', 'blue', 'orange']

Yakashata Indexing

Isu tinokwanisa kuwana izvo zvinhu zviri pane zvakanyorwa kubva kumagumo nekutsanangura isina kunaka index index. Semuenzaniso -1 zvinoreva chinhu chekupedzisira uye -2 zvinoreva chinhu chechipiri chekupedzisira.

colorsList = ['red', 'green', 'blue', 'orange'] print(colorsList[-1])

Kubuda:

orange

Chinja Kukosha Kwechinhu

colorsList = ['red', 'green', 'blue', 'orange'] colorsList[3] = 'yellow' print(colorsList)

Kubuda:

['red', 'green', 'blue', 'yellow']

Maitiro Ekusunungura kuburikidza neRondedzero

Tinogona kusunungura kuburikidza nerondedzero tichishandisa for chiuno.

colorsList = ['red', 'green', 'blue', 'orange'] for i in colorsList:
print(i)

Kubuda:

red green blue orange

Maitiro Ekuwedzera Zvinhu kuRondedzero

Pane nzira mbiri dzekuwedzera zvinhu kuRondedzero muPython, append() uye insert()

I append() nzira inowedzera zvinhu kumagumo eiyo runyorwa:

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.append('yellow') print(colorsList)

Kubuda:

['red', 'green', 'blue', 'orange', 'yellow']

I insert() nzira inowedzera chinhu pane yakatarwa index:

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.insert(2, 'yellow') print(colorsList)

Kubuda:

['red', 'green', 'yellow', 'blue', 'orange']

Maitiro Ekubvisa Zvinhu Kubva Chinyorwa

Unogona kubvisa zvinhu kubva pane zvinyorwa uchishandisa zvinhu zvinoverengeka:

remove() inobvisa chinhu chakatsanangurwa

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.remove('orange') print(colorsList)

Kubuda:

['red', 'green', 'blue']

pop() inobvisa chinhu pane yakatarwa index kana kubvisa chinhu chekupedzisira kana pasina index yakapihwa

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.pop(1) print(colorsList)

Kubuda:

['red', 'blue', 'orange'] colorsList = ['red', 'green', 'blue', 'orange'] colorsList.pop() print(colorsList)

Kubuda:

['red', 'grenn', 'blue']

del() inobvisa chinhu pane yakatarwa index kana kubvisa rwese runyorwa

colorsList = ['red', 'green', 'blue', 'orange'] del colorList[1] print(colorsList)

Kubuda:

['red', 'blue', 'orange'] colorsList = ['red', 'green', 'blue', 'orange'] del colorList print(colorsList)

Kubuda:

Traceback (most recent call last): File 'pythonList.py', line 30, in
print(colorsList) NameError: name 'colorsList' is not defined

clear() inodurura runyorwa

colorsList = ['red', 'green', 'blue', 'orange'] colorList.clear() print(colorsList)

Kubuda:

[]

Maitiro Ekuwana Kureba kweRondedzero

Iwe unogona kuwana iyo urefu hwerunhare nekudana iyo len() basa, i.k.

colorsList = ['red', 'green', 'blue', 'orange'] print(len(colorsList))

Kubuda:

4

Verenga Nhamba Yezvinhu Zvakatsanangurwa

Tinogona kushandisa count() shanda pane chinyorwa kuti uwane huwandu hwezviitiko zvechinhu chakatsanangurwa mune irondedzero. Semuyenzaniso:

colorsList = ['red', 'green', 'red', 'orange'] print(colorsList.count('red'))

Kubuda:

2

Maitiro Ekuronga Zvinhu zveRondedzero

Mune ino kesi, iyo _ _ + _ | basa rinomiririra runyorwa nemaarufabheti.

sort()

Kubuda:

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.sort() print(colorsList)

Sort in Reverse Order

['blue', 'green', 'orange', 'red']

Kubuda:

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.sort(reverse=True) print(colorsList)

Maitiro Ekudzoreredza Izvo Zvinhu zveRondedzero

Tinogona kushandisa, iyo ['red', 'orange', 'green', 'blue'] basa kudzosera rondedzero, i.k.

reverse()

Kubuda:

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.reverse() print(colorsList)

Maitiro Ekuteedzera Rondedzero kune Rumwe Runyorwa

Tinogona kushandisa ['orange', 'blue', 'green', 'red'] basa kuteedzera zviri mukati merondedzero kune rumwe runyorwa.

copy()

Kubuda:

colorsList = ['red', 'green', 'blue', 'orange'] newList = colorsList.copy() print(newList)

Maitiro Ekujoinisa Rondedzero Mbiri Pamwechete

Nzira yakapusa yekubatanidza maviri zvinyorwa pamwechete ndeye kushandisa _ _ + _ | Anoshanda. Semuyenzaniso:

['red', 'green', 'blue', 'orange']

Kubuda:

+