3

我正在做输入输出,我的任务的目标是按字母顺序打印名称,如果一个名称中包含 2 个以上的类,则将名称与两个类一起打印一次。

 joward 2302
 issac 2305
 issac 2245

输出应该是

issac 2305, 2305
joward 2302

所以这是我按名称按字母顺序创建的列表,然后是他们所在的类(名称重复)

['Adam', 'PHYS 1444', 'Ajoy', 'MATH 1426', 'Ajoy', 'CSE 2315', 'August', 'CSE 1320', 'August', 'CSE 2315', 'Chiao-Lin', 'PHYS 1443', 'Dylan', 'CSE 2315', 'Isis', 'CSE 3380', 'James', 'PHYS 1443', 'Jonathan', 'PHYS 1444', 'Jonathan', 'CSE 3380', 'Katherine', 'MATH 2325', 'Michael', 'CSE 1320', 'Randal', 'IE 3312', 'Saroj', 'PHYS 1443', 'Taesu', 'PHYS 1444', 'Taesu', 'CSE 2315', 'Taesu', 'CSE 3380', 'Timothy', 'CSE 3380', "Tre'Shaun", 'CSE 1320']

我的代码是

r = 0
while r < size - 2 :    
    if c[r] == c[r+2] :
        outstring = "%s, %s, %s\n" % (c[r],c[r+1],c[r+3]) #  
    else :                                                   
        outstring = "%s,%s\n" % (c[r], c[r+1])             

    outfile.write(outstring)
    r = r + 2

但是输出的文件是

Adam,PHYS 1444
Ajoy, MATH 1426, CSE 2315
Ajoy,CSE 2315
August, CSE 1320, CSE 2315
August,CSE 2315
Chiao-Lin,PHYS 1443
Dylan,CSE 2315
Isis,CSE 3380
James,PHYS 1443
Jonathan, PHYS 1444, CSE 3380
Jonathan,CSE 3380
Katherine,MATH 2325
Michael,CSE 1320
Randal,IE 3312
Saroj,PHYS 1443
Taesu, PHYS 1444, CSE 2315
Taesu, CSE 2315, CSE 3380
Taesu,CSE 3380
Timothy,CSE 3380

我的逻辑哪里出错了?

4

2 回答 2

2

AnOrderedDict在这里更合适,以名称为键:

>>> from collections import OrderedDict
>>> lis = ['Adam', 'PHYS 1444', 'Ajoy', 'MATH 1426', 'Ajoy', 'CSE 2315', 'August', 'CSE 1320', 'August', 'CSE 2315', 'Chiao-Lin', 'PHYS 1443', 'Dylan', 'CSE 2315', 'Isis', 'CSE 3380', 'James', 'PHYS 1443', 'Jonathan', 'PHYS 1444', 'Jonathan', 'CSE 3380', 'Katherine', 'MATH 2325', 'Michael', 'CSE 1320', 'Randal', 'IE 3312', 'Saroj', 'PHYS 1443', 'Taesu', 'PHYS 1444', 'Taesu', 'CSE 2315', 'Taesu', 'CSE 3380', 'Timothy', 'CSE 3380', "Tre'Shaun", 'CSE 1320']
>>> my_dict = OrderedDict()
>>> for name, marks in zip(*[iter(lis)]*2): 
        my_dict.setdefault(name, []).append(marks)
...     
>>> for k, v in my_dict.items():
...     print k, v
...     
Adam ['PHYS 1444']
Ajoy ['MATH 1426', 'CSE 2315']
August ['CSE 1320', 'CSE 2315']
Chiao-Lin ['PHYS 1443']
Dylan ['CSE 2315']
Isis ['CSE 3380']
James ['PHYS 1443']
Jonathan ['PHYS 1444', 'CSE 3380']
Katherine ['MATH 2325']
Michael ['CSE 1320']
Randal ['IE 3312']
Saroj ['PHYS 1443']
Taesu ['PHYS 1444', 'CSE 2315', 'CSE 3380']
Timothy ['CSE 3380']
Tre'Shaun ['CSE 1320']

如果输入列表中的名称未排序,则使用普通的dictdefaultdict(list)在迭代期间使用sorted

for k, v in sorted(my_dict.items()):
于 2013-10-27T06:54:36.037 回答
0
inp = ['Adam', 'PHYS 1444', 'Ajoy', 'MATH 1426', 'Ajoy', 'CSE 2315', 
'August', 'CSE 1320', 'August', 'CSE 2315', 'Chiao-Lin', 'PHYS 1443', 
'Dylan', 'CSE 2315', 'Isis', 'CSE 3380', 'James', 'PHYS 1443', 'Jonathan', 
'PHYS 1444', 'Jonathan', 'CSE 3380', 'Katherine', 'MATH 2325', 'Michael', 
'CSE 1320', 'Randal', 'IE 3312', 'Saroj', 'PHYS 1443', 'Taesu', 'PHYS 1444', 
'Taesu', 'CSE 2315', 'Taesu', 'CSE 3380', 'Timothy', 'CSE 3380', "Tre'Shaun", 
'CSE 1320']


tup_list = [tuple(ele.split()) for ele in inp]

_dict = {}

for ele in tup_list:
    if not ele[0] in _dict and len(ele) > 1:
        _dict[ele[0]] = [ele[1]] 
    elif ele[0] in _dict and len(ele) > 1:
        _dict[ele[0]].append(ele[1])
    elif len(ele) == 1:
        _dict[ele[0]] = []

print _dict
于 2013-10-27T07:07:44.123 回答