Newbie question regarding the list1.py exercise

170 views
Skip to first unread message

Terry Lake

unread,
Mar 17, 2015, 4:23:48 PM3/17/15
to python-g...@googlegroups.com
Hello all,

I was working thru the class exercises and ran into a problem with the third list1.py exercise that I could not find an answer for. To wit:
-- the problem --
# C. sort_last
# Given a list of non-empty tuples, return a list sorted in increasing
# order by the last element in each tuple.
# e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
# [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
# Hint: use a custom key= function to extract the last element form each tuple.

-- my code --
def sort_last(tuples):
  for s in tuples:
    def last(s): return s[-1]
    print sorted(s, key=last)
    print last(s)
  return

-- Output --

Traceback (most recent call last):
  File "/Users/Terry/Desktop/google-python-exercises/basic/list1.py", line 108, in <module>
    main()
  File "/Users/Terry/Desktop/google-python-exercises/basic/list1.py", line 99, in main
    test(sort_last([(1, 3), (3, 2), (2, 1)]),
  File "/Users/Terry/Desktop/google-python-exercises/basic/list1.py", line 65, in sort_last
    print sorted(s, key=last)
  File "/Users/Terry/Desktop/google-python-exercises/basic/list1.py", line 64, in last
    def last(s): return s[-1]
TypeError: 'int' object has no attribute '__getitem__'

The error appears to be a consequence of the inner definition e.g. def last(s): return s[-1] and the sorted(s, key=last).  I presume that what is returned from the inner def  is an integer value but do not understand why this is a problem or how to fix it.

Thanks in advance for any help.

Terry

Robert Mandić

unread,
Mar 18, 2015, 3:30:26 AM3/18/15
to python-g...@googlegroups.com
You're not returning anything from your function. You're trying to print stuff.

def sort_last(tuples):
  # +++your code here+++
  def last(t): return t[-1]
  return sorted(tuples,key=last)


--
You received this message because you are subscribed to the Google Groups "Python GCU Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-gcu-for...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Lp, Robert

Terry Lake

unread,
Mar 18, 2015, 3:00:55 PM3/18/15
to python-g...@googlegroups.com
Madic,

Thanks for the response. Sorry to inject a red herring into the discussion - the print statement was done as a test and has no impact on the type error.  See the code snippet below which has the print statement removed and shows the same type error being produced:

# C. sort_last

# Given a list of non-empty tuples, return a list sorted in increasing

# order by the last element in each tuple.

# e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields

# [(2, 2), (1, 3), (3, 4, 5), (1, 7)]

# Hint: use a custom key= function to extract the last element form each tuple.

def sort_last(tuples):

  for s in tuples:

    def last(s): return s[-1]

    sorted(s, key=last)

    print last(s)

  return

 

Traceback (most recent call last):

  File "/Users/Terry/Desktop/google-python-exercises/basic/list1.py", line 108, in <module>

    main()

  File "/Users/Terry/Desktop/google-python-exercises/basic/list1.py", line 99, in main

    test(sort_last([(1, 3), (3, 2), (2, 1)]),

  File "/Users/Terry/Desktop/google-python-exercises/basic/list1.py", line 65, in sort_last

    sorted(s, key=last)

Robert Mandić

unread,
Mar 18, 2015, 3:02:35 PM3/18/15
to python-g...@googlegroups.com

You're still returning None

--

Terry Lake

unread,
Mar 18, 2015, 6:57:21 PM3/18/15
to python-g...@googlegroups.com
Thanks for again for your patience.  After reviewing your original post more closely I noted that not only was I not returning a value in the main def but also I was sorting on the wrong object s as opposed to tuples.  

--
You received this message because you are subscribed to a topic in the Google Groups "Python GCU Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-gcu-forum/BRreTa6dtYU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-gcu-for...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages