Python vārdnīcas populācija ()

Metode Python popitem () noņem un atgriež vārdnīcā ievietoto pēdējo elementu (atslēgu, vērtību) pāri.

Sintakse popitem()ir:

 dict.popitem ()

Parametri popitem () metodei

popitem()Neveic nekādus parametrus.

Atgriezt vērtību no metodes popitem ()

Par popitem()metode novērš un atgriež (taustiņu vērtība) pāris no vārdnīcas, kas pēdējo reizi, pirmais ārā (LIFO) rīkojums.

  • Atgriež jaunāko vārdnīcas ievietoto elementu (atslēgu, vērtību) pāri.
  • Noņem no vārdnīcas atgriezto elementu pāri.

Piezīme. Pirms Python 3.7 popitem()metode atgrieza un no vārdnīcas noņēma patvaļīgu elementu (atslēgu, vērtību) pāri.

Piemērs: Popitem () metodes darbība

 person = ('name': 'Phill', 'age': 22, 'salary': 3500.0) # ('salary', 3500.0) is inserted at the last, so it is removed. result = person.popitem() print('Return Value = ', result) print('person = ', person) # inserting a new element pair person('profession') = 'Plumber' # now ('profession', 'Plumber') is the latest element result = person.popitem() print('Return Value = ', result) print('person = ', person)

Rezultāts

 Atgriešanās vērtība = ('alga', 3500,0) persona = ('vārds': 'Phill', 'vecums': 22) Atgriešanās vērtība = ('profesija', 'santehniķis') persona = ('vārds': 'Phill', "vecums": 22)

Piezīme . popitem()Metode rada KeyErrorkļūdu, ja vārdnīca ir tukša.

Interesanti raksti...