论坛首页 编程语言技术论坛

Python学习第三天-Python中的数据结构

浏览 2036 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-11-20   最后修改:2009-11-20

Python有列表 元组(数组) 字典(map表结构)  序列

 

列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引 操作符和切片 操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。

 

请看列表 代码:

'''
Created on 2009-11-20

@author: Administrator
'''

if __name__ == '__main__':
    print "aaa"


def mylist():
    list1 = ["index1","index2",3]
    print "list length :%d " % len(list1)
    print "all list element : %s" % list1
    print "for list : "
    i = 0;
    for a in list1:
        print "Index is %d,and value is %s" % (i,a)
        i=i+1
    print "index is -1,value is %s" % list1[-1]
    print "index is -2,value is %s" % list1[-2]
    
    #append method
    print "append a element : 46"
    list1.append(46);
    print "list length :%d " % len(list1)
    
    #delete index 0 element
    print "Delete index 0 element"
    del list1[0]
    print "list length :%d " % len(list1)
    
    

 

数组 代码:

'''
Created on 2009-11-20

@author: Administrator
'''

if __name__ == '__main__':
    pass

def myarray():
    array1 = ("array_index1","arrray_index2",333,100000)
    print "array length is %d" % len(array1)
    i = 0
    for a in array1:
        print "Index is %d and value is %s" % (i,a)
        i = i+1
    print "Index is -1 and value is %s" % array1[-1]
    
    #error !!!
    #array1[5] = "aaaaa"
    
    array2 = ("array2",array1)
    print "array2:"
    print array2
    print "array in another array:"
    print array2[1]
    print array2[1][0]

 

 字典 代码:

 

'''
Created on 2009-11-20

@author: Administrator
'''

if __name__ == '__main__':
    pass

def myarray():
    array1 = ("array_index1","arrray_index2",333,100000)
    print "array length is %d" % len(array1)
    i = 0
    for a in array1:
        print "Index is %d and value is %s" % (i,a)
        i = i+1
    print "Index is -1 and value is %s" % array1[-1]
    
    #error !!!
    #array1[5] = "aaaaa"
    
    array2 = ("array2",array1)
    print "array2:"
    print array2
    print "array in another array:"
    print array2[1]
    print array2[1][0]
 

 

 

序列 代码:

'''
Created on 2009-11-20

@author: Administrator
'''

if __name__ == '__main__':
    pass
def myseq():
    mylist = ["element1", "element2", "element2", "element3", "element5"]
    print  "Index is 0 , value is  %s" % mylist[0]
    print "Index is 1 , value is  %s" %mylist[1]
    print "Index is 2 , value is  %s" %mylist[2]
    print "Index is 3 , value is  %s" %mylist[3]
    print "Index is 4 , value is  %s" %mylist[4]
    
    
    print "Index is -1 , value is  %s" % mylist[-1]
    print "Index is -2 , value is  %s" % mylist[-2]
    print "Index is -3 , value is  %s" % mylist[-3]
    print "Index is -4 , value is  %s" % mylist[-4]
    print "Index is -5 , value is  %s" % mylist[-5]
    #error
    #print "Index is -6 , value is  %s" % mylist[-6]
    
    
    print "Index is 1---4, value is %s" %mylist[1:4] 
    print "Index is 4---?, value is %s" %mylist[4:] 
    print "Index is -7---?, value is %s" %mylist[-7:]
    print "Index is -6---?, value is %s" %mylist[-6:] 
    print "Index is -4---?, value is %s" %mylist[-4:]
    print "Index is -5---?, value is %s" %mylist[-5:]   
    print "Index is -55---?, value is %s" %mylist[-55:]   
    print "Index is 55---?, value is %s" %mylist[55:]   
    print "Index is ?---55, value is %s" %mylist[:55] 
    print "Index is ?--- -4, value is %s" %mylist[:-4] 
    
    strtest = "I love python"
    print "=====================Test string seq start.....==================="
    print "Index is 1---4, value is %s" %strtest[1:4] 
    print "Index is 4---?, value is %s" %strtest[4:] 
    print "Index is -7---?, value is %s" %strtest[-7:]
    print "Index is -6---?, value is %s" %strtest[-6:] 
    print "Index is -4---?, value is %s" %strtest[-4:]
    print "Index is -5---?, value is %s" %strtest[-5:]   
    print "Index is -55---?, value is %s" %strtest[-55:]   
    print "Index is 55---?, value is %s" %strtest[55:]   
    print "Index is ?---55, value is %s" %strtest[:55] 
    print "Index is ?--- -4, value is %s" %strtest[:-4]  
    
    
    
    
    
       
 

 

测试文件代码:

 

print "=========================================List test========================="
import mylist
#mylist.mylist();

print "=========================================Array test========================="
import myarray
#myarray.myarray()

print "=========================================Map test========================="
import mymap
#mymap.mymap()

print "=========================================Seq test========================="
import myseq
myseq.myseq()

 

 

 

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics