Output Aligned Columns

Output aligned columns

In the class employee of print employee method:
Use this line to print.

cout << setw(20) << left << surname << setw(10) << left << empNumber << setw(4) << hourlyRate << endl;

You forgot to add "<< left". This is required if you want left aligned.

Hope it ll useful.

How to print two aligned columns of text from a list (which may have an odd number of elements) in python?

I tried to put it in a comment but it was too long so it became its own answer. To expand on the other answers, I thought it was a good idea to explain what the code is doing.

Breaking up the code into parts, and starting with j1-lee's answer, we have:

for i in range(0, len(my_list), 2):
print(' '.join(f"-- {s:10}" for s in my_list[i:i+2]))

or

output = '\n'.join(' '.join(f"-- {s:10}" for s in my_list[i:i+2]) for i in range(0, len(my_list), 2))

There are several parts:

  • f-strings are a handy way of formatting strings. The f before the quotes indicates an f-string.
  • Within the f-string there is a variable s that is part of the list comprehension for s in my_list[i:i+2]. {s:10} indicates that the length of the string is 10. (Or you could do something like what Bharel did and have another variable related to the length of the longest string and use that instead of 10.) This is probably a good idea if you plan to apply it to different lists.
  • (f"-- {s:10}" for s in my_list[i:i+2]) creates a generator, for example if you wrote x = (f"-- {s:10}" for s in my_list[0:0+2]) and then tried for i in x: print(i) your output would be two strings: -- Beef and -- Chicken.
  • the ' '.join(...) joins them together in a list, and like j1-lee says, if you nest them with multiple list comprehensions, you can get a single line.

I think you can probably get the most versatile by combining what j1-lee and Bharel did together. Note that I removed the space in the join command since there is now a variable that defines the space between the columns.

def generate_columns(list_in, n_columns, column_space, output_as_list = True):
# Figure our the length of the longest word in the list
longest_word_length = max(map(len,list_in))
column_length = longest_word_length + column_space

output = [''.join([f"-- {s:{column_length}}" \
for s in my_list[i:i+n_columns]]) for i in range(0, len(my_list), n_columns)]
if output_as_list:
return output
return '\n'.join(o for o in output)

my_list = ['Beef','Chicken','Eggs','Lamb','Nuts','Pork','Potatoes']

print(generate_columns(my_list, 3, 5))
#['-- Beef -- Chicken -- Eggs ', '-- Lamb -- Nuts -- Pork ', '-- Potatoes ']
print(generate_columns(my_list, 2, 3, output_as_list = False))
#-- Beef -- Chicken
#-- Eggs -- Lamb
#-- Nuts -- Pork
#-- Potatoes

Aligning columns in C output

Using \t leaves you at the mercy of your output device. Instead you could use minimum field widths for the strings, e.g. %-20s will print at least 20 characters, right-padding with spaces.

%-20.20s will truncate the string if it was longer; %-20s will bump everything else over to the right. The - means to left-justify (default is right-justify)


To avoid code duplication you could use a helper function, e.g.:

void print_item(char code, char const *abbrev, char const *description)
{
printf("%3d %7s %20.20s %#03x\n", code, abbrev, description, (unsigned char)code);
}

// ... in your function
if (c == '\n')
print_item(c, "\\n", "newline");

I modified the printf format string:

  • Using %20.20s as suggested above
  • %#03x, the # means it will prepend 0x for you
  • (unsigned char)code for the last one means it will behave nicely if you passed any negative chars in. (Typically chars range in value from -128 to 127).

How to align my results to look like columns

Use String#format or System.out.printf to generate formatted output, for example

    System.out.printf("%5s %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d%n", "", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

System.out.println("-------------------------------------------------");

// Nested For loops to build multiplication table
for (int number1 = 1; number1 <= 10; number1++) {
System.out.printf("%3d | ", number1);

for (int number2 = 1; number2 <= 10; number2++) {
System.out.printf("%3d ", (number1 * number2));
}
System.out.println(" | ");
}

Output...

        1   2   3   4   5   6   7   8   9  10
-------------------------------------------------
1 | 1 2 3 4 5 6 7 8 9 10 |
2 | 2 4 6 8 10 12 14 16 18 20 |
3 | 3 6 9 12 15 18 21 24 27 30 |
4 | 4 8 12 16 20 24 28 32 36 40 |
5 | 5 10 15 20 25 30 35 40 45 50 |
6 | 6 12 18 24 30 36 42 48 54 60 |
7 | 7 14 21 28 35 42 49 56 63 70 |
8 | 8 16 24 32 40 48 56 64 72 80 |
9 | 9 18 27 36 45 54 63 72 81 90 |
10 | 10 20 30 40 50 60 70 80 90 100 |

Take a look at Java For Complete Beginners - formatted strings for more details about the format qualifiers



Related Topics



Leave a reply



Submit