0

I'm working with a LIST that am getting from a soup which after the clean up gives me:
lists of variable length ['7.0', '7.5', '6.8', '6.9'] or ['7.0', '7.5']

so my problem is when I try formating the output:

code snippet:

if grades:
    print '\r\n["' + str(student[0]),
    if len(grades) == 1:
        print
        print ('%s'*len(grades)) % tuple(grades)

the output of this is:

["John Doe 7.0 7.5 6.8 6.9

however what I need is:
["John Doe, ["7.0","7.5"],["6.8","6.9"]] or ["John Doe, ["7.0","7.5"]]

I've tried with Formatter and Template and seems can't be done with those.

thank you

4
Contributors
5
Replies
33
Views
3 Years
Discussion Span
Last Post by David W
0

You will use something similar to the following (but this code is not complete), tutorial for for loop at Click Here explaination of list append and extend at Click Here

grades = ['7.0', '7.5', '6.8', '6.9']
for ctr in range(0, len(grades), 2):
    print grades[ctr], grades[ctr+1]

Edited by woooee

0

Tony's question was probably referring to

if len(grades) == 1:
    print ('%s'*len(grades)) % tuple(grades)

I would suggest that you test this further, i.e. if len(grades) == 1 then why multiply the output by len(grades) which is just one.

Edited by woooee

1

It appears

  • that you have an even number count in your lists of grades ...

and

  • you want the output to be formatted as a list of list pairs ... but with the name as the fist element in the outer list.

So ...

# nameGradePairsList.py #

grades = [ '4.5', '4.4', '3.9', '4.2', '3.8', '3.7' ]

grade_pair_lst = [ 'Sue Girl' ]
pair = []
for i, grd in enumerate(grades):
    if i % 2 == 0:
        pair.append( grd )
    else:
        pair.append( grd )
        grade_pair_lst.append( pair )
        pair = []

print( grade_pair_lst )

Edited by David W

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.