1. 首页
  2. 编程语言
  3. Python
  4. Python中3种内建数据结构:列表、元组和字典

Python中3种内建数据结构:列表、元组和字典

上传者: 2022-06-25 21:08:02上传 PDF文件 75.24 KB 热度 12次

Python中有3种内建的数据结构:列表、元组和字典。参考简明Python教程列表中的项目应该包括在方括号中,这样Python就知道你是在指明一个列表。由于你可以增加或删除项目,我们说列表是 可变的 数据类型,即这种类型是可以被改变的。list is now ['Linux', 'Nginx', 'MySQL', 'PHP', 'Apache']. print 'Number of animals in the new zoo is', len. print 'Animals brought from old zoo are', new_zoo[2]. print 'Last animal brought from old zoo is', new_zoo[2][2]. Last animal brought from old zoo is penguin注意,你只能使用不可变的对象来作为字典的键,但是你可以不可变或可变的对象作为字典的值。键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2 }。注意它们的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。字典是dict类的实例/对象。print '\nThere are %d contacts in the address-book\n' % len

用户评论