153 lines
4.1 KiB
Python
153 lines
4.1 KiB
Python
"""
|
|
MATRICES
|
|
|
|
se pueden considerar como listas de listas
|
|
lo visto en listas aplica tambien en matrices
|
|
|
|
"""
|
|
# Definción de matriz de 3 filas y 4 columnas.
|
|
matriz = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
|
|
|
|
# Acceso a elementos matriz[ fila o lista1 ] [ columna o lista2 ]
|
|
matriz[0][0] # 1
|
|
matriz[1][2] # 7
|
|
|
|
# Ejm. suma de matrices
|
|
def suma_matr(A, B):
|
|
"""
|
|
Suma 2 matrices.
|
|
A: len(A) == len(B), integers.
|
|
B: len(A) == len(B), integers.
|
|
returns: Matriz con resulta de la suma de
|
|
los elementos de A y B
|
|
"""
|
|
filas, colums, C = len(A), len(A[0]), []
|
|
for fila in range(filas):
|
|
fila_temp = []
|
|
for columna in range(colums):
|
|
fila_temp.append(A[fila][columna] + B[fila][columna])
|
|
C.append(fila_temp)
|
|
return C
|
|
|
|
print(suma_matr(matriz, matriz))
|
|
|
|
# LIBRERIA NUMPY PARA TRABAJAR CON MATRICES
|
|
|
|
|
|
# CONJUNTOS
|
|
# Un conjunto esta entre llaves no tiene elementos repetidos.
|
|
|
|
frutas = {'manzana', 'naranja', 'manzana', 'pera', 'naranja', 'banana', 'kiwi'}
|
|
|
|
print(frutas) #{'kiwi', 'naranja', 'manzana', 'banana', 'pera'}
|
|
|
|
print('pera' in frutas, 'yerba' in frutas) # True False
|
|
|
|
# Creacion de conjuntos:
|
|
conj_a = set()
|
|
type(conj_a) #<class 'set'>
|
|
|
|
a = set('abracadabra') #{'r', 'a', 'b', 'd', 'c'}
|
|
b = set('alacazam') #{'l', 'm', 'a', 'z', 'c'}
|
|
|
|
print('\n a =',a ,' b =',b)
|
|
|
|
# Operaciones de conjuntos:
|
|
|
|
a - b # {'d', 'b', 'r'} elementos de a menos elementos de b
|
|
a | b # {'l', 'd', 'a', 'z', 'm', 'c', 'b', 'r'} elementos de a y b
|
|
a & b # {'c', 'a'} elementos en común (INTERSECCION)
|
|
a ^ b # {'l', 'z', 'b', 'm', 'd', 'r'} elementos únicos de cada set
|
|
|
|
# Comprensión de conjuntos:
|
|
|
|
a = {x for x in 'abracadabra' if x not in 'abc'}
|
|
|
|
a.add('z')
|
|
a.remove('z')
|
|
|
|
print('\n', a) #{'r', 'd'}
|
|
|
|
''' >>>help(set)
|
|
Help on class set in module builtins:
|
|
|
|
class set(object)
|
|
| set() -> new empty set object
|
|
| set(iterable) -> new set object
|
|
|
|
|
| Build an unordered collection of unique elements.
|
|
|
|
|
| Methods defined here:
|
|
| add(...)
|
|
| Add an element to a set.
|
|
|
|
|
| This has no effect if the element is already present.
|
|
|
|
|
| clear(...)
|
|
| Remove all elements from this set.
|
|
|
|
|
| copy(...)
|
|
| Return a shallow copy of a set.
|
|
|
|
|
| difference(...)
|
|
| Return the difference of two or more sets as a new set.
|
|
|
|
|
| (i.e. all elements that are in this set but not the others.)
|
|
|
|
|
| difference(...)
|
|
| Return the difference of two or more sets as a new set.
|
|
|
|
|
| (i.e. all elements that are in this set but not the others.)
|
|
|
|
|
| difference_update(...)
|
|
| Remove all elements of another set from this set.
|
|
|
|
|
| discard(...)
|
|
| Remove an element from a set if it is a member.
|
|
|
|
|
| If the element is not a member, do nothing.
|
|
|
|
|
| intersection(...)
|
|
| Return the intersection of two sets as a new set.
|
|
|
|
|
| (i.e. all elements that are in both sets.)
|
|
|
|
|
| intersection_update(...)
|
|
| Update a set with the intersection of itself and another.
|
|
|
|
|
| isdisjoint(...)
|
|
| Return True if two sets have a null intersection.
|
|
|
|
|
| issubset(...)
|
|
| Report whether another set contains this set.
|
|
|
|
|
| issuperset(...)
|
|
| Report whether this set contains another set.
|
|
|
|
|
| pop(...)
|
|
| Remove and return an arbitrary set element.
|
|
| Raises KeyError if the set is empty.
|
|
|
|
|
| remove(...)
|
|
| Remove an element from a set; it must be a member.
|
|
|
|
|
| If the element is not a member, raise a KeyError.
|
|
|
|
|
| symmetric_difference(...)
|
|
| Return the symmetric difference of two sets as a new set.
|
|
|
|
|
| (i.e. all elements that are in exactly one of the sets.)
|
|
|
|
|
| symmetric_difference_update(...)
|
|
| Update a set with the symmetric difference of itself and another.
|
|
|
|
|
| union(...)
|
|
| Return the union of sets as a new set.
|
|
|
|
|
| (i.e. all elements that are in either set.)
|
|
|
|
|
| update(...)
|
|
| Update a set with the union of itself and others.
|
|
|
|
|
| ----------------------------------------------------------------------
|
|
''' |