看到很多关于python介绍的文章,说它功能很强大,代码写起来又简单。于是今天就打算学习了一下。在使用字典的时候有点小问题。
eg:
[gan@localhost py]$ cat my_dict.py
#!/usr/bin/python
# filename: my_dict.py
vdict={
"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.py
vdict=(
("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[i][0], vdict[i][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用起来还不错,继续学习。是个很有趣的语言。还有好多问题不知道哦!
元组,列表,字典的区别:
阅读(5731) | 评论(1) | 转发(0) |