how to print ones and zeros in columns with their indexes in python?

Viewed 91

I have a list of zeros and ones, I want to print them in two different columns with headings and index numbers. Something like this. list = [1,0,1,1,1,0,1,0,1,0,0]

ones      zeros
1 1       2 0
3 1       6 0
4 1       8 0
5 1       10 0
7 1       11 0
9 1

This is the desired output.

I tried this:

list = [1,0,1,1,1,0,1,0,1,0,0]

print('ones',end='\t')
print('zeros')

for index,ele in enumerate(list,start=1):
    if ele==1:
        print(index,ele,end="    ")
    elif ele==0:
        print("    ")
        print(index,ele,end="    ")
    else:
        print()

But this gives output like this:

ones    zeros
1 1        
2 0    3 1    4 1    5 1        
6 0    7 1        
8 0    9 1        
10 0        
11 0   

How do get the desired output? Any help is appreciated.

2 Answers

You can use itertools.zip_longest, str.ljust, f-strings (for formatting), and some calculations for the printing part, and use two lists to hold the indices of both zeros and ones:

l = [1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0]

ones, zeros = [], []
max_len_zeros = max_len_ones = 0

for index, num in enumerate(l, 1):
  if num == 0:
    zeros.append(index)
    max_len_zeros = max(max_len_zeros, len(str(index)))
  else:
    ones.append(index)
    max_len_ones = max(max_len_ones, len(str(index)))

from itertools import zip_longest

print('ones' + ' ' * (max_len_ones + 2) + 'zeros')

for ones_index, zeros_index in zip_longest(ones, zeros, fillvalue = ''):
  one = '1' if ones_index else ' '
  this_one_index = str(ones_index).ljust(max_len_ones)
  zero = '0' if zeros_index else ''
  this_zero_index = str(zeros_index).ljust(max_len_zeros)
  print(f'{this_one_index} {one}    {this_zero_index} {zero}')

Output:

ones   zeros
1 1    2  0
3 1    6  0
4 1    8  0
5 1    10 0
7 1    11 0
9 1        

List with more zeros than ones:

In: l = [1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0]
Out:
ones    zeros
1  1    2  0
4  1    3  0
7  1    5  0
9  1    6  0
10 1    8  0
14 1    11 0
        12 0
        13 0
        15 0

List with equal number of zeros and ones:

In: l = [1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1]
Out:
ones    zeros
1  1    2  0
3  1    4  0
5  1    6  0
8  1    7  0
9  1    10 0
11 1    13 0
12 1    14 0
15 1    16 0
18 1    17 0
20 1    19 0

It's hard to do what you need in an iterative way. I have kind of a "broken" solution that both shows how you could better do what you are trying to do and why an iterative approach is limited in this case.

I updated your code as following:

list = [1,0,1,1,1,0,1,0,1,0,0]

print('ones',end='\t')
print('zeros')

for index,ele in enumerate(list,start=1):
  # First check if extra space OR new lines OR both are needed
  if index > 1:
    if ele==1:
      print()
    elif ele==0:
      if list[index-2]==1:
        print('', end=' \t')
      else:
        print('', end='\n\t\t')

  # THEN, write your desired output without any end
  if ele==1:
    print(index,ele,end="")
  elif ele==0:
    print(index,ele,end="")

# Finally an empty line
print()

It gives the following ouput:

ones    zeros
1 1     2 0
3 1
4 1
5 1     6 0
7 1     8 0
9 1     10 0
        11 0

As you can see, its limitation is that you can't go "up" and rewrite in old lines.

However, if you need to display EXACTLY as you've shown, you need to construct an intermediate data structure (for example a dict) and then display it using zip

Related