我需要在我的函数中添加哪些代码行?

时间:2015-12-03 22:15:44

标签: python file

在if(i> 0)之后添加代码

obj1 = "Since i is greater than 0, lst[i-1] will become equal to the 'temp'/n"
wds = obj1.split()
file1.write(str(wds))

但是,我相当确定这是正确的。我需要做什么才能将我对分类过程的一部分的解释(obj1)写成文件名?

def sortAndExplain(lst, filename):
    """This function takes two inputs: LST, a list of values and 
    FILENAME, a string that is the name of the file that will be 
    edited. The function opens FILENAME, sorts LST, and writing 
    explanations and/or the current ordering of LST as the sorting 
    of LST proceeds."""
    file1 = open(filename, 'w')
    done = False        
    while not done:     
        i = len(lst) - 1           
        while ((i > 0) and (lst[i] >= lst[i-1])): 
            i = i - 1 
            """Add a line here writes the current ordering of LST 
            into FILENAME on one line. Each ordering should be on 
            its own line.""" 
            file1.write(str(lst))
        if (i > 0):  
            temp = lst[i]   
            lst[i] = lst[i-1]
            lst[i-1] = temp
            """Add a line here that writes an explanation of what 
            happened in this conditional statement into FILENAME on 
            one line. Each explanation should be on its own line.""" 
            obj1 = "Since i is > than 0, lst[i-1] will become equal to 'temp'/n"
            wds = obj1.split()
            file1.write(str(wds))
        else:              
            done = True  
    file1.close()

1 个答案:

答案 0 :(得分:0)

仅描述一行。我希望你需要描述整个块。这三行交换 lst [i] lst [i-1] 的值; temp 是该过程的附带条件。

如果您尚未识别高级进程,则这是一种称为“冒泡排序”的标准算法。你可以从那里搜索解释。维基百科将有一个很好的。