如何通过将索引名称作为标题来打印具有相同名称的带有换行符的索引:
我具有以下列表值,其中我有Art
,Science
和Geology
多行,我希望所有行都使用换行符分隔符以相同的索引值打印。 / p>
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
我很抱歉在第一级别上没有这么夸张。
答案 0 :(得分: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_k所述,如果lst
不正确,它将无法正常工作
按目标列排序,因此必须确保不是这种情况。
还按照注释中的建议用lambda
替换了operator.itemgetter
。
答案 1 :(得分:0)
以这种方式修改代码:
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
答案 2 :(得分:0)
要对数据进行分组:
function askDate(agent) {
const date1 = agent.parameters.date;
return admin.database().ref('date').transaction((date)=>
{
let askdate = date.askDate;
let activity = date.activity;
let askdate1 = date.twelfthAugust.askDate;
let activity1 = date.twelfthAugust.activity;
let askdate2 = date.fourthAugust.askDate;
let activity2 = date.fourthAugust.activity;
let askdate3 = date.ninthAugust.askDate;
let activity3 = date.ninthAugust.activity;
if (date1 == askdate)
{
agent.add("We have " + activity);
}
else if (date1 == askdate1)
{
agent.add("We have " + activity1);
}
else if (date1 == askdate2)
{
agent.add("We have " + activity2);
}
else if (date1 == askdate3)
{
agent.add("We have " + activity3);
}
else
{
agent.add("We do not have any events on this day!");
}
return date;
},
function (error, isSuccess) {
console.log('Success: ' + isSuccess);
});
}
要按预期输出:
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']
的示例输出。因此请尝试以下方式:
[ ..]
答案 3 :(得分:0)
这是一个解决方案:
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)
说明:
v2 =set(map(lambda x: x[2], v))
此行从数组中提取所有最后的不同值,将每个元素映射到其第三个组件,并将所有内容放入集合中以排除重复项
for val in v2:
v3 = list(filter(lambda x: x[2] == val, v))
print(v3)
我遍历集合中的元素,使用过滤器从原始数组中提取最后一个元素为实际元素的行,并将所有内容放入列表中。
我实际上并没有像您的示例中那样显示该元素,但这应该很简单。
答案 4 :(得分:0)
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]))
输出:
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']