Please let me know if I should make something more clear

1. Each item in list contains a tuple with its name -> ((value, work), 'name')

2. the function "sort" below sorts the list of subjects by "value" in descending order

3. I get: TypeError: 'int' object is unsubscriptable (occurs after looping a few times)

It shouldn't be the logic of the sorting method as it works elsewhere

Could the problem be due to how the list is created? (see under sort function)

def sortj(list):
        for i in range(1, len(list)):
            value = list[ i ][ 0 ][ 0 ]	## integer
            j = i - 1
            done = False
            while not done:
                if value > list[ j ][ 0 ][ 0 ]: 	# error occurs ( list [ j ] [ 0 ] [ 0 ] is the problem)
                    list[ j + 1 ] = list[ j ]   
                    j -= 1
                    if j < 0:
                        done = True
                else:
                    done = True
            list[ j + 1 ] = value

after list is created by the following process, the sorting above is attempted
(Why does the first item appear to be an empty list in the finished product of "sortedj" ?)

# this is gathering the tuples that contain j amount of work from a list of subjects in a dictionary

for j in workCaps: # the range of max work
        sortedj = [] 
        for i in subIndexes:	
            if j <= maxWork:	
                if subjects[names[i]][1] == j: ## if work = 
                    sortedj.append((subjects[names[i]], names[i]))

Thanks for any help

Edited 5 Years Ago by old_kid: n/a

Top block of code, line 1: please don't use the built in type list as a parameter name. Use, maybe jlist or somelist Inside the sortj body, at the top line, do something like: print("\n".join((str(x) for x in jlist[:3]))) Where you need to pick the '3' to be a big enough value to show you the problem item... and I"ve taken the liberty to rename the function's parameter to jlist If that doesn't show the problem, please post back, showing that data.

Note: You might want to just use the built in function sorted making sure you pass key=somefunction to extract the actual key from the value.

Edited 5 Years Ago by griswolf: n/a

Thank you for your response.

Below is jlist printed in its entirety. The names are evidence of extracting the key from value properly. Otherwise, I'm sorry to say that I don't understand the sorted method in this context.

((7, 1), '7.16')
((10, 1), '7.17')
((6, 1), '24.12')
((2, 1), '12.03')
((7, 1), '12.04')
((7, 1), '15.01')
((10, 1), '6.00')
((4, 1), '7.06')
((7, 1), '7.00')
((1, 1), '7.09')
((4, 1), '8.08')
((1, 1), '7.08')
((2, 1), '2.06')
((1, 1), '2.07')
((6, 1), '2.03')

However, when I simply print jlist (as opposed to using a for loop), there is shown to be an empty list at the beginning, which may have prevented me from referencing jlist[integer]. I can't, However, delete jlist[0]

# mysterious list brackets (when jlist is printed with no for loop)
"[]
[((7, 1), '7.16')...]"

"in sortj...
if value > l[j][0][0]:
TypeError:..."

Edited 5 Years Ago by old_kid: n/a

The problem is in your line 14: you are replacing((x,y),z) with a different x. Then, later you attempt to extract the key from that single value. Oops.

Try this?

def sortj(alist):
  return sorted(alist,key=lambda x: x[0][0])

which does not do the sort in place as your sortj seems to attempt to do... though I admit I'm not sure exactly what the intention of your code is. With the key argument, order is retained when the initial number is the same, without it, the list is sorted using the entire tuple as a key, items with equal first numbers are re-ordered.

Edited 5 Years Ago by griswolf: n/a

This question has already been answered. Start a new discussion instead.