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