cfmonkey的笔记本

Python笔记9 : 数据结构

python中有三种内建的数据结构-----列表(List), 元组(Tuple), 字典(Dict)

List

example:

#!/usr/bin/python
# Filename: using_list.py
# This is my shopping list

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 
shoplist.remove("apple") #will throw a exception like the red words
print shoplist
shoplist.pop()
print shoplist

output:

$ python using_list.py
I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is now ['banana', 'carrot', 'mango', 'rice']
Traceback (innermost last): File "<interactive input>", line 1, in ? ValueError: list.remove(x): x not in list
'rice'
['banana', 'carrot', 'mango']

  1. 可以在列表中添加任何类型的数据. 使用append()函数.
  2. List是一个可变结构, 即可以改变列表内容和长度等.
  3. 使用for...in....在列表中各项目间递归.
  4. 我们在print语句的结尾使用了一个 逗号(comma) 来消除每个print语句自动打印的换行符。
  5. 使用del删除列表项. Python从0开始计数.
  6. remove 仅仅 删除一个值的首次出现。 在这里, 'new' 在 list 中出现了两次, 但 li.remove("new") 只删除了 'new' 的首次出现。
  7. 如果在 list 中没有找到值, Python 会引发一个异常来响应 index 方法。
  8. pop 是一个有趣的东西。它会做两件事: 删除 list 的最后一个元素, 然后返回删除元素的值。请注意, 这与 li[-1] 不同, 后者返回一个值但不改变 list 本身。也不同于 li.remove(value), 后者改变 list 但并不返回值。
  9. help(list)

list的运算符:

Lists 也可以用 + 运算符连接起来。 list = list + otherlist 相当于 list.extend(otherlist)。 但 + 运算符把一个新 (连接后) 的 list 作为值返回, 而 extend 只修改存在的 list。 也就是说, 对于大型 list 来说, extend 的执行速度要快一些。
Python 支持 += 运算符。 li += ['two'] 等同于 li.extend(['two'])+= 运算符可用于 list, 字符串和整数, 并且它也可以被重载用于用户自定义的类中.
* 运算符可以作为一个重复器作用于 list。 li = [1, 2] * 3 等同于 li = [1, 2] + [1, 2] + [1, 2], 即将三个 list 连接成一个。

Tuple

DIct

  1. Dict没有sort()方法, 建立数据的时候需要自己进行排序.
  2. Dict的key是大小写敏感的. e.g
    >>> d = {}
    >>> d["key"] = "value"
    >>> d["key"] = "other value"
    >>> d
    {'key': 'other value'}
    >>> d["Key"] = "third value"
    >>> d
    {'Key': 'third value', 'key': 'other value'}
  3. Dict的删除
    >>> del d[...]
    >>> d.clear()
  4. 可以通过 key 来引用其值, 但是不能通过值获取 key。所以 d["server"] 的值为 'mpilgrim', 而使用 d["mpilgrim"] 会引发一个异常, 因为 'mpilgrim' 不是一个 key。e.g
    >>> d = {"server":"mpilgrim", "database":"master"}
    >>> d
    {'server': 'mpilgrim', 'database': 'master'}
    >>> d["database"]                                 
    'master'
    >>> d["mpilgrim"]                                 
    Traceback (innermost last):
      File "<interactive input>", line 1, in ?
    KeyError: mpilgrim

posted on 2007-07-17 22:38 cfmonkey 阅读(460) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2010年12月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

常用链接

留言簿(2)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜