我正在做输入输出,我的工作目标是按字母顺序打印名称,如果一个名字中有两个以上的类,请用两个类打印一次名称。
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
我的逻辑错在哪里?
答案 0 :(得分:2)
OrderedDict
在这里更合适,名称为键:
>>> 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']
如果输入列表中的名称未排序,则使用普通dict
或defaultdict(list)
,并在迭代期间使用sorted
:
for k, v in sorted(my_dict.items()):
答案 1 :(得分: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