1

I have a class with 2 attributes which are lists themselves:

class data...:
    list1 = [["g1", 2.0], ["x1", 3.0]...] # n elements
    list2 = [[2, 4, 5],[3, 2, 1]...] # n elements

I need to zip sort both lists, based on value of the second element of list2.

zipped = zip(dataobj.list1, dataobj.list2)
zipped.sort(cmp = lambda k: dataobj.list2[2])

This seems to not work.

How do I reference the second element of dataobj.list2[2] as this is not working and gave me the following error:

TypeError: <lambda>() takes exactly 1 argument (2 given)
2
  • zipped.sort(key = lambda k: k[1][1])? Commented Aug 10, 2016 at 6:34
  • zipped.sort(key = lambda k: k[1]) Commented Aug 10, 2016 at 6:41

1 Answer 1

2

cmp should be a reference to a function that compares two values. Instead, you need something much simpler - a key field.

The easiest way would be to reference the value directly from zipped instead of going back to the original value in list2. Note, BTW, that lists in python are zero-based, so the second element would be [1], not [2]. To make a long story short:

zipped.sort(key = lambda k : k[1][1])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.