Faclairean Python

Is e faclairean am prìomh sheòrsa mapaidh a bhios sinn a ’cleachdadh ann am Python. Tha an nì seo coltach ri mapa ann an Java.

Ann am Python, bidh sinn a ’cruthachadh litrichean faclair le bhith a’ cleachdadh sèbhirean lùbach {}, a ’sgaradh iuchraichean bho luachan a’ cleachdadh coloin :, agus a ’sgaradh paidhrichean iuchraichean / luach le bhith a’ cleachdadh cromagan ,.



Mar a chruthaicheas tu faclair

datedict = { 'date': 13, 'month': 'January', 'year': 1970 } print(datedict)

Toradh:


{'date': 13, 'month': 'January', 'year': 1970}

Mar a gheibh thu cothrom air stuthan faclair

Faodaidh sinn faighinn gu nithean faclair le bhith a ’toirt iomradh air a’ phrìomh ainm taobh a-staigh camagan ceàrnach [] no a ’cleachdadh an get() modh:

datedict = { 'date': 13, 'month': 'January', 'year': 1970 } date = datedict['date'] year = datedict.get('year') print(date, year)

Toradh:


13 1970

Mar a dh ’atharraicheas tu luach nì

Faodaidh sinn luach rud anns an fhaclair atharrachadh le bhith a ’toirt iomradh air a phrìomh ainm, mar eisimpleir:

datedict = { 'date': 13, 'month': 'January', 'year': 1970 } datedict['date'] = 20 print(datedict)

Toradh:

{'date': 20, 'month': 'January', 'year': 1970}

Mar a nì thu lùbadh tro fhaclair

Faodaidh sinn lùbadh tro fhaclair a ’cleachdadh an for lùb. Nuair a thèid sinn tro fhaclair, is urrainn dhuinn na h-iuchraichean, na luachan uile no na paidhrichean iuchrach / luach uile a chlò-bhualadh:

Faigh na h-iuchraichean faclair gu lèir

datedict = { 'date': 13, 'month': 'January', 'year': 1970 } for d in datedict:
print(d)

Toradh:


date month year

Faigh a h-uile luach faclair

datedict = { 'date': 13, 'month': 'January', 'year': 1970 } for d in datedict:
print(datedict[d])

Toradh:

13 January 1970

Faodaidh sinn cuideachd an values() a chleachdadh gnìomh gus na luachan a thilleadh:

datedict = { 'date': 13, 'month': 'January', 'year': 1970 } for d in datedict.values():
print(d)

Toradh:

13 January 1970

Faigh an dà chuid iuchraichean agus luachan anns an fhaclair

Faodaidh sinn an items() a chleachdadh gnìomh gus iuchraichean agus luachan a chlò-bhualadh:


datedict = { 'date': 13, 'month': 'January', 'year': 1970 } for k, v in datedict.items():
print(k, v)

Toradh:

date 13 month January year 1970

Mar a gheibh thu fad faclair

Gheibh thu fad an fhaclair (àireamh de phrìomh chàraidean / luach) le bhith a ’gairm an len() gnìomh, e.g .:

datedict = { 'date': 13, 'month': 'January', 'year': 1970 } print(len(datedict))

Toradh:

3

Mar a chuireas tu stuthan ri faclair

Gus paidhir iuchrach / luach a chur ri faclair feumaidh sinn iuchair ùr agus luach co-cheangailte a thoirt seachad. Mar eisimpleir:


datedict = { 'date': 13, 'month': 'January', 'year': 1970 } datedict['season'] = 'winter' print(datedict)

Toradh:

{'date': 13, 'month': 'January', 'year': 1970, 'season': 'winter'}

Mar a bheir thu air falbh stuthan bho fhaclair

Gus rud a thoirt air falbh bho fhaclair, thoir am prìomh ainm don pop() modh.

datedict = { 'date': 13, 'month': 'January', 'year': 1970, 'season': 'winter' } datedict.pop('season') print(datedict)

Toradh:

{'date': 13, 'month': 'January', 'year': 1970}

Faodaidh sinn cuideachd an del a chleachdadh prìomh fhacal airson rud a dhubhadh às le iuchair ainmichte:


datedict = { 'date': 13, 'month': 'January', 'year': 1970, 'season': 'winter' } del datedict['season'] print(datedict)

Toradh:

{'date': 13, 'month': 'January', 'year': 1970} Nota:Tha an de faodar prìomh fhacal a chleachdadh cuideachd gus am faclair a thoirt air falbh gu tur. datedict = { 'date': 13, 'month': 'January', 'year': 1970, } del datedict

Mar a gheibh thu am faclair falamh

Gus am faclair fhalamhachadh bho na prìomh chàraidean / luach, cleachd an clear() modh

datedict = { 'date': 13, 'month': 'January', 'year': 1970, 'season': 'winter' } datedict.clear() print(datedict)

Toradh:

{}