Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Logic Error ( Orginizing list)

Tags:

python

im doing input output and im the goal of my assignment is to print names in alphabetical order, and if one name has more than 2 classes in it, print the name once with both classes ex..

 joward 2302
 issac 2305
 issac 2245

output should be

issac 2305, 2305
joward 2302

so heres my list that i created in alphabetical order by name, followed by the class their in (names repeat)

['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 code is

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

however the file outputed is

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

where is my logic going wrong?

like image 871
Zach Santiago Avatar asked Mar 03 '26 05:03

Zach Santiago


1 Answers

An OrderedDict would be more suitable here, with names as keys:

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

If the names in input list are not sorted then use a normal dict or defaultdict(list) and during iteration use sorted:

for k, v in sorted(my_dict.items()):
like image 161
Ashwini Chaudhary Avatar answered Mar 04 '26 20:03

Ashwini Chaudhary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!