如何使整行打印相同的索引

2024-04-19 04:09:13 发布

您现在位置:Python中文网/ 问答频道 /正文

如何打印具有相同名称的索引,并通过将索引名称作为标题分隔换行:

我有下面的列表值,其中有ArtScienceGeology有多行,我希望所有的行都用一个带换行符的索引值打印出来。你知道吗

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

以下按上述方式处理

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

我想要的输出:

Art
    Jullu 18 Art
    sean 25 Art

Science
    Rubeena 18 Science
    Kareen 18 Science

更夸张的是:我上面画的列表输出是从Bleow文本文件处理的结果,所以我们需要

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

我的道歉没有使它在一级如此突出。你知道吗


Tags: 名称fields列表linestudentfilesciencesean
3条回答

您可以使用itertools.groupby按 索引名称

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("")

编辑:

正如@tobias\u k所指出的,如果lst未被激活,它将无法正常工作 按目标列排序,因此必须确保不是这样。你知道吗

如注释中所示,还将lambda替换为operator.itemgetter。你知道吗

要对数据进行分组:

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)

要按预期输出:

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']

作为没有[ ..]的示例输出。所以请尝试以下方法:

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

按以下方式修改代码:

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

现在l是一个列表列表,您可以这样做:

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)

输出:

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

相关问题 更多 >