user10029838
user10029838

Reputation:

How to get the entire line printed with Same Index name

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.

Upvotes: 6

Views: 166

Answers (5)

Joe
Joe

Reputation: 12417

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

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71610

l=[['Jullu', '18', 'Art'],
['sean', '25', 'Art'],
['Rubeena', '18', 'Science'],
['Kareen', '18', 'Science'],
['Rene', '18', 'Geology'],
['Babu', '18', 'Geology'],
['Riggu', '18', 'Robotics']]
l=[[x for x in l if x[-1]==i] for i in list(set(list(zip(*l))[-1]))]
for i in l:
   print(i[0][-1])
   print('\n'.join(['\t'+str(x) for x in i]))

Output:

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

Upvotes: 0

taras
taras

Reputation: 6915

You can use itertools.groupby to aggregate your lists by the Index Name

import itertools
import operator

lst = [

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

]

key = operator.itemgetter(2)
# this step is only required if the list is not sorted by the key
lst.sort(key=key)  

for index, values in itertools.groupby(lst, key):
    print(index)
    for value in values:
        print("    " + " ".join(value))
    print("")

Edit:

As noted by @tobias_k, it will not work properly if lst is not sorted by the target column, so you have to make sure it is not the case.

Also replaced lambda with operator.itemgetter as suggested in comments.

Upvotes: 3

bracco23
bracco23

Reputation: 2221

This is a solution:

v = [
     ['Jullu', '18', 'Art'],
     ['sean', '25', 'Art'],
     ['Rubeena', '18', 'Science'],
     ['Kareen', '18', 'Science'],
     ['Rene', '18', 'Geology'],
     ['Babu', '18', 'Geology'],
     ['Riggu', '18', 'Robotics']
 ]
 v2 =set(map(lambda x: x[2], v))
 for val in v2:
     v3 = list(filter(lambda x: x[2] == val, v))
     print(v3)

Explanaition:

 v2 =set(map(lambda x: x[2], v))

This line extracts all the last different values from the array, mapping every element to its third component and putting everything into a set to exclude duplicates

 for val in v2:
     v3 = list(filter(lambda x: x[2] == val, v))
     print(v3)

I iterate over the elements of the set, use a filter to extract from the original array the lines where the last element is the actual one and put everything into a list.

I didn't actually display the element as in your example but that should be trivial.

Upvotes: 0

Frank AK
Frank AK

Reputation: 1781

To group your data:

In [72]: from collections import defaultdict

In [73]: category_to_list = defaultdict(list)

In [81]: origin_data = [
    ...: ['Jullu', '18', 'Art'],
    ...: ['sean', '25', 'Art'],
    ...: ['Rubeena', '18', 'Science'],
    ...: ['Kareen', '18', 'Science'],
    ...: ['Rene', '18', 'Geology']]

In [82]: category_to_list = defaultdict(list)

In [83]: for i in origin_data:
    ...:     category_to_list[i[2]].append(i)

To output as your expect:

In [85]: for cate, lst in category_to_list.items():
    ...:     print(cate)
    ...:     for i in lst:
    ...:         print('\t{}'.format(i))
    ...:         
Science
    ['Rubeena', '18', 'Science']
    ['Kareen', '18', 'Science']
Art
    ['Jullu', '18', 'Art']
    ['sean', '25', 'Art']
Geology
    ['Rene', '18', 'Geology']

As your sample output without [ ..]. so try below way:

In [86]: for cate, lst in category_to_list.items():
    ...:     print(cate)
    ...:     for i in lst:
    ...:         print('\t{}'.format(' '.join(i)))
    ...:         
Science
    Rubeena 18 Science
    Kareen 18 Science
Art
    Jullu 18 Art
    sean 25 Art
Geology
    Rene 18 Geology

Upvotes: 0

Related Questions