Python学习笔记——基本数据结构
2013-07-25 20:52
567 查看
列表list
List是python的一个内置动态数组对象,它的基本使用方式如下:
shoplist = ['apple', 'mango', 'carrot', 'banana']
print
'I have', len(shoplist),'items to purchase.'
print
'These items are:', # Notice the comma at end of the line
for item in shoplist:
print item,
print
'\nI also have to buy rice.'
shoplist.append('rice')
print
'My shopping list is now', shoplist
print
'I will sort my list now'
shoplist.sort()
print
'Sorted shopping list is', shoplist
print
'The first item I will buy is', shoplist[0]
olditem = shoplist[0]
del shoplist[0]
print
'I bought the', olditem
print
'My shopping list is now', shoplist
元组Tuple
Python的typle通过小括号初始化,是一个只读对象。不过它的成员具有数组的访问方式。
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]
字典dict
Dict是一个查询式的数据结构,它的基本用法如下:
ab = { 'Swaroop' : 'swaroopch@byteofpython.info',
'Larry' : 'larry@wall.org',
'Matsumoto' : 'matz@ruby-lang.org',
'Spammer' : 'spammer@hotmail.com'
}
print
"Swaroop's address is %s" % ab['Swaroop']
# Adding a key/value pair
ab['Guido'] = 'guido@python.org'
# Deleting a key/value pair
del ab['Spammer']
print
'\nThere are %d contacts in the address-book\n' % len(ab)
for name, address in ab.items():
print
'Contact %s at %s' % (name, address)
if
'Guido'
in ab: # OR ab.has_key('Guido')
print
"\nGuido's address is %s" % ab['Guido']
List是python的一个内置动态数组对象,它的基本使用方式如下:
shoplist = ['apple', 'mango', 'carrot', 'banana']
'I have', len(shoplist),'items to purchase.'
'These items are:', # Notice the comma at end of the line
for item in shoplist:
print item,
'\nI also have to buy rice.'
shoplist.append('rice')
'My shopping list is now', shoplist
'I will sort my list now'
shoplist.sort()
'Sorted shopping list is', shoplist
'The first item I will buy is', shoplist[0]
olditem = shoplist[0]
del shoplist[0]
'I bought the', olditem
'My shopping list is now', shoplist
元组Tuple
Python的typle通过小括号初始化,是一个只读对象。不过它的成员具有数组的访问方式。
zoo = ('wolf', 'elephant', 'penguin')
'Number of animals in the zoo is', len(zoo)
new_zoo = ('monkey', 'dolphin', zoo)
'Number of animals in the new zoo is', len(new_zoo)
'All animals in new zoo are', new_zoo
'Animals brought from old zoo are', new_zoo[2]
'Last animal brought from old zoo is', new_zoo[2][2]
字典dict
Dict是一个查询式的数据结构,它的基本用法如下:
ab = { 'Swaroop' : 'swaroopch@byteofpython.info',
'Larry' : 'larry@wall.org',
'Matsumoto' : 'matz@ruby-lang.org',
'Spammer' : 'spammer@hotmail.com'
}
"Swaroop's address is %s" % ab['Swaroop']
# Adding a key/value pair
ab['Guido'] = 'guido@python.org'
# Deleting a key/value pair
del ab['Spammer']
'\nThere are %d contacts in the address-book\n' % len(ab)
for name, address in ab.items():
'Contact %s at %s' % (name, address)
if
'Guido'
in ab: # OR ab.has_key('Guido')
"\nGuido's address is %s" % ab['Guido']
相关文章推荐
- Python 2.7.11 基本语法和数据结构学习笔记
- python数据结构之列表基本操作[学习笔记]
- python学习笔记2之基本数据结构
- 数据结构基本概念 - 学习笔记
- nginx 学习笔记(三)基本数据结构
- 学习python笔记——数据结构
- Linux学习笔记--Python操作mysql数据库(封装基本的增删改查)
- 【原】Learning Spark (Python版) 学习笔记(一)----RDD 基本概念与命令
- 《OpenCV 3计算机视觉:Python语言实现》学习笔记——目标跟踪中基本运动检测的思考
- python 入门学习笔记之基本语法与基本数据类型
- python OpenCV学习笔记(六):图片的基本操作
- python数据结构学习笔记-4-链表
- python数据结构学习笔记-2017-01-08-01-N皇后问题、迷宫问题和跳马问题的递归解决
- python学习笔记之面向对象、基本I/O操作(一)
- Python学习笔记:05 数组的基本用法(Tuple和List)
- Python学习笔记(2):基本概念
- python 学习笔记(一)通过做题来熟悉python 的基本语法
- python数据结构学习笔记-2016-10-17-03-多维数组
- python数据结构学习笔记-2016-11-12-02-递归的应用
- nginx 学习笔记(三)基本数据结构