Operaţii pe liste, seturi, dicţionare în PYTHON

Aici fac o prezentare a operaţiilor definite pe liste, seturi şi dicţionare din limbajului de programare PYTHON. Problemele de care mă ocup sunt secvenţele de instrucţiuni asociate operaţiilor.

OPERATIA LISTA SET DICTIONAR
Definirea LIST = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] SET = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} DICTIONAR = {1:100, 2:250, 3:11, 4:400, 5:120, 6:1000, 7:22, 8:77, 9:99, 10:1}
Afisarea print(LISTA) print(SET) print(DICTIONAR)
Copiere LISTA_A = LISTA SET_A = SET DICTIONAR_A = DICTIONAR
Numarare componente nr = len(LISTA) nr = len(SET) nr = len(DICTIONAR)
Sortarea LISTA_B = sorted(LISTA) print(LISTA_B) SET_B = sorted(SET) print(SET_B) DICTIONAR_B = sorted(DICTIONAR) # sortare dupa cheie print(DICTIONAR_B)
Concatenare LISTA_A= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
LISTA_B= [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
LISTA_C = LISTA_A + LISTA_B
print(LISTA_C)
SET_A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} SET_B = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
SET_C = SET_A + SET_B (imposibil!!!)
print(SET_C)
DICTIONAR_A = {1:100, 2:250, 3:11, 4:400, 5:120, 6:1000, 7:22, 8:77, 9:99, 10:1} DICTIONAR_B = {10:100, 20:250, 30:11, 40:400, 50:120, 60:1000, 70:22, 80:77, 90:99, 100:1}
DICTIONAR_C = DICTIONAR_A + DICTIONAR_B (imposibil!!!)
print(DICTIONAR_C)
Adaugare element la sfarsit nume_lista.append(element) nume_set.add(element) nume_dictionar.update(valoare)
Inserare de element mume_lista.insert(pozitie_element, valoare) mume_set.add(valoare_element) mume_dictionar.update(valoare_element)
Stergere de element mume_lista.remove(pozitie_element) mume_set.remove(valoare_element) mume_dictionar.pop(cheie_element)
Programul de mai jos exemplifică operaţiile cu structuri este:


#
# Liste, seturi, dictionare
#
# ******************Operatii pe liste
#
LISTA_A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
LISTA_B = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
print('Lista LISTA_A=', LISTA_A)
LISTA_X = LISTA_A
print('Lista copiata LISTA_X =', LISTA_X)
LISTA_C = LISTA_B + LISTA_A
print('Lista concatenata LISTA_C=', LISTA_C)
LISTA_D = sorted(LISTA_C)
print('Lista sortata LISTA_D =', LISTA_D)
X = 55
LISTA_A.append(X)
print('LISTA_A dupa dadaugare element la sfarsit =', LISTA_A)
Y = 23
IPOZ = 7
LISTA_A.insert(IPOZ,Y)
print('LISTA_A dupa inserarea unui element pe pozitia IPOZ =', LISTA_A)
Z = 7
IPOZ = 3
LISTA_A.insert(IPOZ,Z)
print('LISTA_A dupa inserarea unui element pe pozitia IPOZ chiar daca el mai exista in lista =', LISTA_A)
VAL =2
LISTA_A.remove(VAL)
print('LISTA_A dupa stergerea elementului cu valoarea VAL =', LISTA_A)
print('Setul după sortare =', LISTA_A.sort())
#
# *****************Operatii pe seturi
#
SET_A = {3, 4, 5, 8, 1, 9, 10}
print('Setul initial este =', SET_A)
print(SET_A)
Valoare = 6
SET_A.add(Valoare)
Valoare = 1
SET_A.add(Valoare)
print('Setul după adaugare in fata element =', SET_A)
Valoare = 25
SET_A.add(Valoare)
print('Setul după adaugare la sfarsit element =', SET_A)
Valoare = 9
SET_A.remove(Valoare)
print('Setul după stergere element =', SET_A)
#
# *****************Operatii pe dictionare
#
DICT_A = {333:'teta', 7:'beta', 52:'gama', 88:'delta', 91:'eta', 107:'alfa'}
print('Dictionarul initial este =', DICT_A)
Cheie =12
Valoare = 'epsilon'
DICT_A.update({Cheie:Valoare})
print('Dictionarul dupa adaugare element este =', DICT_A)
Cheie = 52
DICT_A.pop(Cheie)
print('Dictionarul dupa stergere element este =', DICT_A)
DICT_D = sorted(DICT_A.keys())
print('Cheile sortate din dictionarul sortat sunt =', DICT_D)
DICT_E = sorted(DICT_A.items())
print('Dictionarul sortat dupa cheie este =', DICT_E)



Rezultatele afişate sunt:


Lista LISTA_A= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Lista copiata LISTA_X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Lista concatenata LISTA_C= [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Lista sortata LISTA_D = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
LISTA_A dupa dadaugare element la sfarsit = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 55]
LISTA_A dupa inserarea unui element pe pozitia IPOZ = [1, 2, 3, 4, 5, 6, 7, 23, 8, 9, 10, 55]
LISTA_A dupa inserarea unui element pe pozitia IPOZ chiar daca el mai exista in lista = [1, 2, 3, 7, 4, 5, 6, 7, 23, 8, 9, 10, 55]
LISTA_A dupa stergerea elementului cu valoarea VAL = [1, 3, 7, 4, 5, 6, 7, 23, 8, 9, 10, 55]
Setul după sortare = None
Setul initial este = {1, 3, 4, 5, 8, 9, 10}
{1, 3, 4, 5, 8, 9, 10}
Setul dupa adaugare in fata element = {1, 3, 4, 5, 6, 8, 9, 10}
Setul dupa adaugare la sfarsit element = {1, 3, 4, 5, 6, 8, 9, 10, 25}
Setul dupa stergere element = {1, 3, 4, 5, 6, 8, 10, 25}
Dictionarul initial este = {333: 'teta', 7: 'beta', 52: 'gama', 88: 'delta', 91: 'eta', 107: 'alfa'}
Dictionarul dupa adaugare element este = {333: 'teta', 7: 'beta', 52: 'gama', 88: 'delta', 91: 'eta', 107: 'alfa', 12: 'epsilon'}
Dictionarul dupa stergere element este = {333: 'teta', 7: 'beta', 88: 'delta', 91: 'eta', 107: 'alfa', 12: 'epsilon'}
Cheile sortate din dictionarul sortat sunt = [7, 12, 88, 91, 107, 333]
Dictionarul sortat dupa cheie este = [(7, 'beta'), (12, 'epsilon'), (88, 'delta'), (91, 'eta'), (107, 'alfa'), (333, 'teta')]


Limbajul PYTHON are o mare diversitate de variante de realizare a operaţiilor cu structuri.

(afişat azi 16 aprilie 2022 ora 10,20)
revenire