Python中3种内建数据结构:列表、元组和字典
Python中有3种内建的数据结构:列表、元组和字典。参考简明Python教程
1. 列表
list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个 序列 的项目。假想你有一个购物列表,上面记载着你要买的东西,你就容易理解列表了。只不过在你的购物表上,可能每样东西都独自占有一行,而在Python中,你在每个项目之间用逗号分割。
列表中的项目应该包括在方括号中,这样Python就知道你是在指明一个列表。一旦你创建了一个列表,你可以添加、删除或是搜索列表中的项目。由于你可以增加或删除项目,我们说列表是
可变的 数据类型,即这种类型是可以被改变的。
例:
输出
$python using_list.py
These items are: Linux Nginx MySQL PHP
add Apache.
list is now ['Linux', 'Nginx', 'MySQL', 'PHP', 'Apache']
I will sort my list now
Sorted list is ['Apache', 'Linux', 'MySQL', 'Nginx', 'PHP']
The first item Apache
delete first item
list is now ['Linux', 'MySQL', 'Nginx', 'PHP']
2. 元组
元组和列表十分类似,只不过元组和字符串一样是 不可变的
即你不能修改元组。元组通过圆括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。
例:
#!/usr/bin/env python
#coding:utf8
zoo = ('wolf', 'elephant', 'penguin')
print 'Number of animals in the zoo is', len(zoo)
new_zoo = ('monkey', 'dolphin', zoo)
print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]
输出
$ python using_tuple.py
Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are ('monkey', 'dolphin', ('wolf',
'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant',
'penguin')
Last animal brought from old zoo is penguin
3. 字典
字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿,即,我们把键(名字)和值(详细情况)联系在一起。注意,键必须是唯一的,就像如果有两个人恰巧同名的话,你无法找到正确的信息。
注意,你只能使用不可变的对象(比如字符串)来作为字典的键,但是你可以不可变或可变的对象作为字典的值。基本说来就是,你应该只使用简单的对象作为键。
键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2
}。注意它们的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。
记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对它们排序。
字典是dict类的实例/对象。
例:
#!/usr/bin/env python
#coding:utf8
contacts = { 'Admin' : 'admin@jb51.net',
'Linuxeye' : 'linuxeye@jb51.net',
'Support' : 'support@jb51.net'
}
print "Linuxeye's address is %s" % contacts['Linuxeye']
# Adding a key/value pair
contacts['test'] = 'test@jb51.net'
# Deleting a key/value pair
del contacts['Support']
print '\nThere are %d contacts in the address-book\n' %
len(contacts)
for name, address in contacts.items():
print 'Contact %s at %s' % (name, address)
if contacts.has_key('test'):
print "\ntest's address is %s" % contacts['test']
输出
$ python using_dict.py
Linuxeye's address is linuxeye@jb51.net
There are 3 contacts in the address-book
Contact Admin at admin@jb51.net
Contact test at test@jb51.net
Contact Linuxeye at linuxeye@jb51.net
test's address is test@jb51.net
相关文章
- python BDD 框架之lettuce
- 在Python的列表中利用remove()方法删除元素的教程
- python用reduce和map把字符串转为数字的方法
- Python 随机列表、集合、元组、字典的生成和排序
- python操作docker SDK:Docker SDK for Python
- Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Python3.6新特性2 Python2.7新特性Python 2.7的新特性 - 牛皮糖NewPtone - 博客园.html Python 3_x 新特性及10大变化_python_脚本之家.htm
- paip.python 执行shell 带空格命令行attilax总结
- Python编程语言学习:python语言中快速查询python自带模块&函数的用法及其属性方法、如何查询某个函数&关键词的用法、输出一个类或者实例化对象的所有属性和方法名之详细攻略
- Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Entry/Entry的Command)
- Python语言学习之双下划线那些事:python和双下划线使用方法之详细攻略
- 智能优化算法——模拟退火法(Python实现)
- 牛客华为机试第7题python
- 从零开始学习python | 实例讲解如何制作Python模式程序
- 【Python成长之路】python 基础篇 -- global/nonlocal关键字使用
- 【Python成长之路】python 基础篇 -- 装饰器【华为云分享】
- python中set()函数==》创建一个无序不重复的元素集(创将一个集合)
- Python编程:uWSGI配置报错ModuleNotFoundError: No module named 'flask'
- 【异常】前端ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.