Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding three arrays together to form one array in python

Tags:

python

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?

like image 374
Zoolto Avatar asked Feb 21 '26 15:02

Zoolto


1 Answers

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

like image 172
Alex Mandelias Avatar answered Feb 27 '26 10:02

Alex Mandelias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!