Skip to content

Instantly share code, notes, and snippets.

@rg3915
Last active May 16, 2018 01:04
Show Gist options
  • Save rg3915/65d87fa6221c6a032708dcf43d6b35a7 to your computer and use it in GitHub Desktop.
Save rg3915/65d87fa6221c6a032708dcf43d6b35a7 to your computer and use it in GitHub Desktop.
Renumerando uma lista
# Dada a lista
lista = [1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 6, 6, 9, 9, 9]
# como eu chego na lista
nova_lista = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5]
questions_order = (1, 2, 4, 6, 8, 9)
new_orders = {}
for c, q in enumerate(questions_order, 1):
new_orders[str(q)] = c
lista = [1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 6, 6, 8, 9, 9]
nova_lista = []
for c, l in enumerate(lista):
nova_lista.append(new_orders[str(l)])
print(nova_lista)
# ou
list("".join([str(i) * lista.count(x) for i, x in enumerate(set(lista), 1)]))
# ou
def produce_counter(iterable):
previous = None
diff_count = 0
for current in iterable:
if current != previous:
diff_count += 1
previous = current
yield diff_count
print(list(produce_counter([1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 6, 6, 9, 9, 9])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment