python学习---字典顺序问题
看到很多关于Python介绍的文章,说它功能很强大,代码写起来又简单。于是今天就打算学习了一下。在使用字典的时候有点小问题。
eg:
[gan@localhost py]$ cat my_dict.py
#!/usr/bin/python# Filename: my_dict.pyvdict={ "jack@hotmail.com":"newworld", "tom@hotmail.com":"second", "abc@163.com":"123456", "zwp@126.com":"kown123" }for email, pwd in vdict.items(): print "Email: %s password: %s" %(email, pwd)print "Done"
[gan@localhost py]$ my_dict.py
Email: jack@hotmail.com password: newworld
Email: zwp@126.com password: kown123
Email: abc@163.com password: 123456
Email: tom@hotmail.com password: second
Done
看看怎么输出的顺序是这样的呢?和我输入的不是一样的阿? 我也没搞清楚为什么是这样的,但我想按照我输入的顺序来输出怎么作呢?
想到元组了:
[gan@localhost py]$ cat my_dict.py
#!/usr/bin/python# Filename: my_dict.pyvdict=( ("jack@hotmail.com", "newworld"), ("tom@hotmail.com" , "second" ), ("abc@163.com" , "123456" ), ("zwp@126.com" , "kown123" ) )for i in range(0, len(vdict)): print "Email: %s password: %s" %(vdict[0], vdict[1])print "Done"
[gan@localhost py]$ my_dict.py
Email: jack@hotmail.com password: newworld
Email: tom@hotmail.com password: second
Email: abc@163.com password: 123456
Email: zwp@126.com password: kown123
Done
python用起来还不错,继续学习。是个很有趣的语言。还有好多问题不知道哦!