python-列表與元組
阿新 • • 發佈:2019-01-01
1.在python中,是最基本的資料結構是序列(sequence).
序列中的每個元素被分配一個序號----即元素的位置,也稱為索引(第一個索引從0開始,依次類推)
2.序列
列表和元組區別在於,列表可以修改,元組不可修改。
test = ['test hello',36]
print test[0] #resuslt is 'test hello'
print test[1] #result is 36
print test #result is ['test hello',36]
test = ['test hello',40]
value = ['value hello' ,30]
new = [test, value]
print new #restulst [['test hello',40],['value hello',30]]
備註:容器資料結構,容器基本包含其的物件的任意物件。序列和對映是兩類容器。
序列中的每個元素都有自己的編號,而對映有一個名字(稱為鍵)
2.1.通用序列操作。
所有的序列都可以進行某種特定的操作,比如索引(index),分片(slicing),加(adding),乘(multplying)
以及檢查某個元素的序列的成員。除此之外還可以計算序列的長度,找出最大元素和最小元素的內鍵函式
2.1 .1索引(index)
test = 'hello'
print test[0] #resutl is h
負數索引(從右邊開始查詢)
print test[-1] #resuslt is o
直接對返回結果進行索引操作:
test = raw_input("Year: ")[3] #input 2016
print test #返回結果是test序列的第四個元素 6
索引示列:
months = [
'January',
'Febuary',
'March',
'April' ,
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'Decmber'
]
#以下1-31的數字作為結尾的列表
endings = ['st','nd','rd'] + 17 * ['th']\
+['st','nd','rd'] + 7 * ['th']\
+['st']
year = raw_input("Year: ") 1974
month = raw_input("Month (1-12): ") 8
day = raw_input('Day(1-31): ') 16
#記得要將月份和天數-1,獲得正確索引
month_name = months[month_number -1]
ordinal = day + endings[day_number -1]
print month_name +' '+ ordinal + ' '+ year
2.1.2.分片
與使用索引來訪問單個元素類似,可以使用分片來訪問一定範圍內的元素。
分片能過冒號隔開的兩個索引來實現
test = 'http://www.perl.org'
print test[11:15] #reulst is perl
print test[11:-4] #result is perl ,可以使用負數來操作
test = [1,2,3,4,5,6,7,8,9,10]
print test[3:6] #resutl is 4,5,6
print test[-3:-1] #resutl is 8,9,10
按步長分片
test=[1,2,3,4,5,6,7,8,9,10]
print test[0:10:1] #按一個一個來分片範圍0:10 result si 1,2,3,4,5,6,7,8,9,19
print test[1:10:2] #範圍是0:10,每兩步進行分片,result is 1,3,5,7,9
print test[1:10:4] # result is 1,5,9
從右邊按步長分處
print test[8:3:-1] # 9,8,7,6,5
2.1.3.序列相加
[1,2,3] + [4,5,6] #result is [1,2,4,5,6]
'hello' + 'world' #result is helloworld
2.1.4.乘法(重複次數)
'perl' * 5 #'perlperlperlperlperl'
[6] * 5 # result is [6,6,6,6,6]
空列表可以用[]表示
None是一個python內鍵值,表示這裡什麼都沒有 #perl同類值是undef,代表這裡空無一物,走開走開
2.1.5.檢測成員(布林值)
test = 'hello'
'h' in test #result is True
'w' in test #result is False
test = ['laomeng','wang','lisi']
raw_input('Enter your name: ') in test
#laomeng result is True
2.1.6.計算長度,最小值和最大值
len(),
min(),
max()
test=[1,2,3,4,5,6,7]
len(test) #result is 7
minx(test) #result is 1
max(test) #result is 7
3.列表
列表是可以變化的--可以改變列表的內容,並且列表有很多有用的,專門的方法
3.1.1.list()函式
list()函式適用於所有型別的序列,而不只是字串
建立列表
list('hello')
['h','e','l','l','o']
3.1.2.刪除元素
test =['abc','abd','abe','abf']
del test[2] #刪除第三個元素
3.1.3.分片賦值
test = list('Perl') #'P','e','r','l'
test[2:] = list('ar') #result is 'P','e','a','r'
test=[1,5]
test[1:1] =[2,3,4] #result is ['1,2,3,4,5]
刪除元素
test =[1,2,3,4,5]
test[1:4]=[] #result is [1,5]
3.2.列表方法
test =[1,2,3]
3.2.1.append方法用於在末尾增加新的元素
test = [1,2,3,4]
append.test[5] #resut is [1,2,3,4,5]
在perl指令碼有同樣的功能
my @test = qw(1 2 3 4);
push @test,5;
print "@test\n"; #result is 1 2 3 4 5
3.2.2.count統計某個元素在列表出現的次數
test = ['a','b','a','b','a','d']
test.count('a') # result is 3
3.2.3.extend方法可以在序列末尾追加多個值
test = [1,2,3,4,5]
b=[5,7,8]
test.extend(b) #rest 1,2,3,4,5,5,7,8
3.3.4.index方法用於從列表中找出某個值的位置
test =['abc','abd','abe','abf','abg']
test.index('abe') #result is 2
3.3.5.insert方法用於將物件插入列表中
test = [1,2,3,4,5]
test.insert(3,'hello') #result is 1,2,3,'hello',4,5
3.3.6.pop方法移除列表中的一個元素,預設是最後一個元素,pop是有返回值
test = [1,2,3,4,5]
test.pop() #result is 1,2,3,4
test.append(pop()) #result is 1,2,3,4,5
3.3.7.remove方法用於移除列表中某個匹配的值
test =['a','b','c','d']
test.remove('a') #result is b,c,d
3.3.8.reverse結果取反存放
x=[1,2,3,4]
x.reverse() #result is 4,3,2,1
x = 'hello'
x.list(reverse()) #result is o,l,l,h
3.3.9.sort方法是進行排序
x=[4,6,2,1,7,9];
x.sort() #result is 1,2,4,6.7,9
y =x[:] #:切片包包含所有的值
y.sort() #result is 1,2,4,6.7,9
4.元組: 不可改變序列
1,2,3 #restult is (1,2,3)
() #空元組
4.1.1.tulpe函式功能與list函式功能基本上一樣
tulpe([1,2,3]) #result is (1,2,3)
4.1.2元組的基本操作
x = 1,2,3
x[1] #result is 2
x[0:2] #result is 1,2
總結:
序列:是一種資料結構,它包含了元素都行了編號(從0開始).
序列包括了列表,字串,元組。其中列表可以改變修改的
而元組是不可以修改的。通過分片捷捷操作符可以訪問序列的一部分(範圍),其中分片需要兩個索引來
指出分片的起始和結束位置。
序列成員檢測。in操作符可以檢查一個值是否存在於序列中。