Chinaunix首页 | 论坛 | 博客
  • 博客访问: 141241
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 309
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-06 11:27
个人简介

开启暴走模式。

文章分类

全部博文(31)

文章存档

2017年(19)

2016年(1)

2015年(11)

我的朋友

分类: Python/Ruby

2015-08-28 12:11:30

python中,有3种内建的数据结构:列表、元组和字典。
1.列表

list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。列表中的项目。列表中的项目应该包括在方括号中,这样 python就知道你是在指明一个列表。一旦你创建了一个列表,你就可以添加,删除,或者是搜索列表中的项目。由于你可以增加或删除项目,我们说列表是可 变的数据类型,即这种类型是可以被改变的。列表是可以嵌套的。

例子:

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. # Filename: using_list.py

  3. # This is my shopping list
  4. shoplist = ['apple', 'mango', 'carrot', 'banana']

  5. print 'I have', len(shoplist),'items to purchase.'

  6. print 'These items are:', # Notice the comma at end of the line
  7. for item in shoplist:
  8.     print item,

  9. print '\nI also have to buy rice.'
  10. shoplist.append('rice')
  11. print 'My shopping list is now', shoplist

  12. print 'I will sort my list now'
  13. shoplist.sort()
  14. print 'Sorted shopping list is', shoplist

  15. print 'The first item I will buy is', shoplist[0]
  16. olditem = shoplist[0]
  17. del shoplist[0]
  18. print 'I bought the', olditem
  19. print 'My shopping list is now', shoplist

2.元组

元祖和列表十分相似,不过元组是不可变的。即你不能修改元组。元组通过圆括号中用逗号分隔的项目定义。元组通常用在使语句或用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变。元组可以嵌套。


点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. # Filename: using_tuple.py

  3. zoo = ('wolf', 'elephant', 'penguin')
  4. print 'Number of animals in the zoo is', len(zoo)

  5. new_zoo = ('monkey', 'dolphin', zoo)
  6. print 'Number of animals in the new zoo is', len(new_zoo)
  7. print 'All animals in new zoo are', new_zoo
  8. print 'Animals brought from old zoo are', new_zoo[2]
  9. 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

元组与打印语句:
元组最通常的用法是用在打印语句中。

例如:

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. # Filename: print_tuple.py

  3. age = 22
  4. name = 'Swaroop'

  5. print '%s is %d years old' % (name, age)
  6. print 'Why is %s playing with that python?' % name

print语句可以使用跟着%符号的项目元组的字符串。这些字符串具备定制的功能。定制让输出满足某种特定的格式。定制可以是%s表示字符串或%d表示整数。元组必须按照相同的顺序来对应这些定制。
3.字典
字典类似于你通过联系人名称查找地址和联系人详细情况的地址簿,即,我们把键(名字)和值(详细情况)联系在一起。注意,键必须是唯一的,就像如果有两个人恰巧同名的话,你无法找到正确的信息。键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2 }。注意它们的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。另外,记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺 序,那么你应该在使用前自己对它们排序。

例子:

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. # Filename: using_dict.py

  3. # 'ab' is short for 'a'ddress'b'ook

  4. ab = { 'Swaroop' : 'swaroopch@byteofpython.info',
  5.              'Larry' : 'larry@wall.org',
  6.              'Matsumoto' : 'matz@ruby-lang.org',
  7.              'Spammer' : 'spammer@hotmail.com'
  8.      }

  9. print "Swaroop's address is %s" % ab['Swaroop']

  10. # Adding a key/value pair
  11. ab['Guido'] = 'guido@python.org'

  12. # Deleting a key/value pair
  13. del ab['Spammer']

  14. print '\nThere are %d contacts in the address-book\n' % len(ab)
  15. for name, address in ab.items():
  16.     print 'Contact %s at %s' % (name, address)

  17. if 'Guido' in ab: # OR ab.has_key('Guido')
  18.     print "\nGuido's address is %s" % ab['Guido']
序列:
列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符和切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。

使用序列:

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. # Filename: seq.py

  3. shoplist = ['apple', 'mango', 'carrot', 'banana']

  4. # Indexing or 'Subscription' operation
  5. print 'Item 0 is', shoplist[0]
  6. print 'Item 1 is', shoplist[1]
  7. print 'Item 2 is', shoplist[2]
  8. print 'Item 3 is', shoplist[3]
  9. print 'Item -1 is', shoplist[-1]
  10. print 'Item -2 is', shoplist[-2]

  11. # Slicing on a list
  12. print 'Item 1 to 3 is', shoplist[1:3]
  13. print 'Item 2 to end is', shoplist[2:]
  14. print 'Item 1 to -1 is', shoplist[1:-1]
  15. print 'Item start to end is', shoplist[:]

  16. # Slicing on a string
  17. name = 'swaroop'
  18. print 'characters 1 to 3 is', name[1:3]
  19. print 'characters 2 to end is', name[2:]
  20. print 'characters 1 to -1 is', name[1:-1]
  21. print 'characters start to end is', name[:]
输出:
$ python seq.py
Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop

参考
当你创建一个对象并给它赋一个变量的时候,这个变量仅仅 参考 那个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被称作名称到对象的绑定。一般说来,你不需要担心这个,只是在参考上有些细微的效果需要你注意。

示例:

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. # Filename: reference.py

  3. print 'Simple Assignment'
  4. shoplist = ['apple', 'mango', 'carrot', 'banana']
  5. mylist = shoplist # mylist is just another name pointing to the same

  6. del shoplist[0]

  7. print 'shoplist is', shoplist
  8. print 'mylist is', mylist
  9. # notice that both shoplist and mylist both print the same list without
  10. # the 'apple' confirming that they point to the same object

  11. print 'Copy by making a full slice'
  12. mylist = shoplist[:] # make a copy by doing a full slice
  13. del mylist[0] # remove first item

  14. print 'shoplist is', shoplist
  15. print 'mylist is', mylist
  16. # notice that now the two lists are different
输出结果:
$ python reference.py
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist is ['mango', 'carrot', 'banana']
mylist is ['carrot', 'banana']
阅读(1220) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~