首页 > 代码库 > Python开发(二)

Python开发(二)

python提供三种内置的数据结构:

列表 元组 字典

一.列表(你可以对列表进行查,插,删,改)

#!/usr/bin/python
#filename:using_list.py
shoplist=[‘apple‘,‘mango‘,‘carrot‘,‘banana‘]
print ‘I have‘,len(shoplist),‘items to purchase‘
print ‘These item sare:‘,
for item in shoplist:
    print item,‘\n‘
shoplist.append(‘rice‘)
print shoplist
shoplist.sort()
print ‘\n‘,shoplist
print ‘\n‘,shoplist[0]
del shoplist[0]
print shoplist
                                                                     
~                                                                     
"using_list.py" 14L, 333C written                   
[root@~]# python using_list.py
I have 4 items to purchase
These item sare: apple 

mango 

carrot 

banana 

[‘apple‘, ‘mango‘, ‘carrot‘, ‘banana‘, ‘rice‘]

[‘apple‘, ‘banana‘, ‘carrot‘, ‘mango‘, ‘rice‘]

apple
[‘banana‘, ‘carrot‘, ‘mango‘, ‘rice‘]

二.元组(如同字符串,不可修改)

print ‘%s is %d years old‘%(name,age)

#!/usr/bin/python
#filename:print_tuple.py
age=22
name=‘Swaroop‘
print ‘%s is %d years old‘%(name,age)
print ‘Why is %s playing with that python?‘%name
~                                                                                                                                        
"print_tuple.py" [New] 6L, 152C written             
[root@ ~]# python print_tuple.py
Swaroop is 22 years old
Why is Swaroop playing with that python?

三.字典

if ‘Guido‘ in ab:

    print "%s"% ab[‘Guido‘]

#!/usr/bin/python
#filename:using_dict.py
ab={‘Swaroop‘:‘swaroopch@byteofpython.info‘,
    ‘Larry‘:‘larry@wall.org‘,
    ‘Matsumoto‘:‘matz@ruby-lang.org‘,
    ‘Spammer‘:‘spammer@hotmai.com‘
}
print "Swaroop‘s address is %s ab[‘Swaroop‘]"
ab[‘Guido‘]=‘guido@python.org‘
del ab[‘Spammer‘]
print len(ab)
for name,address in ab.items():
    print ‘Contact %s at A%s‘%(name,address)
if ‘Guido‘ in ab:
    print "\nGuido‘s address is %s"% ab[‘Guido‘]                                                                     
~                                                                     
"using_dict.py" [New] 15L, 445C written             
[root@~]# python using_dict.py
Swaroop‘s address is %s ab[‘Swaroop‘]
4
Contact Swaroop at Aswaroopch@byteofpython.info
Contact Matsumoto at Amatz@ruby-lang.org
Contact Larry at Alarry@wall.org
Contact Guido at Aguido@python.org

Guido‘s address is guido@python.org

索引

    shoplist[1:3]

    shoplist[2:]

    shoplist[:]

#!/usr/bin/python
#Filename:seq.py
shoplist=[‘apple‘,‘mango‘,‘canot‘,‘banana‘]
print ‘Item 0 is‘,shoplist[0]
print ‘Item 1 is‘,shoplist[1]
print ‘Item 2 is‘,shoplist[2]
print ‘Item 3 is‘,shoplist[3]
print ‘Item -1 is‘,shoplist[-1]
print ‘Item -2 is‘,shoplist[-2]
print ‘Item 1 to 3 is‘,shoplist[1:3]
print ‘Item 2 to end is‘,shoplist[2:]
print ‘Item 1 to to -1 is‘,shoplist[1:-1]
print ‘Item start to end is‘,shoplist[:]
name=‘swaroop‘
print ‘characters 1 to 3 is‘,name[1:3]
print ‘characters 2 to end is‘,name[2:]
print ‘characters 1 to -1 is‘,name[1:-1]
print ‘characters start to end is ‘,name[:]
                                                                   
"using_seq.py" [New] 18L, 600C written
[root@10-8-11-204 ~]# python using_seq.py
Item 0 is apple
Item 1 is mango
Item 2 is canot
Item 3 is banana
Item -1 is banana
Item -2 is canot
Item 1 to 3 is [‘mango‘, ‘canot‘]
Item 2 to end is [‘canot‘, ‘banana‘]
Item 1 to to -1 is [‘mango‘, ‘canot‘]
Item start to end is [‘apple‘, ‘mango‘, ‘canot‘, ‘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

字符串的方法:

if name.startswith(‘Swa‘):

if ‘a‘ in name:

if name.find(‘War‘)!=-1:

delimiter=‘_*_‘

arr=[‘a‘,‘b‘,‘c‘]

delimiter.join(arr)

a_*_b_*_c

#!/usr/bin/python
#filename:str_methods.py
name=‘Swaroop‘
if name.startswith(‘Swa‘):
    print ‘Yes,the string start with Swa‘
if ‘a‘ in name:
    print ‘Yes,it contains the string "a"‘
if name.find(‘war‘)!=-1:
    print ‘Yes,it contains the string "war"‘
delimiter=‘_*_‘
mylist=[‘Brazil‘,‘Russia‘,‘India‘,‘China‘]
print delimiter.join(mylist)
~                                                                                                                                          
~                                                                     
"str_methods.py" 12L, 344C written                  
[root@10-8-11-204 ~]# python str_methods.py
Yes,the string start with Swa
Yes,it contains the string "a"
Yes,it contains the string "war"
Brazil_*_Russia_*_India_*_China


本文出自 “王尼美的成人之路” 博客,转载请与作者联系!

Python开发(二)