Python学习2

python中有三种内部数据结构:列表、元组、字典(using_list.py,using_tuple.py, using——dict.py)
list:列表。shoplist = ['apple', 'mango', 'carrot', 'banana']
方法,尾部添加shoplist.append('rice'),排序shoplist.sort(),删除del shoplist[i]

 1 # Filename: using_list.py
2 # This is my shopping list
3 shoplist = ['apple', 'mango', 'carrot', 'banana']
4 print 'i have', len(shoplist), 'items to choose'
5 print 'these items are:',
6 for item in shoplist:
7 print item,
8 print '\nI also have to buy rice'
9 shoplist.append('rice')
10 print 'my shoplist now is', shoplist
11 print 'i will sort my list now'
12 shoplist.sort()
13 print 'Sorted shopping list is', shoplist
14 print 'The first item i will buy is', shoplist[0]
15 olditem = shoplist[0]
16 del shoplist[0]
17 print 'i bought the', olditem
18 print 'My shopping list is now', shoplist
测试结果:
i have 4 items to choose
these items are: apple mango carrot banana
I also have to buy rice
my shoplist now is ['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']

 

tuple:元组。与列表类似,只是不能被修改。
zoo = ('wolf', 'elephant', 'penguin') new_zoo = ('monkey', 'dolphin', zoo)

元组在打印语句中应用:name = 'Mike' age = 22 print '%s is years old ' %(name, age)

1 # Filename: using_tuple.py
2 zoo = ('wolf', 'elephant', 'penguin')
3 print 'number of animals in the zoo is', len(zoo)
4 new_zoo = ('monkey', 'dolphin', zoo)
5 print 'number of animals in the new zoo is', len(new_zoo)
6 print 'all animals in the new zoo are', new_zoo
7 print 'Animals brought from old zoo are', new_zoo[2]
8 print 'last animal brought from old zoo is', new_zoo[2][2]
测试结果:
number of animals in the zoo is 3
number of animals in the new zoo is 3
all animals in the 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

 

dict:字典。由键值对构成,键不可改变,值可修改。 d = {key1 : value1, key2 : value2}
方法:ab.items(),ab.has_key('Guido'),删除del
items()方法返回一个元组的列表

 1 # Filename: using_dict.py
2 ab = {'Swaroop' : 'swaroopch@byteofpython.info',
3 'Larry' : 'larry@wall.org',
4 'Matsumoto' : 'matz@ruby-lang.org',
5 'Spammer' : 'spammer@hotmail.com'}
6 print "Swaroop's address is %s" %ab['Swaroop']
7
8 # Adding a key/value pair
9 ab['Guido'] = 'guido@python.org'
10 print '\nThere are %d contacts in the address-book\n' %len(ab)
11 for name, address in ab.items():
12 print 'Contact %s at %s' %(name, address)
13 if 'Guido' in ab: # ab.has_key('Guido')
14 print "\nGuido's address is %s" %ab['Guido']
测试结果:
Swaroop's address is swaroopch@byteofpython.info

There are 5 contacts in the address-book

Contact Swaroop at swaroopch@byteofpython.info
Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org
Contact Spammer at spammer@hotmail.com
Contact Guido at guido@python.org

Guido's address is guido@python.org

 

序列(seq.py)
元组、列表都是序列。共性:索引操作符、切片操作符
索引可以是负数,此时从尾端开始计数。
切片开始位置在序列切片位置中,结束位置被排除在外

 1 # FIlename: seq.py
2 shoplist = ['apple', 'mango', 'carrot', 'banana']
3 # Indexing or Subscriprion operation
4 print 'Item 0 is', shoplist[0]
5 print 'Item 1 is', shoplist[1]
6 print 'Item 2 is', shoplist[2]
7 print 'Item 3 is', shoplist[3]
8 print 'Item -1 is', shoplist[-1]
9 print 'Item -2 is', shoplist[-2]
10
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
17 # slicing on a string
18 name = 'swaroop'
19 print 'characters 1 to 3 is', name[1:3]
20 print 'characters 2 to end is', name[2:]
21 print 'characters 1 to -1 is', name[1:-1]
22 print 'characters start to end is', name[:]
测试结果:
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

 

参考(类似c#引用)(reference.py)
mylist = shoplist, 引用
mylist = shoplist[:], 完全复制

 1 # Filename: reference.py
2 print 'Simple Assignment'
3 shoplist = ['apple', 'mango', 'carrot', 'banana']
4 # mylist is just another name pointed to the same object!
5 mylist = shoplist
6 del shoplist[0]
7 print 'shoplist is', shoplist
8 print 'mylist is', mylist
9 # make a copy by doing a full slice
10 mylist = shoplist[:]
11 del mylist[0]
12 print 'shoplist is', shoplist
13 print 'mylst is', mylist
测试结果:
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
shoplist is ['mango', 'carrot', 'banana']
mylst is ['carrot', 'banana']

 

字符串str类的方法(str_methods.py)
startwith():测试字符串是否以指定字符串开始
in:测试字符串是否含有指定字符串
find():返回指定字符串在字符串中的位置,没有则返回-1
join():可作为序列的连接字符串

 1 # Filename: str_methods.py
2 name = 'Swaroop'
3 if name.startswith('Swa'):
4 print 'Yes, the string strats with "Swa"'
5 if 'a' in name:
6 print 'Yes, it contains the string "a"'
7 if name.find('war') != -1:
8 print 'Yes, it contains the string "war"'
9 delimiter = '_*_'
10 mylist = ['Brazil', 'Russia', 'India', 'China']
11 print delimiter.join(mylist)
测试结果:
Yes, the string strats with "Swa"
Yes, it contains the string "a"
Yes, it contains the string "war"
Brazil_*_Russia_*_India_*_China




posted @ 2012-03-17 14:05  Alex_Monkey  阅读(1593)  评论(0编辑  收藏  举报