How to get the entire line printed with Same Index name

  • A+
Category:Languages

How to print an index with a same name with a newline delimitted by priting the Index Name as a header:

i have below list values, where i have Art, Science and Geology having multiple lines i want all the lines get printed with a same index value with a newline separator.

file = open('student.txt') for line in file:     fields = line.strip().split()     print(fields) 

Below is processed as above

['Jullu', '18', 'Art'] ['sean', '25', 'Art'] ['Rubeena', '18', 'Science'] ['Kareen', '18', 'Science'] ['Rene', '18', 'Geology'] ['Babu', '18', 'Geology'] ['Riggu', '18', 'Robotics'] 

the output i would like:

Art     Jullu 18 Art     sean 25 Art  Science     Rubeena 18 Science     Kareen 18 Science 

More Exaplabation: The List output i have drawn above are the result processed from Bleow text file called, so we need to

$ cat student.text Jullu d18 Art seand d25 Art Rubeenad d18 Science Kareend d18 Science Rened d18 Geology Babud d18 Geology Riggud d18 Robotics 

My aopologies for not making it so claer at the first level.

 


Modify your code in this way:

file = open('student.txt') l=[] for line in file:     fields = line.strip().split()     print(fields)     l.append(fields) 

Now l is a list of list, and you can do so:

sub = [] for i in l:     sub.append(i[2]) sub = list(set(sub)) for i in sub:     print i     for index, j in enumerate(l):         if i in l[index][2]:             print '/t' + ' '.join(j) 

Output:

Science      Rubeena 18 Science      Kareen 18 Science Robotics      Riggu 18 Robotics Art      Jullu 18 Art      sean 25 Art Geology     Rene 18 Geology     Babu 18 Geology 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: