How to print a number of a list in color in Python

I want to compare two lists and if two numbers are not the same, print them in red, only those which are not the same as...!! thnks!!!

salida = ''.join(map(str, bitsconerror))
entrada = ''.join(map(str, bits))
print("Tras la transmisión de", entrada,"por el canal, la salida queda:",salida)
print()
if bits == bitsconerror:
    print("La secuencia de bits NO HA SIDO MODIFICADA al transmitirse por el canal")
else:
    print("La secuencia de bits HA SIDO MODIFICADA al transmitirse por el canal")

print()
#HERE...HOW TO PRINT IN RED ONLY THOSE WHICH ARE NOT THE SAME
print("La secuencia a la entrada era:",  entrada)
print("La secuencia a la salida es  :", salida)

errores = 0
for a,b in zip(bits,bitsconerror):
    if a != b:
       errores = errores + 1
print()
print("Con un error de ",errores," bits")

Upvotes: 2

Views: 836

Answers (1)

Alderven
Alderven

Reputation: 8270

One option is to use colorama lib to color your output:

from colorama import init, Fore, Style
init()

bitsconerror = [0, 2, 4, 6, 8, 10, 12]
bits = [0, 3, 6, 9, 12, 15]

print(' '.join([str(el) if el in bits else f'{Fore.RED}{el}{Style.RESET_ALL}' for el in bitsconerror]))
print(' '.join([str(el) if el in bitsconerror else f'{Fore.RED}{el}{Style.RESET_ALL}' for el in bits]))

Output:

enter image description here

Upvotes: 1

Related Questions