简体   繁体   中英

Insert element into a list method

I'm trying to insert a new item into a sorted list, entering the item in the corrected (Sorted) position in the list. Everytime i test my code to see if it's working or not though, i'm getting a message that i'm not really familiar with. i know what i have so far isn't correct, but i'm not able to see what i'm doing wrong if i don't understand the message that i'm getting.. BTW i'm not allowed to use any built-in list functions

]
Your answer: <__main__.SortedList object at 0x1681c10>

What i have so far:

class SortedList:
    def __init__(self):
        self.L = []

    def insert(self, item):
        data= []
        for j in range(len(self.L)):
           data.append(self.L[j])
        return (data)

Looks like the problem is just that your SortedList class isn't defining a way to print it. Try adding:

def __str__(self):
    return str(self.L)

See this for a discussion of special methods .

At some point in the code, possibly in the wrapper code that your instructor provides is a piece of code that looks like the following:

list_object = SortedList()
print list_object

print list_object , or even print SortedList() will produce output that looks like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM