简体   繁体   English

将元素插入列表方法

[英]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 我知道到目前为止我所得到的是不正确的,但是如果我不明白自己所收到的消息,我将看不到我在做错什么。.BTW我不允许使用任何内置列表功能

]
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. 看起来问题只在于您的SortedList类没有定义打印它的方法。 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. print list_object ,甚至print SortedList()都会产生类似的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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