3

I have an array containing names,3 subjects and the grades for those 3 subjects. In total the data is:

[["Talha","Fazeel","Usayd","Mujtaba","Sufyan","Aukasha","Moiz","Mohid","Wasil"],
   ["Math","English","Science"],
   [10,8,7,4,3,7,8,9,8],[8,8,5,0,9,8,7,5,7],[7,6,4,2,4,3,5,7,9] ]

I want a program that print them in columns like this

the program I made is

Name=()
Subject=()
Marks=()
a=[["Talha","Fazeel","Usayd","Mujtaba","Sufyan","Aukasha","Moiz","Mohid","Wasil"],
   ["Math","English","Science"],
   [10,8,7,4,3,7,8,9,8],[8,8,5,0,9,8,7,5,7],[7,6,4,2,4,3,5,7,9] ]
for r in a:
    for c in r:
        print(c,end="")
    print()

the output it gives is too much mixed up:

TalhaFazeelUsaydMujtabaSufyanAukashaMoizMohidWasil
MathEnglishScience
1087437898
885098757
764243579

can anyone help me to sort it out?

5
  • 1
    What is your expected output? Commented Sep 3, 2020 at 11:31
  • you can copy the link of image and paste it in new tab it will open up... Commented Sep 3, 2020 at 11:31
  • Please post the sample output directly into your question rather than using links, images, or any indirect materials. Commented Sep 3, 2020 at 11:31
  • The expected output is that image Commented Sep 3, 2020 at 11:31
  • I'm new member and new members can't post pictures Commented Sep 3, 2020 at 11:32

3 Answers 3

1

First we separate the data from the array a:

names = a[0]
subjects = a[1]
grades = a[2:]

Next we print the first line along with a print() call to add a newline in the end:

print("Names", end=" ")
for i in range(len(subjects)):
    print(subjects[i], end="\t")
print()

Note the use of the end= "\t" parameter to add a tab after each subject \n after each print call

Next for each name we print the name and for each subject we print the name's grade:

for i in range(len(names)):
    print(names[i], end="\t")
    for j in range(len(subjects)):
            print(grades[j][i], end = "\t")
    print()

After each line we call print() to add a newline \n

Alternatively, for a little more complicated solution, you can use %s to format the string with extra spaces and right justify for a prettier output:

a=[["Talha","Fazeel","Usayd","Mujtaba","Sufyan","Aukasha","Moiz","Mohid","Wasil"],
   ["Math","English","Science"],
   [10,8,7,4,3,7,8,9,8],[8,8,5,0,9,8,7,5,7],[7,6,4,2,4,3,5,7,9] ]

names = a[0]
subjects = a[1]
grades = a[2:]

extra_spaces = 2
max_name_length = len(max(names, key=lambda item: len(item)))

first_col_indent = max_name_length + extra_spaces

print(f"%{first_col_indent}s" % "Names", end="")
for i in range(len(subjects)):
    print(f"%{len(subjects[i])+extra_spaces}s" % subjects[i], end="")

print()
for i in range(len(names)):
    print(f"%{first_col_indent}s" % names[i], end="")
    for j in range(len(subjects)):
            print(f"%{len(subjects[j])+extra_spaces}s" % grades[j][i], end = "")
    print()

Output:

Names      Math  English  Science
    Talha    10        8        7
   Fazeel     8        8        6
    Usayd     7        5        4
  Mujtaba     4        0        2
   Sufyan     3        9        4
  Aukasha     7        8        3
     Moiz     8        7        5
    Mohid     9        5        7
    Wasil     8        7        9

For more information on string formatting view this document

Sign up to request clarification or add additional context in comments.

Comments

1

You can format the cell

a=[["Talha","Fazeel","Usayd","Mujtaba","Sufyan","Aukasha","Moiz","Mohid","Wasil"],
   ["Math","English","Science"],
   [10,8,7,4,3,7,8,9,8],[8,8,5,0,9,8,7,5,7],[7,6,4,2,4,3,5,7,9] ]

row_format ="{:>20}"
for sub in ["Name"] + a[1]:
  print(row_format.format(sub), end = "")
print()
for i,student in enumerate(a[0]):
  print(row_format.format(student), end = "")
  for j in range(2, 5):
    print(row_format.format(a[j][i]), end = "")
  print()

OUTPUT:

       Name                Math             English             Science
       Talha                  10                   8                   7
      Fazeel                   8                   8                   6
       Usayd                   7                   5                   4
     Mujtaba                   4                   0                   2
      Sufyan                   3                   9                   4
     Aukasha                   7                   8                   3
        Moiz                   8                   7                   5
       Mohid                   9                   5                   7
       Wasil                   8                   7                   9

Comments

0

write your lists into a CSV file. It'll store your data, then you can retrieve it easily

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.