Python 列表顺序 [英] Python list order

查看:22
本文介绍了Python 列表顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我写的小脚本中,.append() 函数将输入的项目添加到列表的开头,而不是列表的末尾.(你可以清楚地理解,我对 Python 很陌生,所以放轻松吧)

<块引用>

list.append(x)
在列表末尾添加一个项目;等价于 a[len(a):] = [x].

https://docs.python.org/2/tutorial/datastructures.html.

你可以在下面看到我的代码:

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

我在这里误解了什么,因为打印函数打印 c,b,a 而不是 a,b,c(我输入的顺序是 a,b,c)

解决方案

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

当您的文本未完成" 时,您已经对其进行了编程,以便您立即再次调用该函数(即递归调用它).请注意,在执行 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']

这篇关于Python 列表顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆