且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python 列表顺序

更新时间:2023-11-25 22:35:52

好的,这就是正在发生的事情.

当您的文本未完成" 时,您已经对其进行了编程,以便您立即再次调用该函数(即递归调用它).请注意,在执行 getting_text(raw_input("Enter the text or write done to finish enter ")) 行之后,您实际上是如何将它设置为将项目附加到列表中的.

所以基本上,当您添加变量时,它将在使用递归函数完成后添加所有变量.

因此,当您键入 a 时,它会再次调用该函数(尚未向列表输入任何内容).然后你输入b,然后c.当你输入 done 时,递归位就完成了.现在,它执行 user_input.append(....但是,顺序是相反的,因为它首先处理 c,因为这是最新的.

当您在函数内打印列表时可以显示:

>>>def Getting_text(entered_text):... 打印 user_input...如果输入文本==完成":...打印输入文本完成!"... 别的:... getting_text(raw_input("输入文本或写完成以完成输入"))... user_input.append(entered_text)...>>>>>>Getting_text(raw_input("输入第一个文本"))输入第一个文本a[]输入文本或写 done 完成输入 b[]输入文本或写 done 完成输入 c[]输入文本或写完成以完成输入完成[]输入文本完成!>>>用户输入['c', 'b', 'a']

注意打印语句第 2 行.

那么你如何解决这个问题?简单:在递归调用之前附加到列表中.

>>>用户输入 = []>>>def Getting_text(entered_text):...如果输入文本==完成":...打印输入文本完成!"... 别的:... user_input.append(entered_text)... getting_text(raw_input("输入文本或写完成以完成输入"))...>>>用户输入 = []>>>Getting_text(raw_input("输入第一个文本"))输入第一个文本a输入文本或写 done 完成输入 b输入文本或写 done 完成输入 c输入文本或写完成以完成输入完成输入文本完成!>>>用户输入['a', 'b', 'c']

In the small script I wrote, the .append() function adds the entered item to the beginning of the list, instead of the end of that list. (As you can clearly understand, am quite new to the Python, so go easy on me)

list.append(x)
Add an item to the end of the list; equivalent to a[len(a):] = [x].

That's what is says in https://docs.python.org/2/tutorial/datastructures.html.

You can see my code below:

user_input = []
def getting_text(entered_text):
    if entered_text == "done":
        print "entering the texts are done!"
    else:
        getting_text(raw_input("Enter the text or write done to finish entering "))
        user_input.append(entered_text)

getting_text(raw_input("Enter the first text "))
print user_input

Am I misunderstanding something here, because the print function prints c,b,a instead of a,b,c (the order I entered the input is a,b,c)

Ok, this is what's happening.

When your text isn't "done", you've programmed it so that you immediately call the function again (i.e, recursively call it). Notice how you've actually set it to append an item to the list AFTER you do the getting_text(raw_input("Enter the text or write done to finish entering ")) line.

So basically, when you add your variables, it's going to add all of the variables AFTER it's done with the recursive function.

Hence, when you type a, then it calls the function again (hasn't inputted anything to the list yet). Then you type b, then c. When you type done, the recursive bit is finished. NOW, it does user_input.append(.... HOWEVER, the order is reversed because it deals with c first since that was the latest thing.

This can be shown when you print the list inside the function:

>>> def getting_text(entered_text):
...     print user_input
...     if entered_text == "done":
...         print "entering the texts are done!"
...     else:
...             getting_text(raw_input("Enter the text or write done to finish entering "))
...             user_input.append(entered_text)
... 
>>> 
>>> getting_text(raw_input("Enter the first text "))
Enter the first text a
[]
Enter the text or write done to finish entering b
[]
Enter the text or write done to finish entering c
[]
Enter the text or write done to finish entering done
[]
entering the texts are done!
>>> user_input
['c', 'b', 'a']

Note the print statement line 2.


So how do you fix this? Simple: append to the list before you recursively call.

>>> user_input = []
>>> def getting_text(entered_text):
...     if entered_text == "done":
...         print "entering the texts are done!"
...     else:
...             user_input.append(entered_text)
...             getting_text(raw_input("Enter the text or write done to finish entering "))
... 
>>> user_input = []
>>> getting_text(raw_input("Enter the first text "))
Enter the first text a
Enter the text or write done to finish entering b
Enter the text or write done to finish entering c
Enter the text or write done to finish entering done
entering the texts are done!
>>> user_input
['a', 'b', 'c']