Python Akhazikitsa

Python Sets ndi mtundu wosonkhanitsa womwe uli ndi fayilo ya chosasintha kusonkhanitsa wapadera ndipo osasintha zinthu. Mwanjira ina, mtundu wa Python sungakhale ndi zinthu zobwereza ndipo seti itangopangidwa, zinthuzo sizingasinthe.

Zindikirani:Zinthu za seti sizingasinthe, zomwe zikutanthauza kuti sitingasinthe zinthuzo. Komabe, masanjidwewo amatha kusintha, mwachitsanzo, titha kuwonjezera ndikuchotsa zinthuzo.

Lamuloli silisungidwa. Mwachitsanzo, nthawi iliyonse mukasindikiza seti yomweyo, dongosolo lazinthu lingakhale losiyana.

Mu Python, maseti amamangidwa pogwiritsa ntchito mabotolo opindika {} ndipo chinthu chilichonse mu setiyi chimasiyanitsidwa ndi comma ,.


Monga mndandanda wa Python, ma seti amathanso kukhala ndi mitundu ingapo yazinthu zingapo, motero sizofunikira kukhala zingwe, kapena manambala. Mwachitsanzo, titha kukhala ndi seti yokhala ndi mitundu yosiyanasiyana:

mixedTypesSet = {'one', True, 13, 2.0}

Momwe Mungapangire Set

colorsSet = {'red', 'green', 'blue'} print(colorsSet)

Kutulutsa:


{'red', 'blue', 'green'}

Momwe Mungapezere Zinthu Zosintha

Sitingagwiritse ntchito cholozera kuti tipeze chinthu chosungidwa. Izi ndichifukwa choti seti sinasinthidwe ndipo sasunga index. Komabe, titha kugwiritsa ntchito for tambasukani kuti muzitha kudutsa pazinthu zomwe zakonzedwa.

colorsSet = {'red', 'green', 'blue'} for c in colorsSet:
print(c)

Kutulutsa:

green red blue

Momwe Mungawonjezere Zinthu Kusintha

Kuti tiwonjezere chinthu chimodzi pagulu tiyenera kugwiritsa ntchito add() njira.

Kuti tiwonjezere zinthu zingapo pa seti tiyenera kugwiritsa ntchito update() njira.


Kuwonjezera Chinthu chimodzi

colorsSet = {'red', 'green', 'blue'} colorsSet.add('yellow') print(colorsSet)

Kutulutsa:

{'blue', 'red', 'green', 'yellow'}

Kuwonjezera Chinthu Choposa Chimodzi

colorsSet = {'red', 'green', 'blue'} colorsSet.update(['yellow', 'orange', 'white']) print(colorsSet)

Kutulutsa:

{'white', 'red', 'green', 'yellow', 'orange', 'blue'}

Momwe Mungatulutsire Chinthu Pachigawo

Pali njira ziwiri zochotsera chinthu pa seti: remove() ndi discard().

remove() Njira imachotsa chinthucho. Ngati chinthucho kulibe, remove() adzautsa cholakwika.


colorsSet = {'red', 'green', 'blue', 'orange'} colorsSet.remove('orange') print(colorsSet)

Kutulutsa:

{'blue', 'green', 'red'}

discard() Njirayo imachotsa chinthucho. Ngati chinthucho kulibe, discard() ndidzatero OSATI kwezani cholakwika.

Chotsani Zinthu Zonse za Set

Kuchotsa zinthu zonse ndikukhala ndi seti, timagwiritsa ntchito clear() njira:

colorsSet = {'red', 'green', 'blue', 'orange'} colorsSet.clear() print(colorsSet)

Kutulutsa:


set()

Chotsani Set kwathunthu

Kuti muchotse seti yonse, gwiritsani ntchito del mawu osakira:

colorsSet = {'red', 'green', 'blue', 'orange'} del colorSet print(colorsSet)

Kutulutsa:

Traceback (most recent call last): File 'pythonSet.py', line 78, in
del colorSet NameError: name 'colorSet' is not defined


Momwe Mungapezere Kutalika kwa Set

Mutha kupeza kutalika kwake poyimba len() njira, mwachitsanzo:

colorsSet = {'red', 'green', 'blue', 'orange'} print(len(colorsSet))

Kutulutsa:


4

Momwe Mungalumikizire Maseti Awiri Pamodzi

Njira yosavuta yolumikizira magulu awiri ndikugwiritsa ntchito union() njira yomwe imabwezeretsa seti yatsopano yomwe ili ndi zinthu zojambulidwa.

colorsSet = {'red', 'green', 'blue', 'orange'} numbersSet = {1, 2, 3, 4} numbersAndColors = colorsSet.union(numbersSet) print(numbersAndColors)

Kutulutsa:

{1, 2, 'blue', 3, 4, 'green', 'red', 'orange'}