0

I am working on a data structure list class in python. I would like to get the largest item in the list.

 inlist = self.data
 if inlist[0] > inlist[1]:
 largest = inlist[0]
 else:
 largest = inlist[1]
 for item in inlist[2]:
 if item > largest:
 largest = item
 return largest

With the above getting stuck at largest gets returns

<bound method ListData.largest2 of <list.ListData instance at 0x2b35602c3440>>

while the data

[2, 5]
asked Feb 7, 2013 at 21:46
6
  • 2
    um, yeah, your list is only 2 items long, there is no inlist[2] Commented Feb 7, 2013 at 21:48
  • 3
    i guess you're trying to avoid using already built in functions? max(self.data) ? Commented Feb 7, 2013 at 21:53
  • 2
    Can you post a screenshot of the output you're getting? You normally see things like <blah hex-code for memory address> when you forget something silly, like forgetting the trailing parenthesis in a function that takes zero arguments. Commented Feb 7, 2013 at 22:08
  • 1
    You need to explain what your structure looks like. You're trying to iterate over the third item in self.data, so clearly you're expecting that to be a list or other iterable. So... what is it a list or iterable of? For example, is your structure in self.data something like (len, capacity, [item0, item1, ...])? Ideally, give us the actual class definition (stripped down to just enough to run a test that shows the problem), either posted here or somewhere like pastebin.com. Commented Feb 7, 2013 at 22:23
  • when i print out listdata.data it returns : [2, 5] Commented Feb 7, 2013 at 22:38

2 Answers 2

1

Trust the loop to get all the indicies rather than specifying them yourself.

if len(self.data) == 0:
 return None
result = self.data[0]
for item in self.data:
 if item > result:
 result = item
return result

that for loop is going through all your data. Trying to coerce an index got you into trouble.

answered Feb 7, 2013 at 21:50
Sign up to request clarification or add additional context in comments.

5 Comments

it still returns <bound method ListData.largest3 of <list.ListData instance at 0x2adc79695ef0>> <bound method ListData.largest4 of <list.ListData instance at 0x2adc79695ef0>>
Sounds like self.data isn't an array of numerics. What did you put in self.data?
def data(self): return self.data
doh! Change the word "largest" to "temp" wherever it occurs. I bet you defined and used largest as something else in a different part of your code.
I suspect you have (another) problem in the code not included here. Likely culprits include bad indentation on your def myFunction: line
0

You can use the build in max() function.

mylist = [2, 5]
mymax = max(mylist)
answered Feb 7, 2013 at 21:53

1 Comment

<bound method ListData.largest5 of <list.ListData instance at 0x2adc79695ef0>>

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.