Created
February 11, 2020 04:46
-
-
Save luizomf/daa038e6b02d8cba125d87a28e66ec8b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Considerando duas listas de inteiros ou floats (lista A e lista B) | |
Some os valores nas listas retornando uma nova lista com os valores somados: | |
Se uma lista for maior que a outra, a soma só vai considerar o tamanho da | |
menor. | |
Exemplo: | |
lista_a = [1, 2, 3, 4, 5, 6, 7] | |
lista_b = [1, 2, 3, 4] | |
=================== resultado | |
lista_soma = [2, 4, 6, 8] | |
""" | |
lista_a = [10, 2, 3, 40, 5, 6, 7] | |
lista_b = [1, 2, 3, 4] | |
lista_soma = [x + y for x, y in zip(lista_a, lista_b)] | |
print(lista_soma) | |
# lista_soma = [] | |
# for i in range(len(lista_b)): | |
# lista_soma.append(lista_a[i] + lista_b[i]) | |
# print(lista_soma) | |
# lista_soma = [] | |
# for i, _ in enumerate(lista_b): | |
# lista_soma.append(lista_a[i] + lista_b[i]) | |
# print(lista_soma) |
list_a = [1, 2, 3, 4 ,5 ,6, 7]
list_b = [1, 2, 3, 4]
def List_sum(list1, list2):
size = min(len(list1),len(list2))
return[
list1[i] + list2[i] for i in range(size)
]
print(List_sum(list_a, list_b))
l1 = [1, 2, 3, 4, 5, 6, 7]
l2 = [1, 2, 3, 4]
def sum_list(list1, list2):
list_sum = []
value_min = min(list1, list2)
for x in range(len(value_min)):
new_list = list1[x] + list2[x]
list_sum.append(new_list)
return list_sum
print(sum_list(l1, l2))
def criar_funcao(func):
def interna(*args, **kwargs):
for arg in args:
is_int(arg)
resultado = func(*args, **kwargs)
return resultado
return interna
def is_int(param):
if not isinstance(param, int):
raise TypeError('param deve ser um int')
@criar_funcao
def soma(a, b):
return a + b
def zipper(lista1, lista2):
intervalo = min(len(lista1), len(lista2))
return [
soma(lista1[i],lista2[i]) for i in range(intervalo)
]
lista_a = [1, 2, 3, 4, 5, 6, 7]
lista_b = [1, 2, 3, 4]
print(list(zipper(lista_a, lista_b)))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
lista_a = [1, 2.8, 3, 4, 5, 6, 7]
lista_b = [1, 2, 3, 4]
def somando_valores(l1, l2):
menor_valor = min(len(l1), len(l2))
if len(l1) <= len(l2):
return [l1[i] + l2[i] for i in range(menor_valor)]
elif len(l2) < len(l1):
return [l2[i] + l1[i] for i in range(menor_valor)]
else:
return "Não sei como chegou aq"
print(somando_valores(lista_a, lista_b))